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
Cindy M -WordMVP- - 10 May 2006 11:01 GMT
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 :-)
Jezebel - 10 May 2006 11:23 GMT
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
> Hi =?Utf-8?B?aGFwcHlzYWNrcw==?=,
>
[quoted text clipped - 20 lines]
> reply
> in the newsgroup and not by e-mail :-)
Cindy M -WordMVP- - 10 May 2006 15:28 GMT
Hi Jezebel,
> on error resume next
> set wdapp = word.application
[quoted text clipped - 3 lines]
> 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
Jezebel - 10 May 2006 23:28 GMT
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.
> Hi Jezebel,
>
[quoted text clipped - 11 lines]
>
> Cindy Meister
Cindy M -WordMVP- - 11 May 2006 15:39 GMT
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 :-)
Jezebel - 13 May 2006 01:05 GMT
Don't have one to hand. Try Googling for VB +"binary compatability" or some
such.
> Thanks again, Jezebel :-)
>
[quoted text clipped - 18 lines]
> reply
> in the newsgroup and not by e-mail :-)
sswater shi - 15 May 2006 03:29 GMT
:), 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);
}
}
> Hi all,
>
[quoted text clipped - 7 lines]
>
> Andy Sweetman
Jezebel - 15 May 2006 03:42 GMT
How do you suggest that be used within a VB6 program?
> :), I have a piece of code in VC++, for reference:
>
[quoted text clipped - 43 lines]
>>
>> Andy Sweetman
sswater shi - 15 May 2006 05:49 GMT
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
> How do you suggest that be used within a VB6 program?
Jezebel - 15 May 2006 06:39 GMT
Yes, we've already been there. I was asking about the C++ sample.
> Private Sub Command1_Click()
>
[quoted text clipped - 20 lines]
>
>> How do you suggest that be used within a VB6 program?