Speichern Sie das eingebettete Word-Dokument als PDF

SZENARIO

Ein Word-Dokument ist in eine Excel 2011-Datei eingebettet. Ich muss es als pdf speichern.

Wäre es Excel 2010 gewesen, wäre es kein Problem gewesen, da MS-Office in Windows-PCs die OLE-Automatisierung unterstützt.

WAS HABE ICH VERSUCHT?

Dies ist der Code, den ich in Excel 2010 ausprobiert habe und der funktioniert.

<code>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
</code>

Offensichtlich kann ich nicht das gleiche in MAC verwenden. Nicht, dass ich das in MAC nicht ausprobiert hätte ... Ich tat: - / (Grundlegende menschliche Natur, denke ich?). Es ist wie erwartet fehlgeschlagen. :)

Für Excel 2011 habe ich dies versucht. Es funktioniert, aber es wird weder ein PDF erstellt noch eine Fehlermeldung ausgegeben. Ich habe versucht, es zu debuggen, aber keine Freude.

<code>'~~> 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
</code>

Ich glaube, dass dies auch mit AppleScript möglich ist. Also habe ich auch mit Applescript getestet. Hier versuche ich, ein Word-Dokument direkt in PDF umzuwandeln. Wenn ich diesen Teil bekomme, kann ich einen kleinen Umweg in meinem Code machen :)

<code>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
</code>

Allerdings bekomme ich den Laufzeitfehler anMacScript(scriptToRun) Ich bin mir also sicher, dass mein Applescript nicht funktioniert.

SCHNAPPSCHUSS

AppleScript-Fehler

FRAGE

Wie kann ich das eingebettete Word-Dokument in Excel 2011 speichern? Ich bin offen für VBA und Applescript.

Antworten auf die Frage(1)

Ihre Antwort auf die Frage