Link to home
Start Free TrialLog in
Avatar of TeDeSm
TeDeSm

asked on

How to create a Word document using current Word instance, then close it.

I have a project that creates a number of Word documents, one after the other. The first one document is to open Word and the remainder will use the same Word instance. After each document is created, it is to be printed then closed before another document is created.

How would I handle the Word application when creating the documents and then disposing of the document object without any 'save to normal.dot' dialogues?
Avatar of PandaPants
PandaPants
Flag of United States of America image

You should be able to close each document with the parameter wdDoNotSave. If that gives you trouble, try setting the Saved property of the document to True, then closing it.
You should just go into Word and tell it not to prompt you to save normal.dot.

Tools, Options, Save... uncheck "Prompt to save change to normal"
Avatar of Nasir Razzaq
>without any 'save to normal.dot' dialogues?

Is that all you need help with?
Avatar of TeDeSm
TeDeSm

ASKER

The option to not save to normal.dotm is unchecked.

However, I think the problem I had is with the code that creates the documents. I have amended it to include a Try/Catch as below. However I have made an assumption that the only exception thrown would be if Word was already open. In VBA a specific error 429 would be thrown.


Try
  oWord = GetObject(, "Word.Application")
Catch ex As Exception
' Word is not already open, create new Word object
  oWord = CreateObject("Word.Application")
  Err.Clear()
End Try

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TeDeSm

ASKER

Still had to use Try/Catch to handle when Word was not open, however it does give an exception code.
Try
oWord = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application")
Catch
oWord = New Word.Application
End Try