如何提取/删除每页的第一个单词?

发布于 2025-01-06 09:37:22 字数 929 浏览 1 评论 0原文

我做了一个邮件合并来创建包含客户信息的动态单词页面。

然后我(通过在网上查找)一个宏将结果文件分成几页,每一页都保存为一个文件。

现在我希望为这些文件提供一些包含客户信息的名称。我用谷歌搜索了一下,我认为(唯一的?)方法是在页面的最开始创建一个包含该信息的合并字段,然后使用宏从页面中提取并删除它,将其放入文件名中。

示例:如果我有一个名为 Stackoverflow 的客户,我想要一个名为 Facture_Stackoverflow.doc 的文件。

我找不到如何从我的页面中选择、提取然后删除第一个单词的方法。

这是我的“分割宏”,它当前仅使用递增的 ID 来命名文件:

Sub DecouperDocument()
    Application.Browser.Target = wdBrowsePage

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

        ActiveDocument.Bookmarks("\page").Range.Copy

        Documents.Add
        Selection.Paste

        Selection.TypeBackspace
        ChangeFileOpenDirectory "C:\test\"
        DocNum = DocNum + 1
        ActiveDocument.SaveAs FileName:="Facture_" & DocNum & ".doc"
        ActiveDocument.Close

        Application.Browser.Next
    Next i
    ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

I did a mailmerge to create dynamic word pages with customer information.

Then I did (by looking on the net) a macro to split the result file into several pages, each page being saved as one file.

Now I'm looking to give those files some names containing customer info. I googled that and I think the (only?) way is to create a mergefield with that info, at the very beginning of the page, then extract and delete it from the page with a macro to put it in file names.

Example: If I have a customer named Stackoverflow I would like to have a file named Facture_Stackoverflow.doc.

I found nowhere how to select, extract and then delete this first word from my page.

Here is my "splitting macro", which currently names the files just with an incremented ID:

Sub DecouperDocument()
    Application.Browser.Target = wdBrowsePage

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

        ActiveDocument.Bookmarks("\page").Range.Copy

        Documents.Add
        Selection.Paste

        Selection.TypeBackspace
        ChangeFileOpenDirectory "C:\test\"
        DocNum = DocNum + 1
        ActiveDocument.SaveAs FileName:="Facture_" & DocNum & ".doc"
        ActiveDocument.Close

        Application.Browser.Next
    Next i
    ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2025-01-13 09:37:22

下面的函数将使您能够提取 Word 文档的第一个单词(并可选择删除它)。

Public Function GetFirstWord(Optional blnRemove As Boolean = True) As String

    Dim rng As Range
    Dim intCharCount As Integer
    Dim strWord As String

    With ThisDocument
        Set rng = .Characters(1)

        intCharCount = rng.EndOf(wdWord, wdMove)

        With .Range(0, intCharCount - 1)
            strWord = .Text
            If blnRemove Then
                .Delete
            End If
        End With
    End With

    GetFirstWord = strWord

End Function

我希望这有帮助。

The function below will enable you to extract the first word (and optionally remove it) of a Word document.

Public Function GetFirstWord(Optional blnRemove As Boolean = True) As String

    Dim rng As Range
    Dim intCharCount As Integer
    Dim strWord As String

    With ThisDocument
        Set rng = .Characters(1)

        intCharCount = rng.EndOf(wdWord, wdMove)

        With .Range(0, intCharCount - 1)
            strWord = .Text
            If blnRemove Then
                .Delete
            End If
        End With
    End With

    GetFirstWord = strWord

End Function

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文