2012-05-09 3 views
7

시나리오PDF로 포함 된 Word 문서를 저장

워드 문서는 엑셀 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를 실패 것을 확신합니다.

스냅

enter image description here

애플 스크립트 오류

enter image description here

가 어떻게 2011 Excel에서 포함 된 Word 문서를 저장할 수 있습니다 질문? 나는 VBA와 AppleScript로 공개되어 있습니다.

+1

+1 멋지게 설명 된 질문입니다. MAC가 없지만이 링크가 도움이되는지 확인하십시오. 어쩌면 당신은 APPLESCRIPT에서 스크립트를 먼저 시도 할 수 있습니까? http://homepage.mac.com/swain/Macinchem/Applescript/AppScript_tut/AppScrip_tut_1/appscript_tut_1.htm –

+1

감사합니다. MAC을 확인했지만 응용 프로그램 폴더에서 AppleScript라는 폴더를 찾을 수 없습니다. 아마 내가 그것을 다운로드해야합니까? 애플 스토어를 확인하겠습니다. –

+1

@PradeepKumar : 방향에 감사드립니다. 정말 도움이되었습니다. –

답변

5

글쎄, 나는 저주받을 것이다!

제안 해 주신 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 

스마일 스냅

enter image description here

편집이 : 오류 정밀한 점검에

을 발견 내 원래 스크립트가 여분의 라인을주었습니다. PDF 경로를 두 번 설정했습니다. 스냅 샷에서 볼 수 있습니다.

+1

내 유일한 관심사는 SMILE을 설치했기 때문에 Excel 코드가 올바로 작동한다는 의구심이 맞다면 문제가되는 것입니다. 그런 시나리오에서, 만약 내가 내 코드를 배포하고 싶다면 사용자 MAC가 SMILE을 가지지 않기 때문에 작동하지 않을 것이다. MAC (Excel 2011 포함)을 가진 사람이 위의 코드를 테스트 할 수 있습니까? –

+0

+1 직접 해결하기 위해 :) –

+0

자기 해결에 +1 좋은 설명 – brettdj