vbakf_word中的VBA应用实例「建议收藏」

(115) 2024-05-21 20:01:01

word另存成PDF如果用宏的方式解决,可以录制如下:

Sub 宏1()
'
' 宏1 宏
'
'
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "X:\AHK\office\测试另存成pdf.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

不好的地方是每次都需要启用宏才行,如何不用启动宏呢,那据我所知最好的办法就是写ahk脚本,在ahk圈中戏称写vbk。

以上宏vba改写vbk如下:

oWord:=ComObjActive("Word.Application")
oWord.ActiveDocument.ExportAsFixedFormat(OutputFileName:=A_Desktop "\test4.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)
 oWord := ""

方法就是 去掉原来宏中的_连接符将后面内容提上来,并在逗号前面回车 使得每行独立成行,在前面创建oWord := ComObjActive("Word.Application")对象。用该com对象调用相关函数oWord.ActiveDocument.ExportAsFixedFormat

并把参数用括号括起来。

做到以上还差关键一步,就是将微软的一些常数声明一下:

wdExportFormatPDF := 17
wdExportOptimizeForPrint := 0
wdExportAllDocument := 0
wdExportDocumentContent := 0
wdExportCreateNoBookmarks := 0 

合起来如下,至于如何加热键那是ahk拿手好戏,这里就不赘述了:


wdExportFormatPDF := 17
wdExportOptimizeForPrint := 0
wdExportAllDocument := 0
wdExportDocumentContent := 0
wdExportCreateNoBookmarks := 0 


oWord:=ComObjActive("Word.Application")
oWord.ActiveDocument.ExportAsFixedFormat(OutputFileName:=A_Desktop "\test4.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)
 
oWord := ""

THE END

发表回复