2012-05-09 189 views
7

情景保存嵌入Word文檔爲PDF

Word文檔嵌入在Excel 2011文件。我需要將它保存爲pdf。

如果它是Excel 2010,那麼Win Pcs中的MS-Office支持OLE自動化將不會出現問題。

我試過了什麼?

這是我在Excel 2010中工作的代碼。

Option Explicit 

Sub Sample() 
    Application.ScreenUpdating = False 

    Dim shp As Shape 
    Dim objWord As Object 
    Dim objOLE As OLEObject 

    Set shp = Sheets("Sheet1").Shapes("Object 1") 

    shp.OLEFormat.Activate 

    Set objOLE = shp.OLEFormat.Object 

    Set objWord = objOLE.Object 

    objWord.ExportAsFixedFormat OutputFileName:= _ 
      "C:\Users\Siddharth Rout\Desktop\Sid.pdf", ExportFormat:= _ 
      17, OpenAfterExport:=True, OptimizeFor:= _ 
      0, Range:=0, From:=1, To:=1, _ 
      Item:=0, IncludeDocProps:=True, KeepIRM:=True, _ 
      CreateBookmarks:=0, DocStructureTags:=True, _ 
      BitmapMissingFonts:=True, UseISO19005_1:=False 

    objWord.Application.Quit 

    Set objWord = Nothing 
    Set shp = Nothing 
    Set objOLE = Nothing 

    Application.ScreenUpdating = True 
End Sub 

很明顯,我不能在MAC中使用相同的。不是我沒有在MAC中試過這個...我做過: - /(基本人性我猜?)。它按預期失敗。 :)

對於Excel 2011,我試過這個。它的工作原理,但不創建PDF也不會給出任何錯誤消息。我試着調試它,但沒有喜悅。

'~~> Reference set to MS Word Object Library 
Option Explicit 

Sub Sample() 
    Dim oWord As Word.Application, oDoc As Word.Document 

    Application.ScreenUpdating = False 

    Sheets("Sheet1").Shapes.Range(Array("Object 1")).Select 

    Selection.Verb Verb:=xlPrimary 

    Set oWord = GetObject(, "word.application") 

    For Each oDoc In oWord.Documents 
     Debug.Print oDoc.FullName & ".pdf" 

     oDoc.SaveAs Filename:=oDoc.FullName & ".pdf", FileFormat:=wdFormatPDF 
     oDoc.Close savechanges:=False 
    Next oDoc 

    oWord.Quit 

    Set oworddoc = Nothing 

    Set oWord = Nothing 
    Application.ScreenUpdating = True 
End Sub 

我相信這也可以使用AppleScript來完成。所以我也用Applescript進行了測試。在這裏,我試圖直接將word文檔轉換爲pdf。如果我得到這個部分,那麼我可以帶一個小的彎路,我的代碼:)

Sub tester() 
    Dim scriptToRun As String 

    scriptToRun = "set pdfSavePath to " & Chr(34) & "Users:siddharth:Documents:Sid.pdf" & Chr(34) & Chr(13) 
    scriptToRun = scriptToRun & "set theDocFile to choose file with prompt " & Chr(34) & "Please select a Word document file:" & Chr(34) & Chr(13) 
    scriptToRun = scriptToRun & "tell application " & Chr(34) & "Microsoft Word" & Chr(34) & Chr(13) 
    scriptToRun = scriptToRun & "open theDocFile" & Chr(13) 
    scriptToRun = scriptToRun & "set theActiveDoc to the active document" & Chr(13) 
    scriptToRun = scriptToRun & "save as theActiveDoc file format format PDF file name pdfSavePath" & Chr(13) 
    scriptToRun = scriptToRun & "end tell" & Chr(13) 

    Debug.Print scriptToRun 
    'Result = MacScript(scriptToRun) 
    'MsgBox Result 
End Sub 

但是我得到MacScript(scriptToRun)運行時錯誤,所以我敢肯定,我的AppleScript的失敗。

快照

enter image description here

AppleScript的錯誤

enter image description here

問題

我如何保存在Excel 2011中嵌入Word文檔?我對VBA和Applescript很開放。

+1

+ 1對於很好解釋的問題。我沒有MAC,但看看這個鏈接是否有幫助?也許你可以先在APPLESCRIPT中嘗試腳本? http://homepage.mac.com/swain/Macinchem/Applescript/AppScript_tut/AppScrip_tut_1/appscript_tut_1.htm –

+1

感謝Pradeep。我檢查了我的MAC,但在Applications文件夾中找不到名爲Applescript的文件夾。也許我需要下載它?讓我檢查蘋果商店。 –

+1

@PradeepKumar:感謝您的指導。這真的有幫助。 –

回答

5

嗯,我會被詛咒!

感謝Pradeep的建議。看起來你所指的應用程序已經過時了新的MAC版本。於是我搜索了MAC商店,發現另一個名爲SMILE的應用程序。

我在SMILE中測試了原稿腳本。它沒有錯,它完美的工作!

set pdfSavePath to "Users:siddharth:Documents:Sid.pdf" 
set theDocFile to choose file with prompt "Please select a Word document file:" 
tell application "Microsoft Word" 
    open theDocFile 
    set theActiveDoc to the active document 
    save as theActiveDoc file format format PDF file name pdfSavePath 
end tell 

所以,我想這是我早先測試代碼,並讓我吃驚,它的工作這一次我沒有做任何改變原來的代碼!所以我難住可能是什麼問題...... Smile安裝了什麼使腳本在Excel中工作?猜猜我永遠不會知道。

Option Explicit 

Sub tester() 
    Dim scriptToRun As String 

    scriptToRun = "set pdfSavePath to " & Chr(34) & "Users:siddharth:Documents:Sid.pdf" & Chr(34) & Chr(13) 
    scriptToRun = scriptToRun & "set theDocFile to choose file with prompt " & Chr(34) & "Please select a Word document file:" & Chr(34) & Chr(13) 
    scriptToRun = scriptToRun & "tell application " & Chr(34) & "Microsoft Word" & Chr(34) & Chr(13) 
    scriptToRun = scriptToRun & "open theDocFile" & Chr(13) 
    scriptToRun = scriptToRun & "set theActiveDoc to the active document" & Chr(13) 
    scriptToRun = scriptToRun & "save as theActiveDoc file format format PDF file name pdfSavePath" & Chr(13) 
    scriptToRun = scriptToRun & "end tell" & Chr(13) 

    Debug.Print scriptToRun 
    Result = MacScript(scriptToRun) 
    MsgBox Result 
End Sub 

SMILE快照

enter image description here

編輯:發現錯誤

仔細觀察,我發現,我原來的劇本有一個額外的線。我設置了兩次PDF路徑。可以在快照中看到。

+1

我唯一擔心的是,如果我的懷疑是正確的,Excel代碼的工作只是因爲我安裝了SMILE,那麼這是一個問題。在這種情況下,如果我想分發我的代碼,那麼它不會工作,因爲用戶MAC可能沒有SMILE。有人可以使用MAC(使用Excel 2011)爲我測試上述代碼嗎? –

+0

+ 1自己解決:) –

+0

+1自我解決的好解釋 – brettdj