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:I would have thought this foreach loop would do the trick, but I am getting told that Documents does not existCode: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; }Have any of you done this before? Could I have a pointer on how you did it?Code:foreach (Word.Document tempWordDoc in wordApp.Documents) { if (tempWordDoc.Path == filename.ToString()) { wordDoc = tempWordDoc; break; } }
Thanks Steve




Reply With Quote