none
How can I save a Word document using the contents of a textbox as the file name . . . RRS feed

  • Question

  • I have a merged document.  Each record is two pages.  I run the below script and it separates the larger file in to separate files (a record) and I can save them but I need to insert into the file name the value of a text box in the document.

    I've tried several different things and can't figure out how to put the textbox value in the filename.  I'm new to VB . . .
    Can someone please help!

    Lisa

    Sub BreakOnSection()


       ' Used to set criteria for moving through the document by section.
       Application.Browser.Target = wdBrowseSection

    'Dim filename As Field
    'Dim termdate As Field

        
        'A mailmerge document ends with a section break next page.
       'Subtracting one from the section count stop error message.
       For i = 1 To ((ActiveDocument.Sections.Count) - 1)
       
          'Select and copy the section text to the clipboard
          ActiveDocument.Bookmarks("\Section").Range.Copy

          'Create a new document to paste text from clipboard.
          Documents.Add
          Selection.Paste
           
    '  filename = ActiveDocument. '("MEDID" & "TERMDATE").Name
     
     ' termdate = ActiveDocument.FormFields("TERMDATE").Range
           
           ' Removes the break that is copied at the end of the section, if any.
          Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
          Selection.Delete Unit:=wdCharacter, Count:=1


         ChangeFileOpenDirectory "\\vcuhshmo\groups\Enrollment\PCP Notification Letters\Specialist Termination\Test letters"
          docnum = docnum + 1
         ActiveDocument.SaveAs filename:="medid" & "termdate" & ".docx"
         ActiveDocument.Close
          ' Move the selection to the next section in the document
         Application.Browser.Next
       
       Next i
         
       ActiveDocument.Close savechanges:=wdDoNotSaveChanges
    End Sub

    • Moved by KareninstructorMVP Sunday, July 12, 2015 2:58 PM Moved from Windows Windows data forum
    Friday, July 10, 2015 4:19 PM

Answers

  • Integrating the code to work with your document's footers using your existing code would require a significant re-write, especially if you want the header/footer information replicated in the output documents. With the code in the link I posted, though, it would be fairly simple, as indicated in my previous post. Might I suggest you use that? If you choose to do so and your document always has one Section per record, you could change:
    j = InputBox("How many Section breaks are there per record?", "Split By Sections", 1)
    to:
    j = 1
    to eliminate the prompt.


    Cheers
    Paul Edstein
    [MS MVP - Word]



    Monday, July 13, 2015 12:45 PM

All replies

  • Hello,

    I am moving your question to the Word for Developer's forum from which you will get better responses that you can work with in a VB.NET project.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.

    Sunday, July 12, 2015 2:57 PM
  • Your code refers to a formfield named "TERMDATE", but text & dropdown formfields don't survive a mailmerge.

    IMHO, you'd do better to automate the whole merge so that the individual documents are produced on the fly. If that's not possible, you'll need to explicitly identify the range containing whatever the formfield named "TERMDATE" contained.

    For code to implement either of the above processes, see 'Send Mailmerge Output to Individual Files' and 'Split Merged Output to Separate Documents' in the Mailmerge Tips and Tricks threads at:
    http://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
    or:
    http://windowssecrets.com/forums/showthread.php/163017-Word-Mailmerge-Tips-amp-Tricks


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, July 13, 2015 12:21 AM
  • Hi Paul,

    The part where the merge letters are being placed in separate files and saved is working like a champ.  It's just when I try to use the text in the footer as a filename that things don't work.

    Monday, July 13, 2015 12:04 PM
  • What code are you using? The code you posted doesn't reference the footer. If you're using the code from the 'Split Merged Output to Separate Documents' example in the link I posted, you'd need to change:
    Set Rng = .Range.Paragraphs(1).Range
    to something along the lines of:
    Set Rng = .Footers(wdHeaderFooterPrimary).Range.Paragraphs(1).Range


    Cheers
    Paul Edstein
    [MS MVP - Word]


    Monday, July 13, 2015 12:16 PM
  • That's what I need help with.  The code is my feeble attempt.  I am so new to this.
    Monday, July 13, 2015 12:28 PM
  • Integrating the code to work with your document's footers using your existing code would require a significant re-write, especially if you want the header/footer information replicated in the output documents. With the code in the link I posted, though, it would be fairly simple, as indicated in my previous post. Might I suggest you use that? If you choose to do so and your document always has one Section per record, you could change:
    j = InputBox("How many Section breaks are there per record?", "Split By Sections", 1)
    to:
    j = 1
    to eliminate the prompt.


    Cheers
    Paul Edstein
    [MS MVP - Word]



    Monday, July 13, 2015 12:45 PM