We still have a VB6 application in use. In this application, documents are created in Word for Windows by using an OLE link (Office Automation). The connection is set up with the following code.
2 global variables are declared:
Global objWdApp As WORD.Application
Global objWdDoc As WORD.Document
Then the Word object objWdDoc is created for the document that has to be edited:
Set objWdApp = CreateObject("Word.Application")
Set objWdDoc = objWdApp.Documents.Open("C:\test\test.doc")
, and the document is activated and made invisible:
objWdApp.Documents("test.doc").Windows(1).Visible = False
The document can then be generated , for example using code like this:
objWdApp.Selection.Font.Bold = true
Set myTable = ActiveDocument.Tables.Add(Range:=myRange,…………….
Selection.Borders(wdBorderRight) = true
and so on. This works fine.
However, there is a problem if there are more Word documents open at the time the OLE link is initialized and opened. Using the user interface, the user is able to put the focus on to one of the other Word documents. The OLE commands will then be sent to the document that has the focus, which may not be the one intended. We have solved this by making Word completely invisible so that all other Word documents disappear from view:
objWdApp.Visible = False
This solves the problem, even though it might not be very elegant.
Later, however, we also received a message from one of our customers saying that the same problem also occurs when an email is created in Outlook using a Word module for writing emails. In that situation the focus will be put on the e-mail (it is seen as a Word document) and the OLE commands will be sent to this email! We can make Outlook invisible too, but I am looking for a better solution for this behavior.
Does anyone have an idea how we can manage this problem?
Any help is greatly appreciated!