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的失敗。
快照
AppleScript的錯誤
問題
我如何保存在Excel 2011中嵌入Word文檔?我對VBA和Applescript很開放。
+ 1對於很好解釋的問題。我沒有MAC,但看看這個鏈接是否有幫助?也許你可以先在APPLESCRIPT中嘗試腳本? http://homepage.mac.com/swain/Macinchem/Applescript/AppScript_tut/AppScrip_tut_1/appscript_tut_1.htm –
感謝Pradeep。我檢查了我的MAC,但在Applications文件夾中找不到名爲Applescript的文件夾。也許我需要下載它?讓我檢查蘋果商店。 –
@PradeepKumar:感謝您的指導。這真的有幫助。 –