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?
TeDeSmAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PandaPantsCommented:
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.
0
Joanne M. OrzechManager, Document Services CenterCommented:
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"
0
CodeCruiserCommented:
>without any 'save to normal.dot' dialogues?

Is that all you need help with?
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

TeDeSmAuthor Commented:
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

0
CodeCruiserCommented:
Try this

oWord = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application")

If IsNothing(oWord) Then
   oWord = New Word.Application
End If



This will get the reference to active object if one is open or open one if there is none open.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TeDeSmAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.