9

设想

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 持开放态度。

4

1 回答 1

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

所以我尝试了我之前测试的代码,令我惊讶的是,这次它在我没有对原始代码进行任何更改的情况下工作!!!所以我很困惑可能是什么问题......是否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 路径。可以在快照中看到。

于 2012-05-09T11:42:00.367 回答