Saving Word file to disk automatically

S

Someone

Hello

My database has a facility to auto-generate a Word document for a selected
record. The OnClick even runs the following module.

Function WordMerge()
Dim objWord As Word.Document
Set objWord = GetObject("C:\Location....\Template_Name.dot",
"Word.Document")
objWord.Application.Visible = True
objWord.MailMerge.OpenDataSource _
Name:="C:\Location....\db_name.mdb", _
LinkToSource:=True, _
Connection:="TABLE tbl_Direction", _
SQLStatement:="SELECT tbl_Name.field_name, etc, etc, etc, etc FROM
[tbl_Name] WHERE [tbl_Name.Check] = True"
objWord.MailMerge.Execute

With Dialogs(wdDialogFileSaveAs)
.Name = "C:\Location....\Word_Name.doc"
.Show
End With

End Function

The With/End With part is me experimenting and I can get the save as
dialogue box to appear and change the file name. However, I want the file
to atomically save using a specified filename without prompting / user
interaction.

How can I do this?

Thanks
M
 
N

Norman Yuan

Since you have already gotten the Word.Document object in hand, you can
simply call Word.Document.SaveAs() method.

You may want to have your function like this:

Function WordMerge(Optional fileName As String="C:\MyDocs\Myfile.doc")

'your code
Dim objWord As Word.Document
Set objWord =
GetObject("C:\Location....\Template_Name.dot","Word.Document")
objWord.Application.Visible = True
objWord.MailMerge.OpenDataSource _
Name:="C:\Location....\db_name.mdb", _
LinkToSource:=True, _
Connection:="TABLE tbl_Direction", _
SQLStatement:="SELECT tbl_Name.field_name, etc, etc, etc, etc FROM
[tbl_Name] WHERE [tbl_Name.Check] = True"
objWord.MailMerge.Execute

'Save doc
objWord.SaveAs fileName
objWord.Close

End Function
 
S

Someone

Hi Norman

Thanks very much for that. This worked.

Despite this, I decided to choose a different approach and use bookmarks
instead, as the new security on importing SQL commands on mail merges, plus
documents being saved with merge fields was not viable, as the resultant
documents needed to be emailed to 3rd parties. I'm happy with the bookmark
approach.

Thanks again for your help.
M
Norman Yuan said:
Since you have already gotten the Word.Document object in hand, you can
simply call Word.Document.SaveAs() method.

You may want to have your function like this:

Function WordMerge(Optional fileName As String="C:\MyDocs\Myfile.doc")

'your code
Dim objWord As Word.Document
Set objWord =
GetObject("C:\Location....\Template_Name.dot","Word.Document")
objWord.Application.Visible = True
objWord.MailMerge.OpenDataSource _
Name:="C:\Location....\db_name.mdb", _
LinkToSource:=True, _
Connection:="TABLE tbl_Direction", _
SQLStatement:="SELECT tbl_Name.field_name, etc, etc, etc, etc FROM
[tbl_Name] WHERE [tbl_Name.Check] = True"
objWord.MailMerge.Execute

'Save doc
objWord.SaveAs fileName
objWord.Close

End Function

Someone said:
Hello

My database has a facility to auto-generate a Word document for a
selected
record. The OnClick even runs the following module.

Function WordMerge()
Dim objWord As Word.Document
Set objWord = GetObject("C:\Location....\Template_Name.dot",
"Word.Document")
objWord.Application.Visible = True
objWord.MailMerge.OpenDataSource _
Name:="C:\Location....\db_name.mdb", _
LinkToSource:=True, _
Connection:="TABLE tbl_Direction", _
SQLStatement:="SELECT tbl_Name.field_name, etc, etc, etc, etc FROM
[tbl_Name] WHERE [tbl_Name.Check] = True"
objWord.MailMerge.Execute

With Dialogs(wdDialogFileSaveAs)
.Name = "C:\Location....\Word_Name.doc"
.Show
End With

End Function

The With/End With part is me experimenting and I can get the save as
dialogue box to appear and change the file name. However, I want the
file
to atomically save using a specified filename without prompting / user
interaction.

How can I do this?

Thanks
M
 

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