Discussion Locked

Access XP/Word XP VBA Automation Problem

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have an Access XP database that uses VBA to create a new Word Application, open a blank Word document and write letters to customers using information in variables passed as arguments to a procedure called PrintToWord().

If winword.exe is running in the background when Word is closed, e.g. on the Processes tab in Task Manager, The program throws up an error box with a title of ?Run Time Error 91? and a message of ?object variable or with block variable not set?. On pressing debug, it highlights the first line of code in the PrintToWord() procedure ?Selection.ParagraphFormat.Alignment = wdAlignParagraphRight?

This then leaves a lock on the actual word document, which sits on our servers? shared mapped drive. The only way to release it is to go into computer management on the server and delete the file lock.

On the other hand if a word document is open, even if its minimised in the background, it receives all of the printed letters instead of the one opened by the program.

If word is closed and I close down winword.exe in task manager, my program works perfectly.

Here is some skeleton code from my program, showing the creation and killing of my word application (which is invisible so the user never sees it) and the call of the procedure, which writes the letters.

Any help with this matter will be very much appreciated.

Ian

Private Sub cmdCreateMailmerge_Click()

Dim MyWordApp As New Word.Application
Dim MyDoc As Word.Document

Set MyDoc = MyWordApp.Documents.Open("H:\Customer Services\Recall\Recall.doc")
MyDoc.Activate
.
.
Do Until RS.EOF
.
.
Call PrintToWord(AccountNumber, Orders, BatchNumber, Product, MyDoc, ExtraText, False, DelAddress)
.
Loop
.
MyDoc.Close SaveChanges:=wdSaveChanges, OriginalFormat:=wdWordDocument, RouteDocument:=False

MyWordApp.Quit
Set MyDoc = Nothing
Set MyWordApp = Nothing
RS.Close
DoCmd.Quit
End Sub

4 replies

by steve

I think the problem may be in the scope of your variables. If you are calling a function or sub that?s out side the local scope of the calling procedure (button click) then you will get that error. Try declaring the variables in the general declaration area of the form and use Option Explicit at the top

This discussion is closed to new replies.