How to determine if a .doc file is already open?

A

Andrew

Hello, friends,

This may not belong here, but I could not find the proper group.

In c#.net 2005, we use office interop to start different .doc files. The
source code are as the follows:

object fileName = fullPathFileName;
object newTemplate = false;
object docType = 0;
object isVisible = true;

if (wordApp != null && wordApp.Documents != null)
{
Microsoft.Office.Interop.Word.Document doc =
wordApp.Documents.Add(ref fileName, ref newTemplate, ref docType, ref
isVisible);
wordApp.Visible = true;
}

The above worked for the first document opening without problem.

What we want to do is: We need to check if the next given fullFileName is
already open. If yes, we just bring it to front, rather than to open a
duplicate one. Otherwise, open it using above code. The source code for this
checking are like the follows:

if (wordApp != null)
{
foreach (Microsoft.Office.Interop.Word.Document doc in
wordApp.Documents)
{
if (String.Compare(doc.FullName, fullPathFileName,
true) == 0)
{
doc.Activate();
return;
}
}
}

However, we found the doc.FullName had the value Document1 for the first
open .doc file, therefore, failed the checking, and always caused a
previously given fullFileName being opened again.

Any ideas, reference paper or snippet? Thanks a lot!
 
F

fumei via OfficeKB.com

Or check through the Documents collection with a Document object. You could
use a Boolean variable to set as true if the document is open, or you could
bring your code into the If statement. Frankly, using the boolean may be
better for debugging purposes.

Dim CheckDoc As Word.Document
Dim YesOpen As Boolean

For Each CheckDoc In wrdApp.Documents
If CheckDoc.Name = "whatever.doc" Then
YesOpen = True
End If
Next

If YesOpen = False Then openfile
Could this be of any help?

WD2000: VBA Function to Check Whether File or Document Is Open
http://support.microsoft.com/kb/209189/en-us

Cheers,
AL
Hello, friends,
[quoted text clipped - 42 lines]
Any ideas, reference paper or snippet? Thanks a lot!
 

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