【问题标题】:Save the Word file with Specific Text in the Word document在 Word 文档中保存带有特定文本的 Word 文件
【发布时间】:2021-04-29 14:50:34
【问题描述】:

我是 VBA 新手,并尝试编写可以将多页 word 文档另存为单独 PDF 的代码。

我不仅在文件名上苦苦挣扎,目前,它正在使用 Page no 保存,但我要求它是 Word 文档中的特定字段。

我已经添加了word doc的截图,文件名应该是“User ID”和“Reminder 1 (this is fixed value)”.doc

enter image description here

这是我的代码

Sub SaveAsSeparatePDFs()
    Dim I As Long
    Dim xDlg As FileDialog
    Dim xFolder As Variant
    Dim xStart, xEnd As Integer
    On Error GoTo lbl
    Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)
    If xDlg.Show <> -1 Then Exit Sub
    xFolder = xDlg.SelectedItems(1)
    xStart = CInt(InputBox("Select Start Page No", "Information"))
    xEnd = CInt(InputBox("Select End Page No", "Information"))
    If xStart <= xEnd Then
        For I = xStart To xEnd
            ActiveDocument.ExportAsFixedFormat OutputFileName:= _
                xFolder & "\Reminder 1" & I & ".pdf", ExportFormat:=wdExportFormatPDF, _
                OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
                wdExportFromTo, From:=I, To:=I, Item:=wdExportDocumentContent, _
                IncludeDocProps:=False, KeepIRM:=False, CreateBookmarks:= _
                wdExportCreateHeadingBookmarks, DocStructureTags:=True, _
                BitmapMissingFonts:=False, UseISO19005_1:=False
        Next
    End If
    Exit Sub
lbl:
    MsgBox "Enter right page number", vbInformation, "ERROR"
End Sub

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    无法从您的屏幕截图中确定您的 ID# 在哪个段落中。它似乎在第 3 段中,在这种情况下:

    Sub SaveAsSeparatePDFs()
        Dim I As Long, Rng As Range
        Dim xDlg As FileDialog, xFolder As Variant
        Dim xStart As Long, xEnd As Long
        Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)
        If xDlg.Show <> -1 Then Exit Sub
        xFolder = xDlg.SelectedItems(1)
        On Error GoTo lbl
        xStart = CInt(InputBox("Select Start Page No", "Information"))
        xEnd = CInt(InputBox("Select End Page No", "Information"))
        If xStart <= xEnd Then
            For I = xStart To xEnd
                With ActiveDocument
                    Set Rng = .Range.GoTo(What:=wdGoToPage, Name:=I)
                    Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page")
                    .ExportAsFixedFormat OutputFileName:=xFolder & "\" & _
                        Split(Split(Rng.Paragraphs(3).Range.Text, vbCr)(0), ": ")(1) & _
                        " Reminder 1.pdf", ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, _
                        OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportFromTo, _
                        From:=I, To:=I, Item:=wdExportDocumentContent, IncludeDocProps:=False, _
                        KeepIRM:=False, CreateBookmarks:=wdExportCreateHeadingBookmarks, _
                        DocStructureTags:=True, BitmapMissingFonts:=False, UseISO19005_1:=False
                End With
            Next
        End If
        Exit Sub
    lbl:
        MsgBox "Enter right page number", vbInformation, "ERROR"
    End Sub
    

    如果不是第 3 段,请将 '.Paragraphs(3)' 中的 '3' 更改为适合。

    【讨论】:

    • 尊敬的先生,非常感谢您.. 它在一定程度上起作用,只是一个小帮助.. Name & Reminder 1.pdf 之间需要空格。请求您的帮助。
    • 当然你至少可以做那么多???查看更新的答案。
    • 这现在不起作用..我的要求是将多个页面保存为具有特定名称的 PDF ..但是您的代码仅保存 1 个文档,其中名称来自第一页(第 3 段)和格式内容来自未输入的最后一页
    • 显然您更改了其他内容。在我做的地方放一个空格对其余代码没有任何影响。
    • 我明白,但你的初始代码只是没有工作,
    猜你喜欢
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    相关资源
    最近更新 更多