PC Review


Reply
Thread Tools Rate Thread

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

 
 
nxqviet
Guest
Posts: n/a
 
      10th Jan 2007
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_

 
Reply With Quote
 
 
 
 
nxqviet
Guest
Posts: n/a
 
      10th Jan 2007
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/ex...tributors.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_
> >
> >


 
Reply With Quote
 
 
 
 
nxqviet
Guest
Posts: n/a
 
      11th Jan 2007
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/ex...tributors.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_


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word 2007 "Save As" closes document, doesn't allow me to save it to a different directory or change name of document. =?iso-8859-1?q?El_Capit=E1n_de_las_Albondigas?= Microsoft Word Document Management 3 27th Jul 2007 03:59 AM
Using VBA - save an open word document with "Save As" nxqviet Microsoft Access 0 10th Jan 2007 05:07 AM
how do I save Word 2003 document as Word for Mac X document? =?Utf-8?B?a19kdWI=?= Microsoft Word Document Management 2 24th Oct 2004 08:05 PM
Re: how save all the open excel or word document is there no save all. Suvodip Moitra Microsoft Powerpoint 0 17th Sep 2004 05:41 AM
Re: how save all the open excel or word document is there no save all. Echo S Microsoft Powerpoint 0 16th Sep 2004 09:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:07 PM.