-->

保存嵌入Word文档为PDF(Save embedded Word Doc as PDF)

2019-06-24 06:27发布

情景

Word文档嵌入在Excel 2011文件。 我需要将其保存为PDF。

如果这是Excel 2010中则不会一直作为微软Office在Win PC支持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。

Answer 1:

好吧,我该死!

普拉迪普感谢您的建议。 好像是你指的是过时的新的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快照

编辑:发现错误

仔细观察,我发现,我原来的剧本有一个额外的线。 我设置的两倍PDF路径。 可以在快照中看到。



文章来源: Save embedded Word Doc as PDF