none
How to extract / delete first word of each page? RRS feed

  • Question

  • Hello everyone,

    First of all I'm a beginner in VBA so please be kind. Let me explain the situation.

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

    Then I did (by looking on the net) a macro to split the result file in several others, 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.

    Exemple : if I have Microsoft as a customer I would like to have a file named Facture_Microsoft.doc for it.

    But I'm totally stuck on how to do that. I found nowhere how to select, extract and then delete this first word from my page.
    Any help would be most welcome, here is my "splitting macro", which currently names the files just with an incremented ID :

    VBA:
    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

    Thanks in advance. By the way I'm using Office 2007.
    js
    • Edited by PeaceMakah Wednesday, February 15, 2012 2:55 PM
    Wednesday, February 15, 2012 2:40 PM

Answers

  • Hi js,

    You might try to use the Selection.Text and see if that resolves in saving the Company Name instead of the DocNum.

    In your case the DocNum is actualy not needed anymore, as you already don't want to use it.

    So the code would look something like this:

    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 
    ' Instead of DocNum use the Selection.Text
    ActiveDocument.SaveAs FileName:="Facture_" & Selection.Text & ".doc" 
    ActiveDocument.Close 
    Application.Browser.Next 
    Next i 
    ActiveDocument.Close savechanges:=wdDoNotSaveChanges 
    
    End Sub

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Wednesday, February 15, 2012 11:21 PM
    Moderator