|
-
Feb 28th, 2006, 02:01 PM
#1
Thread Starter
Fanatic Member
[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
-
Feb 28th, 2006, 02:08 PM
#2
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 28th, 2006, 02:37 PM
#3
Thread Starter
Fanatic Member
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.
-
Feb 28th, 2006, 02:40 PM
#4
Thread Starter
Fanatic Member
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.
-
Feb 28th, 2006, 02:41 PM
#5
Re: Scan For Open Word Documents
I dont see where your calling that procedure - OpenWordFile?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 28th, 2006, 02:42 PM
#6
Re: Scan For Open Word Documents
 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?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 28th, 2006, 02:52 PM
#7
Thread Starter
Fanatic Member
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
{
}
}
-
Feb 28th, 2006, 03:22 PM
#8
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)
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 28th, 2006, 05:29 PM
#9
Thread Starter
Fanatic Member
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.
-
Feb 28th, 2006, 06:09 PM
#10
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 2nd, 2006, 12:59 PM
#11
Thread Starter
Fanatic Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|