[RESOLVED] Scan For Open Word Documents
Hi All,
I hope one of you may have done this before. I am trying to open a word doc. I have two things that I am trying to check for before I do this. One to make sure that Word is not already open, if it is then use that instance of Word. Second, if Word is open I would like to check the open Word docs to see if any of them are mine. I have the first part figured out but what I can’t figure out is how to loop through the open Word docs to see if any are mine. My code is below:
Code:
public void OpenWordApp()
{
try
{
// If we don't have a word app then try and create one
if (wordApp == null)
{
//Get the reference to Word.Application from the ROT.
wordApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
}
}
// If there is not a Word instance GetActiveObject will throw an exception, set wordApp to null
catch
{
wordApp = null;
}
// If wordApp is still null then we will create a new Word instance
if(wordApp == null)
{
try
{
wordApp = new Word.ApplicationClass();
}
catch
{
wordApp = null;
}
}
}
private bool OpenWordFile(string wordFilePath)
{
// TODO: We need to see if the file has already been opened by another instance of Word, if it has pass back that reference
object filename = wordFilePath;
object confirmConversions = Type.Missing;
object readOnly = Type.Missing;
object addToRecentFiles = Type.Missing;
object passwordDocument = Type.Missing;
object passwordTemplate = Type.Missing;
object revert = Type.Missing;
object writePasswordDocument = Type.Missing;
object writePasswordTemplate = Type.Missing;
object format = Type.Missing;
object encoding = Type.Missing;
object visible = Type.Missing;
object openConflictDocument = Type.Missing;
object openAndRepair = Type.Missing;
object documentDirection = Type.Missing;
object noEncodingDialog = Type.Missing;
if (wordDoc == null)
{
foreach (Word.Document tempWordDoc in wordApp.Documents)
{
if (tempWordDoc.Path == filename.ToString())
{
wordDoc = tempWordDoc;
break;
}
}
if (wordDoc == null)
{
wordDoc = wordApp.Documents.Open(ref filename,
ref confirmConversions, ref readOnly, ref addToRecentFiles,
ref passwordDocument, ref passwordTemplate, ref revert,
ref writePasswordDocument, ref writePasswordTemplate,
ref format, ref encoding, ref visible, ref openConflictDocument,
ref openAndRepair, ref documentDirection, ref noEncodingDialog);
}
}
wordDoc.Activate();
return true;
}
I would have thought this foreach loop would do the trick, but I am getting told that Documents does not exist
Code:
foreach (Word.Document tempWordDoc in wordApp.Documents)
{
if (tempWordDoc.Path == filename.ToString())
{
wordDoc = tempWordDoc;
break;
}
}
Have any of you done this before? Could I have a pointer on how you did it?
Thanks Steve
Re: Scan For Open Word Documents
The issue is that the Documents collection is separate for each application object if the documents are not opened in the same instance of Word. If the documents are all opened in the same app instance, like clicking "new" for each one or such, then they are going to be in that applications documents collection.
Re: Scan For Open Word Documents
I understand that, I would look at wordApp.Documents.Count and its value is 1. I would get into the foreach loop once, but when I looked at tempWordDoc nothing is assigned to it.
Re: Scan For Open Word Documents
Adding... For Windows XP, Word 2003 I can open Outlook and a couple of Word files all at the same time and there is only one wonword process running.
Re: Scan For Open Word Documents
I dont see where your calling that procedure - OpenWordFile?
Re: Scan For Open Word Documents
Quote:
Originally Posted by steve65
Adding... For Windows XP, Word 2003 I can open Outlook and a couple of Word files all at the same time and there is only one wonword process running.
It depends on how your opening the Word docs?
Re: Scan For Open Word Documents
I call it here
Code:
private void StartFind()
{
try
{
OpenWordApp();
if (wordApp != null)
{
wordApp.Visible = true;
if (OpenWordFile(this.txtFilePath.Text))
{
// process file
}
}
}
catch(System.Exception ex)
{
}
finally
{
}
}
Re: Scan For Open Word Documents
If the wordApp.Documents has a .Count of 1 or more then tempWordDoc should have at least one document in it?
Code:
foreach (Word.Document tempWordDoc in wordApp.Documents)
Re: Scan For Open Word Documents
Yeah, that is what I did, but I could not get any infomation out of tempWordDoc. I will look at it again in the morning.
Re: Scan For Open Word Documents
I see your only testing for it not being Null (if (wordApp != null)) but your also not testing it for .Count > 0
2 Attachment(s)
Re: Scan For Open Word Documents
I hope this make sense. I have attached two images at the point in time that I am trying to get a Document from the open app. I opened up 5 Word documents at the OS level. They are using one instance of winword.exe.
Image one show that in the watch windows wordApp.Documents does not exist. However, in image two the watch window show that wordApp.Documents.Count = 5.
I am not sure what I am doing wrong that I can't work on a doc that is part of wordApp.