[英]Save embedded Word Doc as 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 失敗了。
快照
Applescript 錯誤
問題
如何在 Excel 2011 中保存嵌入的 Word 文檔? 我對 VBA 和 Applescript 持開放態度。
好吧,我會被詛咒的!
感謝普拉迪普的建議。 似乎您所指的應用程序在新的 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
微笑快照
編輯:發現錯誤
仔細檢查后,我發現我的原始腳本多了一行。 我設置了兩次 PDF 路徑。 可以在快照中看到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.