none
Using VBA to "Save As" a specific filename

    Question

  • I'm using Access 2007 to open a template Word document and fill a few formfields. I'd like to find a way of having Word "Save As" at some point in the proceedings and have it save the active document as a specific filename in a specific drive and folder. Furthermore, one of the fields I'm filling in the Word template is a unique number (in this case, it relates to an application number) - Would it be possible for me to use this data as the text for the filename?

    Friday, May 27, 2011 10:13 AM

Answers

  • Guessing you didn't catch my response. Not to worry. I sat down and had a think and came up with this:

    Private Sub cmdExport_Click()
    'Print data for current App Number.
    Dim wordApp As Word.Application
    Dim wordDoc As Word.Document
    Set wordApp = New Word.Application
    With wordApp
      .Visible = True
      Set wordDoc = .Documents.Open("C:\Documents and Settings\user\Desktop\Doc1.dotx", , False)
      With wordDoc
        .FormFields("fldAppNumber").Result = Me!AppNum
        .FormFields("fldAgent").Result = Me!Agent
        .Activate
        Dim pStr As String
        Set wordDoc = ActiveDocument
        pStr = Me!AppNum
        wordDoc.SaveAs "C:\SpecificFolder\SpecificSubFolder\" & pStr, wdFormatDocument
       End With
    End With
    End Sub

    ...which does exactly what I want it to do. Thank you for your pointing me in the right direction!

    Dave

    • Marked as answer by DaveTech80 Friday, June 03, 2011 1:01 PM
    Friday, June 03, 2011 1:01 PM

All replies

  • Dave,

    Something like this perhaps:

     

    Dim pStr As String
    Dim oDoc As Word.Document
    Set oDoc = ActiveDocument
    pStr = "123245" 'your unique number
    oDoc.SaveAs "C:\Test" & pStr, wdFormatDocument


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    Friday, May 27, 2011 10:34 AM
  • Hi Greg,

    Many thanks for the response (for the second time, recently! :-)).

    Given the following code:

    Private Sub cmdExport_Click()
    'Print data for current App Number.
    Dim wordApp As Word.Application
    Dim wordDoc As Word.Document
    Set wordApp = New Word.Application
    With wordApp
      .Visible = True
      Set wordDoc = .Documents.Open("C:\Documents and Settings\user\Desktop\Doc1.dotx", , False)
      With wordDoc
        .FormFields("fldAppNumber").Result = Me!AppNum
        .FormFields("fldAgent").Result = Me!Agent
        .Activate
      End With
    End With
    End Sub

    ..and given that for each time I do this, the application number will be different. Firstly, where is best for me to insert the section of code and secondly, how can I get pStr to equal Me!AppNum ?

    Many thanks!

    Dave

    Friday, May 27, 2011 10:49 AM
  • Guessing you didn't catch my response. Not to worry. I sat down and had a think and came up with this:

    Private Sub cmdExport_Click()
    'Print data for current App Number.
    Dim wordApp As Word.Application
    Dim wordDoc As Word.Document
    Set wordApp = New Word.Application
    With wordApp
      .Visible = True
      Set wordDoc = .Documents.Open("C:\Documents and Settings\user\Desktop\Doc1.dotx", , False)
      With wordDoc
        .FormFields("fldAppNumber").Result = Me!AppNum
        .FormFields("fldAgent").Result = Me!Agent
        .Activate
        Dim pStr As String
        Set wordDoc = ActiveDocument
        pStr = Me!AppNum
        wordDoc.SaveAs "C:\SpecificFolder\SpecificSubFolder\" & pStr, wdFormatDocument
       End With
    End With
    End Sub

    ...which does exactly what I want it to do. Thank you for your pointing me in the right direction!

    Dave

    • Marked as answer by DaveTech80 Friday, June 03, 2011 1:01 PM
    Friday, June 03, 2011 1:01 PM