简体   繁体   English

MS Word VBA:将 Word Doc 保存为每页的前 2 个单词

[英]MS Word VBA: Saving a Word Doc as the First 2 Words on Every Page

I used a mail merge from excel to pull in 400 pages of labels into 1 word document.我使用来自 excel 的邮件合并将 400 页标签拉入 1 个 word 文档。 Each page has the same label 6 times in a 2x3 format with each page being unique.每个页面在 2x3 格式中具有相同的 label 6 次,每个页面都是唯一的。 I currently have a VBA code to split up each page and save it with the correct margins needed.我目前有一个 VBA 代码来拆分每个页面并以所需的正确边距保存它。 It then saves the document as the document name + what page number it is.然后它将文档保存为文档名称+它的页码。

However, I would like the document name for each label as the first 2 words on the page it is on.但是,我希望每个 label 的文档名称作为它所在页面的前 2 个单词。

For example, if the label looks like the below, I would want the document to save as 'Chipole Burrito.docx'例如,如果 label 如下所示,我希望将文档另存为“Chipole Burrito.docx”

Chipotle Burrito墨西哥卷饼

  • steak (double)牛排(双份)
  • white rice白米

Below is the code I used to save the document with the page number.下面是我用来保存带有页码的文档的代码。

docSingle.Range.Find.Execute Findtext:="^m", ReplaceWith:=""

strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc")

docSingle.SaveAs strNewFileName

This should get you the first words, basically accessing the Words object and returning the 1st and 2nd item:这应该让您获得第一个单词,基本上访问单词 object 并返回第一个和第二个项目:

Private Sub GetFirstTwoWords()
    With ActiveDocument
        Debug.Print Trim$(.Range(.Words(1).Start, .Words(2).End).Text)
    End With
End Sub

Or you can get the first paragraph text if it's possible to have label with more/less than 2 words:或者,如果 label 的字数多于/少于 2 个,您可以获得第一段文字:

Private Sub GetFirstParagraphText()
    With ActiveDocument
        Debug.Print Trim$(.Paragraphs(1).Range.Text)
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM