I have an application that is making changes to a word document from within VB.
Now as you can see, if Word wasn't already open when this code is run it loads word, processes the document and then quits Word saving the changes and everything is fine.Code:Dim oWord as object Set oWord = GetObject(sFileName,"Word.Document") <Changes made> If oWord.Application.Documents.Count = 1 Then ' Word wasn't open before I started oWord.Application.Quit -1 'wdSaveChanges Else oWord.Application.Documents(1).Close -1 ' wdSaveChanges End If Set oWord = Nothing
BUT!!!
If Word WAS actually open and had a document in it, then I cannot use the Application.Quit because it will close down everything and not just the document I was modifying. So instead I use the "Application.Documents(1).Close -1" but visual basic gives me the error "Object cannot access this method as the the Document is not in the Active Window" (Well something close to that).
If I go into Word VBA and run this command it works perfectly fine because Word is my active application.
If anyone has any ideas on how to resolve this I would greatly appreciate them. Do I need to somehow make Word "think" that it is the active object (When you access Word from VB it stays behind the scenes unless its already open) or is there something else I need to do?


Reply With Quote