vba Word宏在文件保存时另存为PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9309081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Word Macro to save as PDF when file saves
提问by Dycey
I'm trying to create a macro that executes when a file is saved from Word (or Excel) 2007+. The macro needs ot check the name/location of the file to decide whether to execute, and then if the filename checks out (probably because it has '_temp' appended to it, or exists in the \temp folder) then as well as saving the file, Word also saves to PDF with the same name but obviously with the .pdf extension instead. Preferably I'd like the PDFing to happen prior to the save, but I'm not fussed. The Word client already has the SaveAsPDForXPS plug-in installed.
我正在尝试创建一个宏,该宏在从 Word(或 Excel)2007+ 保存文件时执行。宏需要检查文件的名称/位置来决定是否执行,然后如果文件名检出(可能是因为它附加了“_temp”,或者存在于 \temp 文件夹中)然后以及保存文件,Word 也使用相同的名称保存为 PDF,但显然使用 .pdf 扩展名。最好我希望在保存之前进行 PDFing,但我并不大惊小怪。Word 客户端已经安装了 SaveAsPDForXPS 插件。
So far, I've managed to figure out that I need a macro with FileSave() handler in it, and that (from recording a test macro) the save bit might look like:
到目前为止,我已经设法弄清楚我需要一个带有 FileSave() 处理程序的宏,并且(通过录制测试宏)保存位可能如下所示:
Sub FileSave()
'
' FileSave Macro
'
'
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"C:\Documents and Settings\rdyce\Desktop\Doc1.pdf", ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub
回答by Dycey
OK, I think that this does the jjob, but any pointers to glaring errors still gratefully received. Also, how to turn it into an Addin is still proving puzzling:
好的,我认为这可以完成 jjob,但是仍然很感激地收到了任何指向明显错误的指针。此外,如何将其变成 Addin 仍然令人费解:
Sub FileSave()
'
' FileSave Macro
'
ActiveDocument.Save
Dim StrFile As String
Dim StrPath As String
Dim StrName As String
Dim StrPDFName As String
StrPath = ActiveDocument.Path 'Get document path
StrFile = ActiveDocument.Name 'Get document name
If InStr(StrFile, "_tempkey") Then 'Check if this is a special file
If InStr(StrFile, ".") Then
StrName = Left(StrFile, (InStr(StrFile, ".") - 1))
Else
StrName = StrFile
End If
StrPDFName = StrPath + "\" + StrName + ".pdf"
ActiveDocument.ExportAsFixedFormat OutputFileName:=StrPDFName, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, _
OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
End If
End Sub