none
Use VBA to save word document with a form field as the name RRS feed

  • Question

  • I have form fields within a Word document, and I want to use VBA to choose one of the fields in the document and make it part of a new file name.  So far I’ve only managed to get it to work so the save dialog box displays with today’s date, but don’t know where to go from here to pull the field from within the document.

    ChangeFileOpenDirectory _

            "H:\Appeals\appeals\Test Appeals Project\MuAppealsOutput\" (this is my directory)

           Dim dlgSave As Dialog

        Set dlgSave = Dialogs(wdDialogFileSaveAs)

        With dlgSave

            .Name = Format(Date, "yyyy_mm_dd ")

            .Show

        End With

    I've done it before with Excel, but can't seem to make this work with Word.  Done lots of searching on line and can’t seem to find anything that makes sense.  Any help will be appreciated.  Thank you

    • Moved by Perry-Pan Thursday, February 28, 2019 1:33 AM
    Wednesday, February 27, 2019 9:29 PM

Answers

  • Assuming you want the SaveAs dialogue to show, try something along the lines of:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim StrFldr As String, StrNm As String
    StrFldr = "H:\Appeals\appeals\Test Appeals Project\MuAppealsOutput\"
    With ActiveDocument
      StrNm = .FormFields("Text1").Result
      With Application.Dialogs(wdDialogFileSaveAs)
        .Name = StrFldr & StrNm
        .Format = wdFormatXMLDocument
        .Show
      End With
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Bb's Thursday, February 28, 2019 10:13 PM
    Thursday, February 28, 2019 6:16 AM

All replies

  • Hi,

    As this issue is more related with VBA code, I'll help move this thread to Word for Developer forum to see if anyone else is familiar with this issue.

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.

    Thank you for your understanding and support.

    Regards,

    Perry


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Edited by Perry-Pan Thursday, February 28, 2019 1:34 AM
    Thursday, February 28, 2019 1:33 AM
  • Assuming you want the SaveAs dialogue to show, try something along the lines of:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim StrFldr As String, StrNm As String
    StrFldr = "H:\Appeals\appeals\Test Appeals Project\MuAppealsOutput\"
    With ActiveDocument
      StrNm = .FormFields("Text1").Result
      With Application.Dialogs(wdDialogFileSaveAs)
        .Name = StrFldr & StrNm
        .Format = wdFormatXMLDocument
        .Show
      End With
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Bb's Thursday, February 28, 2019 10:13 PM
    Thursday, February 28, 2019 6:16 AM
  • Thank you, works great!

    Brenda

    Thursday, February 28, 2019 10:10 PM