将嵌入式Word文档另存为PDF

Sid*_*out 9 excel vba excel-vba applescript-excel excel-vba-mac

情景

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
Run Code Online (Sandbox Code Playgroud)

显然我不能在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
Run Code Online (Sandbox Code Playgroud)

我相信这也可以使用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
Run Code Online (Sandbox Code Playgroud)

但是我得到了运行时错误,MacScript(scriptToRun)所以我确信我的Applescript失败了.

快照

在此输入图像描述

Applescript错误

在此输入图像描述

如何在Excel 2011中保存嵌入的word文档?我对VBA和Applescript持开放态度.

Sid*_*out 7

好吧,我会被诅咒!

感谢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
Run Code Online (Sandbox Code Playgroud)

所以我尝试了之前测试过的代码,令我惊讶的是,这次我没有对原代码进行任何更改!所以我对可能出现的问题感到困惑......是否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
Run Code Online (Sandbox Code Playgroud)

微笑快照

在此输入图像描述

编辑:发现错误

仔细观察后,我发现我的原始剧本有一条额外的线.我正在设置两次PDF路径.可以在快照中看到.

  • 我唯一关心的是,如果我猜对了Excel代码仅因为我安装了SMILE而起作用,那么这是一个问题。在这种情况下,如果我想分发我的代码,则它将无法工作,因为用户MAC可能没有SMILE。拥有MAC的人(使用Excel 2011)可以为我测试以上代码吗? (2认同)