【问题标题】:Loop all pages and get the first line with text循环所有页面并获取第一行文本
【发布时间】:2020-11-23 00:54:19
【问题描述】:

我想使用 VBA 循环我的 MS Word 文档的页面并将每个页面分别打印为 PDF。每个 PDF 文件的名称必须是每页中带有文本的第一行。我正在尝试以下方式:

Sub printSepPdf()

        Dim pages As Page
        Dim p As Paragraph
        
    
    
        For Each pages In ActiveDocument
            ActiveDocument.ExportAsFixedFormat OutputFileName:=p.Range.Sentences(1) & ".pdf", ExportFormat:=wdExportFormatPDF, _
            OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
            wdExportFromTo, From:=1, To:=1, Item:=wdExportDocumentContent, _
            IncludeDocProps:=False, KeepIRM:=False, CreateBookmarks:= _
            wdExportCreateHeadingBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=False, UseISO19005_1:=False
    
    Next pages
    
     
    End Sub

【问题讨论】:

标签: vba loops pdf printing ms-word


【解决方案1】:

您的代码存在一些问题...ActiveDocument 实际上并没有直接引用 Pages ...您没有在任何地方设置 Paragraph p ...页码在导出中是硬编码的。

试试下面的。它遍历文档的每一页(注意文档必须在 PrintLayout 视图中),然后获取第一句,将其减 1 以删除段落结尾字符,并创建 PDF 文件名。然后它将整个页面保存为 PDF。

Sub printSepPdf()

    Dim PageCounter As Long
    Dim PageFirstSentence As Range
    Dim PDFName As String
    
    For PageCounter = 1 To ActiveDocument.Windows(1).Panes(1).pages.Count
        Set PageFirstSentence = ActiveDocument.Range.GoTo(wdGoToPage, wdGoToAbsolute, PageCounter)
        PageFirstSentence.Expand (wdSentence)
        PageFirstSentence.SetRange PageFirstSentence.Start, PageFirstSentence.End - 1
        PDFName = PageFirstSentence & ".pdf"
        
        ActiveDocument.ExportAsFixedFormat OutputFileName:=PDFName, _
        ExportFormat:=wdExportFormatPDF, _
        OpenAfterExport:=False, _
        OptimizeFor:=wdExportOptimizeForPrint, _
        Range:=wdExportFromTo, _
        From:=PageCounter, _
        To:=PageCounter, _
        Item:=wdExportDocumentContent, _
        IncludeDocProps:=False, _
        KeepIRM:=False, _
        CreateBookmarks:= _
        wdExportCreateHeadingBookmarks, _
        DocStructureTags:=True, _
        BitmapMissingFonts:=False, _
        UseISO19005_1:=False
        
    Next

End Sub

【讨论】:

    猜你喜欢
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 2019-04-03
    相关资源
    最近更新 更多