Problem saving the Mail Merge destination file from a script

L

Laurie C

I am trying to create a VB script that runs a mail merge and then saves the
destination file. However, the script crashes every time with a dialog box
that says "Word cannot complete the save due to a file permission error."

I suspect some kind of file lock problem, as this is my own computer and I
am the administrator (the only user, period), so I wouldn't think that file
permissions could really be the problem.

Sometimes I get a similar error when trying to save the destination file
when I run the merge manually and try to save the file. (The wording is a
little different, but same result - sorry I didn't write it down.)

My script is below. Any help would be appreciated.

Laurie C


Option Explicit
Dim fs, rpt, objWord

Set fs = CreateObject("Scripting.FileSystemObject")
Set objWord = CreateObject("Word.Application")

objWord.documents.open("F1.DOT")

objWord.ActiveDocument.MailMerge.OpenDataSource("025139.doc")

With objWord.ActiveDocument.MailMerge
.Destination = 0 'wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = 1 'wdDefaultFirstRecord
.LastRecord = -16 'wdDefaultLastRecord
End With
.Execute (false) ' (pause = false)
End With

Call objWord.ActiveDocument.SaveAs ("testtest.DOC")
objWord.ActiveDocument.Password = "password"
Call objWord.ActiveDocument.Save()
objWord.ActiveDocument.Close()
objWord.ActiveDocument.Close()

objWord.Quit
set objWord = Nothing
Set fs = Nothing
 
D

Doug Robbins - Word MVP

I don't think that you need the "Call" word. It would also be a good idea
to provide the path to the folder into which you want the file saved.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
L

Laurie C

Hi, Doug,

Thank you very much for your answer. However, I took out the "call" but I
still get the same error. In my actual script, I do have the complete path
name along with the file name, but I took it out for my post just so it
wouldn't be so long.

The path is the same for the main file and the source file, which the script
is finding with no problem, etc., so it wouldn't be a path problem. It seems
to be a problem that Word has the the file locked open somehow so it can't be
saved or renamed.

(I have checked taskmgr and I have to kill the winword process that is
running after the script crashes.)

Does anyone know how to prevent this mail merge destination file from
getting locked?

Laurie C
 
P

Peter Jamieson

I can't see why either of the following would fix the problem, but
perhaps worth trying anyway...
a. keep the references to the Mail Merge Main Document (MMMD) and
output document separate and
b. use add to add a .doc rather than open to open a .dot

Also, can we assume that you have followed the instructions in

http://support.microsoft.com/kb/825765 ?

'----------------------------------
Option Explicit
Dim fs, rpt, objWord, objMMMD

Set fs = CreateObject("Scripting.FileSystemObject")
Set objWord = CreateObject("Word.Application")

Set objMMMD = objWord.documents.add("F1.DOT")

objMMMD.MailMerge.OpenDataSource("025139.doc")
With objMMMD.MailMerge
.Destination = 0 'wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = 1 'wdDefaultFirstRecord
.LastRecord = -16 'wdDefaultLastRecord
End With
.Execute (false) ' (pause = false)
End With

objWord.ActiveDocument.SaveAs "testtest.DOC"
objWord.ActiveDocument.Password = "password"
objWord.ActiveDocument.Save()
objWord.ActiveDocument.Close()
objMMMD.Close()

objWord.Quit
set objWord = Nothing
Set fs = Nothing
'----------------------------------


Peter Jamieson

http://tips.pjmsn.me.uk
 

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