How to take control of an already running instance of Word

H

happysacks

Hi all,

I'm need to use word to update a document as part of a vb6 program. If the
'winword.exe' process is already present I want to use it instead of creating
a new word process by using the 'new Word.Application' statement.

Any ideas?

Many thanks,

Andy Sweetman
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?aGFwcHlzYWNrcw==?=,
I'm need to use word to update a document as part of a vb6 program. If the
'winword.exe' process is already present I want to use it instead of creating
a new word process by using the 'new Word.Application' statement.
You need the GetObject function. It's part of VB, so you can look it up in the
Help. Roughly:

Dim wdapp as Word.Application 'or object

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

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
J

Jezebel

Or

on error resume next
set wdapp = word.application
on error goto 0

if wdapp is nothing then 'Word not running
set wdapp = new word.applicaiton
end if
 
C

Cindy M -WordMVP-

Hi Jezebel,
on error resume next
set wdapp = word.application
on error goto 0

if wdapp is nothing then 'Word not running
set wdapp = new word.applicaiton
end if
Interesting. I've never seen this variation, before. So of
course, I had to test it immediately :) And it works! Hah!
Learn something new every day. Thanks!

Cindy Meister
 
J

Jezebel

There's a subtle error you can get with using CreateObject in conjunction
with early binding. Dim x as Word.Application creates an object according to
the Word library registered for your project; but set x = CreateObject() or
GetObject() instantiates the object according to the library registered in
the registry. These are not necessarily the same. Not sure that it ever
matters with Word, but it certainly does with some libraries.
 
C

Cindy M -WordMVP-

Thanks again, Jezebel :)

Would you happen to have a URL where I can read up on this?
There's a subtle error you can get with using CreateObject in conjunction
with early binding. Dim x as Word.Application creates an object according to
the Word library registered for your project; but set x = CreateObject() or
GetObject() instantiates the object according to the library registered in
the registry. These are not necessarily the same. Not sure that it ever
matters with Word, but it certainly does with some libraries.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
S

sswater shi

:), I have a piece of code in VC++, for reference:

//
// Attach or Create Word Application instance
//
CComPtr <_Application> AttachRunningWord()
{
HRESULT hr;
CLSID clsid;

hr = ::CLSIDFromProgID(L"Word.Application", &clsid);
if(FAILED(hr))
{
return NULL;
}

IUnknown * pUnknown = NULL;

hr = ::GetActiveObject(clsid, NULL, &pUnknown);
if( !FAILED(hr) )
{
return CComQIPtr <_Application> ( pUnknown );
}
else
{
COleDispatchDriver ddrv;
ddrv.m_bAutoRelease = FALSE;

ddrv.CreateDispatch(clsid);

return CComQIPtr <_Application> (ddrv.m_lpDispatch);
}
}
 
S

sswater shi

Private Sub Command1_Click()

Dim wdApp As Word.Application
On Error Resume Next

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

If Not wdApp Is Nothing Then

MsgBox "Word is running. Quiting..."
wdApp.Quit

Else

MsgBox "Word is not running. Opening..."

Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True

End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top