Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to save a word document automacally w/ a different name?

Status
Not open for further replies.

Marclem

Technical User
Aug 5, 2003
96
US
I have been searching and I found out this code:

objWord.ActiveDocument.SaveAs FileName:="C:\MyPath\" & [MyDocName.doc"

It did not work, it is saying [compile error] [Syntax Error]

I need to save each document using a field value[field name = Company] located under my current form.

Private Sub Command143_Click()
Dim objWord As Word.Application
Set objWord = CreateObject("Word.application")
With objWord
.Visible = True
.Documents.Open ("c:\MyMerge.doc")
.ActiveDocument.Bookmarks("First").Select
.Selection.Text = (CStr(Forms![ALL WOOL]![Vendor]))
End With
objWord.ActiveDocument.SaveAs FileName:="C:\" & [MyMerge.doc"
End Sub
 
Replace this:
:="C:\MyPath\" & [MyDocName.doc"
By this:
:="C:\MyPath\" & "MyDocName.doc"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thankyou PHV It worked,

Does anybody know if I can let's say every time I click on this button it would look for a field value under my current form and this value would then be my fieldvalue.doc?
 
I've got some exports that chuck a whole load of info in the saved name.

Initially define a name, strFilename...

strFilename = Environ("UserName") & "_" & Me.Fieldname & Format(Now, "dd_MM_YYYY hh_mm_ss") & ".doc"

Me.Fieldname will return the value of the field on you form. I like to put the date and user name in as well so i've shown that as well.

Substitute it into you code like so...

objWord.ActiveDocument.SaveAs FileName:="C:\MyPath\" & strFilename

Hope this get you moving in the right direction

Regards

Stephen

 
Thankyou stephenk1973

This is what I wrote down, but it highlights the word Enviro and says that: [compile error] [Syntax Error]

strFilename = Environ("UserName") & "_" & Me.Vendor & Format(Now, "dd_MM_YYYY hh_mm_ss") & ".doc"
objWord.ActiveDocument.SaveAs FileName:="C:\" & strFilename
 
Just get rid of it or alternatively sub in CurrentUser()"

strFilename = CurrentUser() &_& Me.Vendor & Format(Now, "dd_MM_YYYY hh_mm_ss") & ".doc"

 
stephenk1973 I changed It so that it would only have the file name but I am getting an error on strFilename stating [compile error] [Syntax Error]

Private Sub Command143_Click()
Dim objWord As Word.Application
Set objWord = CreateObject("Word.application")
With objWord
.Visible = True
.Documents.Open ("c:\MyMerge.doc")
.ActiveDocument.Bookmarks("First").Select
.Selection.Text = (CStr(Forms![ALL WOOL]![Vendor]))
End With
strFilename = Me.Contact.Value & ".doc"
objWord.ActiveDocument.SaveAs FileName:="C:\" & strFilename
End Sub
 
This is my code under the button. Is there anything I am missing, because when I test the code it says [compile error] [Syntax Error] under strFilename


Private Sub Command143_Click()
Dim objWord As Word.Application
Set objWord = CreateObject("Word.application")
With objWord
.Visible = True
.Documents.Open ("c:\MyMerge.doc")
.ActiveDocument.Bookmarks("First").Select
.Selection.Text = (CStr(Forms![ALL WOOL]![Vendor]))
End With
strFilename = Me.Contact.Value & ".doc"
objWord.ActiveDocument.SaveAs FileName:="C:\" & strFilename
End Sub
 
Declare strFilename at with your other declarations.

Dim strfilename as String

I think

Regards

Stephen

 
Hey stephenk1973 I finally got thanks for your time.
 
stephenk1973,

Say I wanted to import an excel spreadsheet into Access via a form right?

Well by your method and naming convention, if I named it userID+date.xls, would your method then allow the database to pull the user id and date and auto-fill that in those appropriate rows of the records it imports?

What I mean is, the data being imported from the .xls is numerical data, call times, etc. Two of the columns in my table used for query reasons are AGENT ID and DATE. Currently, one would have to import their records, go into the table, find the records they just imported and then in each row, type their Agent ID and Date.

By using your method, would that auto-fill just by the naming convention you give the files?
 
I can't think of a direct way of doing what you want. I'm pretty certain the reverse of the above will not work.

There may be a way of braking the task down into more managable chunks.

Pull the username and date out of the title of the file
Importing you data to a temporary table.
Append temporary table to your table defining the username and date values into the query.
Delete temporty table.

I'd think about starting a new thread, there's probably a whole science to importing that i'm not quite clued up on and more advanced user than me may be able to impart better advice.

All the best

Stephen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top