Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba MS Word将每个页面保存为单独的PDF文档,并在文档中以特定文本命名_Vba_Pdf_Ms Word - Fatal编程技术网

Vba MS Word将每个页面保存为单独的PDF文档,并在文档中以特定文本命名

Vba MS Word将每个页面保存为单独的PDF文档,并在文档中以特定文本命名,vba,pdf,ms-word,Vba,Pdf,Ms Word,我想转换发票数据的Microsoft excel表格并填充邮件合并模板,合并后,我需要拆分每个发票并将其保存为pdf 下面的代码符合我的要求,但将它们保存为1、2、3等。我希望使用的名称是文档上的发票号(每页的前8个字符,不包括页眉) 这就是我的代码现在的样子: Sub BreakOnPage() Selection.HomeKey Unit:=wdStory ' Used to set criteria for moving through the document by

我想转换发票数据的Microsoft excel表格并填充邮件合并模板,合并后,我需要拆分每个发票并将其保存为pdf

下面的代码符合我的要求,但将它们保存为1、2、3等。我希望使用的名称是文档上的发票号(每页的前8个字符,不包括页眉)

这就是我的代码现在的样子:

 Sub BreakOnPage()
     Selection.HomeKey Unit:=wdStory
     ' Used to set criteria for moving through the document by page.
     Application.Browser.Target = wdBrowsePage

     For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")

     'Select and copy the text to the clipboard.
     ActiveDocument.Bookmarks("\page").Range.Copy

     ' Open new document to paste the content of the clipboard into.
     Documents.Add
     Selection.Paste
     ' Removes the break that is copied at the end of the page, if any.
     Selection.TypeBackspace
     Selection.HomeKey Unit:=wdStory
     Selection.EndKey Unit:=wdStory
     Selection.TypeBackspace
     Selection.Delete Unit:=wdWord, Count:=1
     Selection.Delete Unit:=wdCharacter, Count:=1

     Dim strInvoiceNumber As String
     Selection.HomeKey Unit:=wdStory
     With Selection.Find
     .ClearFormatting
     Text:="^#^#^#^#^#^#^#^#"
     .Forward = True
     .MatchWildcards = False
     .Execute
     End With

     ' Defines the DocNum

     strInvoiceNumber = Selection.Text


     ' Exports the document to a pdf file and saves in sequence starting at 1 and closes the word document without saving

     ActiveDocument.ExportAsFixedFormat OutputFileName:= _
     "C:\Users\MLock\Documents\MacrosDocs\" & strInvoiceNumber & ".pdf", ExportFormat:= _
     wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
     wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
     Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
     CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
     BitmapMissingFonts:=True, UseISO19005_1:=False


     ActiveDocument.Close savechanges:=wdDoNotSaveChanges

     ' Move the selection to the next page in the document.
     Application.Browser.Next
     Next i
     ' ActiveDocument.Close savechanges:=wdDoNotSaveChanges
 End Sub

如何在此处设置PDF文档的名称?

因此,您必须在页面上找到发票号并将其分配给一个变量,然后使用该变量替换docnum

查找的两种方法是使用Selection.find,执行查找,然后将变量分配给Selection.Text。这可能是最简单的

您还可以使用正则表达式,捕获反向引用中的前8个字符,然后使用它

如果您需要,我可以澄清其中任何一点,但不确定您的专业水平

下面是一些代码来完成我认为您正在尝试做的事情。我假设Selection.HomeKey Unit:wdStory是指包含发票号的文档

Sub BreakOnPage()
    Dim strInvoiceNumber as String
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Text:="^#^#^#^#^#^#^#^#"
        .Forward = True
        .MatchWildcards = False
        .Execute
    End With
现在将选择您的8位发票号码。将变量strInvoiceNumber指定给Selection.Text,如下所示:

strInvoiceNumber = Selection.Text
现在在ExportAsFixedFormat语句中使用strInvoiceNumber,而不是DocNum


祝你好运。

zero专家,我非常感谢你能提供的任何额外帮助。第一个选择似乎很好。下一步是什么?再次感谢,以Text开头的行:=“^#…给了我一个编译错误。在Text之前需要一个句点,因此它包含在with语句中。