2012-05-09 14 views
7

SCENARIOPDFとして埋め込まれた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でもテストしました。ここで私は単語の文書をpdfに直接変換しようとしています。私はこの部分を取得する場合、私は私のコードに小さな回り道をすることができます:)私はMacScript(scriptToRun)上のランタイムエラーを取得するしかし

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 

ので、私は私のAppleScriptが失敗していることを確信しています。

SNAPSHOT

enter image description here

のAppleScriptエラー

enter image description here

は、どのように私は、2011年Excelで埋め込まれたWord文書を保存することができますQUESTION?私はVBAとApplescriptにオープンしています。

+1

+1きれいに説明された質問のため。 MACはありませんが、このリンクが役立つかどうかを確認してください。おそらく、あなたはAPPLESCRIPTで最初にスクリプトを試すことができますか? http://homepage.mac.com/swain/Macinchem/Applescript/AppScript_tut/AppScrip_tut_1/appscript_tut_1.htm –

+1

ありがとうPradeep。 MACを確認しましたが、アプリケーションフォルダ内のApplescriptというフォルダが見つかりませんでした。たぶん私はそれをダウンロードする必要がありますか?リンゴの店をチェックさせてください。 –

+1

@PradeepKumar:方向をありがとう。それが本当に助けになりました。 –

答えて

5

まあ、私は卑劣になります!

あなたの提案に感謝します。あなたが参照していたアプリケーションが新しい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のSNAPSHOT

enter image description here

EDITは:ERROR精密検査で

を発見し、私は私の元のスクリプトは、余分なラインを持っていたことが分かりました。私はPDFパスを2回設定していました。スナップショットで見ることができます。

+1

私の唯一の懸念は、ExcelコードがSMILEをインストールしたためにしかうまくいかないという私の疑惑が正しければ、それは問題であるということです。このようなシナリオでは、自分のコードを配布したい場合、ユーザーのMACがSMILEを持たないかもしれないので、コードは機能しません。 MAC(Excel 2011)搭載の誰かが私のために上記のコードをテストできますか? –

+0

+1自分で解決するために:) –

+0

自己解決の+1良い説明 – brettdj

関連する問題