SCENARIO
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持开放态度。
答案 0 :(得分:6)
好吧,我会被诅咒!
感谢Pradeep的建议。看起来你所指的应用程序已经过时了新的MAC版本。所以我搜索了MAC Store并找到了另一个名为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 SNAPSHOT
编辑:发现错误
仔细观察后,我发现原来的剧本有一条额外的线条。我正在设置两次PDF路径。可以在快照中看到。