Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word VBA Automation with a document already open, template conflict

Status
Not open for further replies.

trb5016

Programmer
Nov 23, 2011
2
US
I'm trying to automate word (from Access) using what I think is standard code.

Dim wordApp As Word.Application
Dim wordDoc As Word.Document


Set wordApp = New Word.Application
wordApp.Visible = False

Set wordDoc = wordApp.Documents.Add

wordApp.Visible = True

Set wordApp = Nothing
Set wordDoc = Nothing

This works fine, but if there's another word document open when this is run and then you try to close the vba created document it brings up the message saying the Normal.dot template is in use by another user, and that changes have been made to the global template.

I don't understand how the above code is making changes to the Normal.dot template?

Is there a better method I should be using to automate? (I've also used the late-binding CreateObject and it has the same effect)

I appreciate any help or insights.
 

Sometimes its hard to understand what changes Word is making to the Normal Template - or why. As you know you are not making any consequential changes you could tell Word that the Normal Template has already been saved and it will not then prompt to save it again ..

[tt] wordApp.NormalTemplate.Saved = True[/tt]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
It's funny you answered today, because I actually just came up with a solution. I'm not sure if what you suggested, Tony, works. I think I tried something similar, and it was still giving me trouble (We have norton installed, I've read that could be the issue)

Anyways, what I've done is check to see if word as an application is open, if it is, use that instance instead of creating a new one. My revised code is below:

'Bind word application
If IsWordRunning = True Then
Set wordApp = GetObject(, "Word.Application")
Else
Set wordApp = New Word.Application
End If

Set wordDoc = wordApp.Documents.Add
wordDoc.Activate
wordApp.ActiveWindow.Visible = False

...
...

'show the generated document
wordDoc.Activate
wordApp.ActiveWindow.Visible = True


Public Function IsWordRunning() As Boolean
Dim oApp As Object

On Error Resume Next

Set oApp = GetObject(, "Word.Application")

If Not oApp Is Nothing Then
Set oApp = Nothing
IsWordRunning = True
End If

End Function

This code won't throw that error and I think it's cleaner since it isn't creating another application version of word.

Also, it allows you to hide just the word document you're working on instead of hiding word as an application (it hides a users open word documents if you just used wordApp.visible = false)

Thanks for your reply though!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top