View Full Version : How to judge if a Word documnet is opened with automation?


Susan Kong
02-27-2006, 08:00 AM
Hi there,

I'm using VC++ to develop Word automation application. Before I opened a
document, I'd like to know if it's already opened in Word so that I know to
activate it or open it.

Any hint?

Thanks.

Susan

Cindy M -WordMVP-
02-27-2006, 06:25 PM
Hi Susan,

> I'm using VC++ to develop Word automation application. Before I opened a
> document, I'd like to know if it's already opened in Word so that I know to
> activate it or open it.
>
Well... In the VB world we generally use the Open command (as for
reading/writing to text files, not the Open method in the Word Object model).
If that returns an error (the file can't be opened), then the file is locked.

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 :-)

Susan Kong
02-28-2006, 01:37 AM
Here are my code:

_Application oWordApp;
if (!oWordApp.CreateDispatch(_T("Word.Application")))
{
AfxMessageBox(_T("CreateDispatch failed."), MB_OK |
MB_SETFOREGROUND);
return;
}

oDocs = oWordApp.GetDocuments();

if (strMyFilePath is already opened in Word) // how to implement this
statement?
oDoc.Activate();
else
}
oDoc.AttachDispatch(oDocs.Open(COleVariant(strMyFilePath),
vFalse, // Confirm Conversion.
vOpt, // ReadOnly.
vOpt, // AddToRecentFiles.
vOpt, // PasswordDocument.
vOpt, // PasswordTemplate.
vOpt, // Revert.
vOpt, // WritePasswordDocument.
vOpt, // WritePasswordTemplate.
vOpt,// Format. // Last argument for Word 97
vOpt, // Encoding // New for Word 2000/2002
vOpt) // Visible
);
oDoc.Activate();
}

I have no idea how to implement this line:
if (strMyFilePath is already opened in Word) // how to implement this
statement?

Thanks a lot.

Susan
"Cindy M -WordMVP-" <C.Meister-C@hispeed.ch> wrote in message
news:VA.0000bac7.003677f8@speedy...
> Hi Susan,
>
> > I'm using VC++ to develop Word automation application. Before I opened a
> > document, I'd like to know if it's already opened in Word so that I know
to
> > activate it or open it.
> >
> Well... In the VB world we generally use the Open command (as for
> reading/writing to text files, not the Open method in the Word Object
model).
> If that returns an error (the file can't be opened), then the file is
locked.
>
> 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 :-)
>

Jonathan West
02-28-2006, 11:31 AM
Hi Susan,

This is a VBA example.

How to check if a file has already been opened by another user
http://www.word.mvps.org/FAQs/MacrosVBA/CheckIfFileOpen.htm

I don't know enough C++ to be able to translate it for you, but hopefully
this may help.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org

"Susan Kong" <susan@365rainbow.com> wrote in message
news:eDSzrbAPGHA.668@TK2MSFTNGP11.phx.gbl...
> Here are my code:
>
> _Application oWordApp;
> if (!oWordApp.CreateDispatch(_T("Word.Application")))
> {
> AfxMessageBox(_T("CreateDispatch failed."), MB_OK |
> MB_SETFOREGROUND);
> return;
> }
>
> oDocs = oWordApp.GetDocuments();
>
> if (strMyFilePath is already opened in Word) // how to implement this
> statement?
> oDoc.Activate();
> else
> }
> oDoc.AttachDispatch(oDocs.Open(COleVariant(strMyFilePath),
> vFalse, // Confirm Conversion.
> vOpt, // ReadOnly.
> vOpt, // AddToRecentFiles.
> vOpt, // PasswordDocument.
> vOpt, // PasswordTemplate.
> vOpt, // Revert.
> vOpt, // WritePasswordDocument.
> vOpt, // WritePasswordTemplate.
> vOpt,// Format. // Last argument for Word 97
> vOpt, // Encoding // New for Word 2000/2002
> vOpt) // Visible
> );
> oDoc.Activate();
> }
>
> I have no idea how to implement this line:
> if (strMyFilePath is already opened in Word) // how to implement this
> statement?
>
> Thanks a lot.
>
> Susan
> "Cindy M -WordMVP-" <C.Meister-C@hispeed.ch> wrote in message
> news:VA.0000bac7.003677f8@speedy...
>> Hi Susan,
>>
>> > I'm using VC++ to develop Word automation application. Before I opened
>> > a
>> > document, I'd like to know if it's already opened in Word so that I
>> > know
> to
>> > activate it or open it.
>> >
>> Well... In the VB world we generally use the Open command (as for
>> reading/writing to text files, not the Open method in the Word Object
> model).
>> If that returns an error (the file can't be opened), then the file is
> locked.
>>
>> 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 :-)
>>
>
>