Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > Excel VBA GetObject(,"Word.Application")


Reply
 
Thread Tools Display Modes
  #1  
Old 06-30-2010, 02:27 PM
Josh Hazel Josh Hazel is offline
Senior Contributor
 
Join Date: May 2008
Posts: 805
Default Excel VBA GetObject(,"Word.Application")


If using:
Code:
Set wdApp = GetObject(, "Word.Application")
To set an object reference to an already open word app, how do you control which word app it grabs hold of? In some instances there are two processes (word apps, containers) running at the same time. This should obviously grab the first, but what if I want the second one?
__________________
Josh

If Google = NoHelp Then PostHere = True
Reply With Quote
  #2  
Old 06-30-2010, 06:34 PM
nyeoman nyeoman is offline
Freshman
 
Join Date: Sep 2005
Posts: 40
Default

Code:
 Set wdApp = GetObject("C:\FilePath\FileName.doc")
Reply With Quote
  #3  
Old 07-01-2010, 12:36 AM
Josh Hazel Josh Hazel is offline
Senior Contributor
 
Join Date: May 2008
Posts: 805
Default

It seems this Word document is being created on the fly, so i dont think it has a path, unless the program (whos code i cant access) that is being used to create the word document is saving it somewhere
__________________
Josh

If Google = NoHelp Then PostHere = True
Reply With Quote
  #4  
Old 07-01-2010, 08:53 PM
nyeoman nyeoman is offline
Freshman
 
Join Date: Sep 2005
Posts: 40
Default

Ok, if you know the document name you could use the name of the open item minus any path.. for example:
Code:
oWdApp = GetObject("Document1")
..
If you have multiple documents open and do not know what the names are, you would have to perform some type of check to verify it's the document you are looking for.. but something like this would work:
Code:
Set oWdApp = GetObject(, "Word.Application")
Do Until oWdApp.Documents.Count = 0 
oWdApp.Documents(1).Close
Loop
That would iterate through the open documents and close them all. You could do something like a For Each.. depends what u are doing with them.

Hope this helps.
Reply With Quote
  #5  
Old 07-05-2010, 01:46 PM
Josh Hazel Josh Hazel is offline
Senior Contributor
 
Join Date: May 2008
Posts: 805
Default

Okay that information would be helpful I think. Im not sure but I think part of the reason I was not able to get working what I needed is that the document was pw protected so as soon as i added code to unprotect it, i was able to withdraw data from the formfields. Either that or maybe i was typing something wrong =p
__________________
Josh

If Google = NoHelp Then PostHere = True
Reply With Quote
  #6  
Old 07-05-2010, 02:47 PM
Bob Phillips's Avatar
Bob Phillips Bob Phillips is offline
Contributor
 
Join Date: Jul 2009
Posts: 506
Default

Quote:
Originally Posted by Josh Hazel View Post
If using:
Code:
Set wdApp = GetObject(, "Word.Application")
To set an object reference to an already open word app, how do you control which word app it grabs hold of? In some instances there are two processes (word apps, containers) running at the same time. This should obviously grab the first, but what if I want the second one?
You don't, the system manages it.
Reply With Quote
  #7  
Old 07-05-2010, 03:30 PM
Josh Hazel Josh Hazel is offline
Senior Contributor
 
Join Date: May 2008
Posts: 805
Default

That was part of our discussion. If you check processes open, it seems as though I sometimes have 2 x WinWord.exe processes running. Which means if it grabs the first instance automatically, its going to possibly grab the wrong instance. If I am incorrect please elaborate.
__________________
Josh

If Google = NoHelp Then PostHere = True
Reply With Quote
  #8  
Old 07-16-2010, 07:06 PM
Josh Hazel Josh Hazel is offline
Senior Contributor
 
Join Date: May 2008
Posts: 805
Default

So, I sorta figured a work around since I wasnt able to specificy what instance of the Word App to get. First of all, I found out that the WinWord.exe that it was grabbing, was not even Visible meaning i could only close it through task manager.

I have yet to determine if it was my prog (bad code) causing this or another prog. Anyway, to resolve this I just:

Code:
'Set application instance
Set wdApp = GetObject(, "Word.Application")

'Check if there is an extra app process running, if is then close it
If wdApp.Visible = False Then
    wdApp.Quit False
    Set wdApp = Nothing
    Set wdApp = GetObject(, "Word.Application")
End If

simply just quite the app thats not visible, then regrab the next app - works for me so far
__________________
Josh

If Google = NoHelp Then PostHere = True
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->