将嵌入的Word文档保存为PDF [英] Save embedded Word Doc as PDF

查看:325
本文介绍了将嵌入的Word文档保存为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景



Word文档嵌入Excel 2011文件。我需要将其保存为pdf。



如果是Excel 2010,那么它不会是一个问题,因为MS P Office支持OLE自动化。 / p>

我尝试了什么?



这是我在Excel中尝试的代码2010年的作品。

  Option Explicit 

子样本()
Application.ScreenUpdating = False

Dim shp As Shape
Dim objWord As Object
Dim objOLE As OLEObject

Set shp = Sheets(Sheet1)。形状(对象1)

shp.OLEFormat.Activate

设置objOLE = shp.OLEFormat.Object

设置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,K eepIRM:= 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,也不会给出任何错误消息。我尝试调试,但没有喜悦。

 '~~>参考设置为MS Word对象库
选项显式

子样本()
Dim oWord As Word.Application,oDoc As Word.Document

应用程序.ScreenUpdating = False

表格(Sheet1)。Shapes.Range(Array(Object 1))。选择

Selection.Verb Verb:= xlPrimary

设置oWord = GetObject(,word.application)

对于每个oDoc在oWord.Documents
Debug.Print oDoc.FullName& .pdf

oDoc.SaveAs文件名:= oDoc.FullName& .pdf,FileFormat:= wdFormatPDF
oDoc.Close savechanges:= False
下一个oDoc

oWord.Quit

设置oworddoc = Nothing

设置oWord = Nothing
Application.ScreenUpdating = True
End Sub

我相信这也可以使用AppleScript。所以我也用Applescript进行了测试。在这里,我试图将word文档直接转换成pdf。如果我得到这个部分,那么我可以在我的代码中走一条小路:)

 子测试者()
Dim scriptToRun As String

scriptToRun =set pdfSavePath to& Chr(34)& 用户:siddharth:文件:Sid.pdf& Chr(34)& Chr(13)
scriptToRun = scriptToRun& 设置DocFile选择文件提示& Chr(34)& 请选择一个Word文档文件:& Chr(34)& Chr(13)
scriptToRun = scriptToRun& 告诉申请& Chr(34)& 微软Word& Chr(34)& Chr(13)
scriptToRun = scriptToRun& 打开DocFile& Chr(13)
scriptToRun = scriptToRun& 将theActiveDoc设置为活动文档& Chr(13)
scriptToRun = scriptToRun& 另存为theActiveDoc文件格式格式PDF文件名pdfSavePath& Chr(13)
scriptToRun = scriptToRun& 结束告诉& Chr(13)

Debug.Print scriptToRun
'Result = MacScript(scriptToRun)
'MsgBox Result
End Sub

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



SNAPSHOT





Applescript错误





QUESTION



如何在Excel 2011中保存嵌入的单词文档?我对VBA和Applescript开放。

解决方案

嗯,我会死的!



谢谢Pradeep的建议。看起来像您所指的应用程序已经过时了新的MAC版本。所以我搜索了MAC Store,发现另一个名为 SMILE 的应用程序。



我测试了SMILE中的原始脚本。没有任何错误,它的工作完美!!!

 将pdfSavePath设置为用户:siddharth:文件:Sid.pdf 
设置theDocFile选择文件提示请选择一个Word文档文件:
告诉应用程序Microsoft Word
打开theDocFile
将theActiveDoc设置为活动文档
保存为ActiveDoc文件格式格式PDF文件名pdfSavePath
结束告诉

所以我试过我以前测试的代码,令我惊讶的是,这次工作没有我对原始代码进行任何更改!所以我很遗憾可能是什么问题... Smile 安装的东西使脚本在Excel中工作?猜测我永远都不会发现。

  Option Explicit 

子测试者()
Dim scriptToRun As String

scriptToRun =set pdfSavePath to& Chr(34)& 用户:siddharth:文件:Sid.pdf& Chr(34)& Chr(13)
scriptToRun = scriptToRun& 设置DocFile选择文件提示& Chr(34)& 请选择一个Word文档文件:& Chr(34)& Chr(13)
scriptToRun = scriptToRun& 告诉申请& Chr(34)& 微软Word& Chr(34)& Chr(13)
scriptToRun = scriptToRun& 打开DocFile& Chr(13)
scriptToRun = scriptToRun& 将theActiveDoc设置为活动文档& Chr(13)
scriptToRun = scriptToRun& 另存为theActiveDoc文件格式格式PDF文件名pdfSavePath& Chr(13)
scriptToRun = scriptToRun& 结束告诉& Chr(13)

Debug.Print scriptToRun
结果= MacScript(scriptToRun)
MsgBox结果
End Sub
/ pre>

SMILE SNAPSHOT





编辑:发现错误 p>

仔细检查,我发现我的原始脚本有一个额外的线。我正在设置PDF路径两次。可以在快照中看到。


SCENARIO

A word document is embedded in Excel 2011 file. I need to save it as a pdf.

Had it been Excel 2010 then it wouldn't have been a problem as MS-Office in Win Pcs support OLE automation.

WHAT HAVE I TRIED?

This is the code that I tried in Excel 2010 which works.

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

Obviously I cannot use the same in MAC. Not that I didn't try this in MAC... I did :-/ (Basic human nature I guess?). It failed as expected. :)

For Excel 2011, I tried this. It works but doesn't create a pdf nor does it give any error message. I tried debugging it but no joy.

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

I believe this can also be done using AppleScript. So I tested with Applescript as well. Here I am trying to convert a word document directly to pdf. If I get this part then I can take a small detour in my 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

However I get the runtime error on MacScript(scriptToRun) so I am sure that my Applescript is failing.

SNAPSHOT

Applescript Error

QUESTION

How can I save the embedded word doc in Excel 2011? I am open to VBA and Applescript.

解决方案

Well, I will be damned!

Thanks Pradeep for your suggestion. Seems like the application that you were referring to is obsolete with new MAC versions. So I searched the MAC Store and found another application called SMILE.

I tested the original script in in SMILE. There was nothing wrong with it and it worked perfectly!!!

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

So I tried the code which I was testing earlier and to my surprise, it worked this time without me making any change to the original code!!! So I am stumped on what could be the problem... Did Smile install something which made the script work in Excel? Guess I will never find out.

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

EDIT: FOUND THE ERROR

On closer inspection, I found that my original script had an extra line. I was setting the PDF path twice. Can be seen in the snapshot.

这篇关于将嵌入的Word文档保存为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆