0

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.

この最初の単語をページから選択、抽出、削除する方法がどこにも見つかりませんでした。

これが私の「分割マクロ」です。現在、増分された 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
4

1 に答える 1

0

以下の関数を使用すると、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

これが役立つことを願っています。

于 2012-02-15T15:52:40.610 に答える