Using VBA - save an open word document with "Save As"

Discussion in 'Microsoft Access' started by nxqviet, Jan 10, 2007.

  1. nxqviet

    nxqviet Guest

    Hi,

    I use a macro in excel to open an new word application, which then will
    open an existing Word document file. Then the excel copy a bunch on
    data into the Word document. All that is done, then I need to save this
    modified Word document file using a new name (which will generate
    automatically). How do I do that? Below is a part of the code I have.

    ================================

    'Generate File Name
    Dim varUserName As Variant
    Dim varDate As Variant
    Dim varFileName As Variant
    varUserName = Environ("UserName")
    varDate = Format(Date, "yyyymmdd")
    varFileName = "varDate & "_" & varUserName

    'Create Word Document
    Dim WRD As New Word.Application
    Dim SOFWR As New Word.Document
    Dim strFile As String
    strFile = "Template.doc"
    Set WRD = New Word.Application

    Set SOFWR = WRD.Documents.Open(Filename:="C:\Document\" & strFile)
    WRD.Visible = True

    ......Copy codes...

    ----> Save as "varFileName" ? <----

    ======================================

    Thanks,

    V_
     
    nxqviet, Jan 10, 2007
    #1
    1. Advertisements

  2. nxqviet

    nxqviet Guest

    Tom,

    Thanks for the code. Instead of setting the default parth, can i save
    the file at a specific netwoork address such as:
    //DriveName/FolderName/...

    Then would this code work?

    appWord.ActiveDocument.SaveAs Filename:="\\DriveName\FolderName\" &
    varFileName & ".doc"

    After saving this file using this name, I want to close the Word
    Application, how do I do that?

    Thanks,

    V_



    Tom Wickerath wrote:
    > Dim strPath As String
    >
    > 'Get the user's default directory and save the document in this folder
    > strPath = appWord.Options.DefaultFilePath(wdDocumentsPath) _
    > & "\" & strCustomer & " " & lstYear & " Profile.doc"
    >
    > appWord.ActiveDocument.SaveAs FileName:=strPath, _
    > AddToRecentFiles:=False
    >
    >
    > Tom Wickerath
    > Microsoft Access MVP
    >
    > http://www.access.qbuilt.com/html/expert_contributors.html
    > http://www.access.qbuilt.com/html/search.html
    > __________________________________________
    >
    > "nxqviet" wrote:
    >
    > > Hi,
    > >
    > > I use a macro in excel to open an new word application, which then will
    > > open an existing Word document file. Then the excel copy a bunch on
    > > data into the Word document. All that is done, then I need to save this
    > > modified Word document file using a new name (which will generate
    > > automatically). How do I do that? Below is a part of the code I have.
    > >
    > > ================================
    > >
    > > 'Generate File Name
    > > Dim varUserName As Variant
    > > Dim varDate As Variant
    > > Dim varFileName As Variant
    > > varUserName = Environ("UserName")
    > > varDate = Format(Date, "yyyymmdd")
    > > varFileName = "varDate & "_" & varUserName
    > >
    > > 'Create Word Document
    > > Dim WRD As New Word.Application
    > > Dim SOFWR As New Word.Document
    > > Dim strFile As String
    > > strFile = "Template.doc"
    > > Set WRD = New Word.Application
    > >
    > > Set SOFWR = WRD.Documents.Open(Filename:="C:\Document\" & strFile)
    > > WRD.Visible = True
    > >
    > > ......Copy codes...
    > >
    > > ----> Save as "varFileName" ? <----
    > >
    > > ======================================
    > >
    > > Thanks,
    > >
    > > V_
    > >
    > >
     
    nxqviet, Jan 10, 2007
    #2
    1. Advertisements

  3. nxqviet

    nxqviet Guest

    Thanks Tom


    Tom Wickerath wrote:
    > Yes, you should be able to specify a default path. Have you tried doing this
    > yet?
    >
    > I recommend downloading the automation help file from Microsoft. You can get
    > a copy here:
    >
    > The Office XP Automation Help file is available for download
    > http://support.microsoft.com/?id=302460
    >
    > Here is information I copied directly from this help file:
    >
    > Closing a Microsoft Word Document
    > Through Automation, it is possible to close the files that you are working
    > with by using the Close method. To destroy the Automation object variable and
    > close the instance of the application, use the Quit method, and set the
    > object variable to the keyword Nothing.
    >
    > When the Automation object variable goes out of scope, the instance of
    > Microsoft Word is unloaded unless the object was created from a previous
    > instance. It is possible to set the object to a static or public variable so
    > it does not lose scope until the application is closed.
    >
    > Sub CloseWordDoc()
    >
    > Dim WordApp As Word.Application
    > Dim WordDoc As Word.Document
    >
    > ' Open an instance of Word.
    > Set WordApp = CreateObject("Word.Application")
    >
    > With WordApp
    > Set WordDoc = .Documents.Open("C:\My Documents\Test.Doc")
    > ' Selects the entire document and makes it bold.
    >
    > With WordDoc
    > .Range.Font.Bold = True
    > 'Closes the Document and saves changes
    > .Close (wdSaveChanges)
    > End With
    >
    > .Quit
    > End With
    >
    > Set WordDoc = Nothing
    > Set WordApp = Nothing
    >
    > End Sub
    >
    >
    >
    > Tom Wickerath
    > Microsoft Access MVP
    >
    > http://www.access.qbuilt.com/html/expert_contributors.html
    > http://www.access.qbuilt.com/html/search.html
    > __________________________________________
    >
    > "nxqviet" wrote:
    >
    > > Tom,
    > >
    > > Thanks for the code. Instead of setting the default parth, can i save
    > > the file at a specific netwoork address such as:
    > > //DriveName/FolderName/...
    > >
    > > Then would this code work?
    > >
    > > appWord.ActiveDocument.SaveAs Filename:="\\DriveName\FolderName\" &
    > > varFileName & ".doc"
    > >
    > > After saving this file using this name, I want to close the Word
    > > Application, how do I do that?
    > >
    > > Thanks,
    > >
    > > V_
     
    nxqviet, Jan 10, 2007
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    1
    Views:
    330
    Albert D.Kallal
    Oct 8, 2005
  2. Guest
    Replies:
    5
    Views:
    291
    Arvin Meyer [MVP]
    Feb 23, 2010
  3. nxqviet
    Replies:
    0
    Views:
    447
    nxqviet
    Jan 10, 2007
  4. Replies:
    1
    Views:
    936
    Allen Browne
    Jun 16, 2007
  5. Rasta
    Replies:
    0
    Views:
    495
    Rasta
    Dec 12, 2007
Loading...

Share This Page