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

N

nxqviet

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_
 
N

nxqviet

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_
 
N

nxqviet

Thanks Tom


Tom said:
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
__________________________________________
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top