-1

Is there any way to check if a specific word document is Open? When I open document myself before opening the app when I tell to my app to write something in document first try to open the document and thats where my app is stuck.Is there a way to check before I try to open if the file is already opened? at this moment my code looks like this:

     object filename = s; // s is a string path which I get from database 
                Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document doc1 = app.Documents.Open(s);
                object missing = System.Reflection.Missing.Value;
                app.Visible = true;
  • Possible duplicate of [Is there a way to check if a file is in use?](https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use) – Filburt Aug 31 '17 at 07:52
  • @Filburt also tried to use that method to check if it is open or not , but actually it tell me the file is open every time even the file is not open , so I don't know why that method isn't working for me... –  Aug 31 '17 at 08:19
  • Exactly what do you mean by _"thats where my app is stuck"_ - do you get an exception, does the application "hang up"? – PaulF Aug 31 '17 at 08:34
  • I don't get any exception is just that I can't do anything and I have to stop the application from Visual studio –  Aug 31 '17 at 08:37
  • I think what is happening is that Word is trying to open the document & is displaying the "File In Use" dialog - asking if you want to open as Read-Only etc - but the application is not visible at that point. Try moving _"app.Visible =true;"_ to the line before you open the document. I know that doesn't answer the question (hence being a comment) but you will see what is going on. – PaulF Aug 31 '17 at 08:47
  • Also note - if you are stopping the application from Visual Studio - you do not close the instance of Word. If you check task manager you will most likely find multiple instances of Word running - all of them hidden because you make the application visible after a successful open. If you end all of those processes & try the solution in the link posted by Filburt then you will probably find it works (because one of the hidden instances has the file open). I have checked that solution & it does work for me. – PaulF Aug 31 '17 at 08:59
  • Well the problem isn't that my application is hidden, actually is not hidden ,I just open the word document by myself before trying to open it from Visual Studio and want to check what happens then, I also edited code as you said and yes , it does tell me that he can only open doc in red only, how can I check if that document is opened and then to close it before opening it again? ( What I want to do is that to avoid the case in which user is opening the word document and then trying to use my application which will need that document) –  Aug 31 '17 at 10:02
  • It is the new instance of Word created by your application that is hidden - not your application or Word you opened yourself. The answer given in the link from Filburt does work for me - returns true if the file is open & false if not. If it is always returning true then close all instances of Word you can see & then check the currently running processes via TaskManager - you may find instances of "WinWord" which are the hidden copies created by your application & remaining open because you closed your application via Visual Studio. If you end all of those you should find the answer works. – PaulF Aug 31 '17 at 10:14
  • Ah yes I checked the task manager and yes indeed there are a few winword applications opened there, butt what if the user opened the document not via application but manually, will still be a winword.exe opened in task manager? how I can delete all winword.exe from there when I start the application? –  Aug 31 '17 at 10:18
  • The unwanted instances are only there because you stopped your application with Visual Studio - if you properly close the document via your application or via Word itself - then you will not get the hidden instances which are holding the document open/locked - so will not need to find a way of shutting them down from your application. Checking with the IsFileLocked method should work how you expect once you end the hidden WinWords - check before creating an instance of the Application - directly after _object filename = s;_ in your code snippet. – PaulF Aug 31 '17 at 10:29
  • I am checking with isfilelocked if any winword.exe is available in task manager and if it is I use a method to kill the process, I do the same when I close the application, cause app.quit() does not kill the .exe from task maanger, and btw if I kill all winword processes that means it will also shut down unwanted word documents? I mean if a user has another word docs opened, will my app shut down them? cause I don't want that to happen. –  Aug 31 '17 at 10:35
  • Always close the Document before quitting the application. In my application the WinWord process disappears a few seconds after quitting. You may have to force the shutting down of the process by manually releasing the COM objects (both the application & the document) & calling the GC. When using Excel interop I always ensure that I do release all COM objects immediately after use, but I have not had any issues requiring that with Word. The answer here shows what to do : https://stackoverflow.com/questions/10963621/how-to-free-an-microsoft-office-interop-word-application-wordapp-instance – PaulF Aug 31 '17 at 10:58
  • Ok I will try like that –  Aug 31 '17 at 11:06
  • Btw do you know where doc.SaveAs() method is saving the document? Or is just making a temporarry file ?How can I save the document to a specified file? –  Aug 31 '17 at 11:07
  • What I would normally do is to put everything into a try-catch-finally block. The document close, application quit, release COM objects would all be in the finally block ensuring that they are performed regardless of any exceptions that may occur. – PaulF Aug 31 '17 at 11:08
  • Without no parameters, the document is saved in the original file if it has a name, otherwise it will popup a save as dialog. If you want to save as a different filename - then yo specify the new path/filename as a parameter. https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._document.saveas.aspx – PaulF Aug 31 '17 at 11:10
  • I tried to add a path but actually I get error from Visual Studio when I try to write ref missing for the parameters I do not need, is there any solution for that? –  Aug 31 '17 at 11:19
  • All of the parameters are optional, so you should only need to pass the new filename as the first parameter. What error message do you get - if you are using the try-catch-finally block as I suggested the you need to declare _object missing_ outside of that so it is in scope in the finally block - otherwise I get no compiler errors when I try that. – PaulF Aug 31 '17 at 11:27
  • Ah but if I do like this doc.SaveAs(filename) than it works but actually I can't find where is that document saved so I want something like this doc.SaveAs(filename,filepath) –  Aug 31 '17 at 13:32
  • The second parameter is the file format, not the path - you need to create the full pathname & pass it as the first parameter - you could use doc.SaveAs(Path.Combine(filepath,filename)); - https://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110).aspx – PaulF Aug 31 '17 at 14:53
  • Ah hope it will work, thank you for your time –  Aug 31 '17 at 15:51
  • @PaulF I still can't eliminate the WINWORD.exe after I close the doc and app : finally { doc1.Close(); ((_Application)app).Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); app = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } that's what I have in finally block... –  Sep 01 '17 at 06:22
  • Everything works correctly in my small app. I suggest you go back to the basics of debugging - firstly create a new Word instance & quit it - check the TaskManager that WinWord disappears. Then add opening/closing the document. Then add SaveAs. Then add each part of your processing in turn. Try to find out what stops WinWord from disappearing. Remember I said to release ALL COM objects - pretty much every object you create with Microsoft.Office.Interop.Word is a COM object - so you may need to add _Marshal.ReleaseComObject(doc1)‌​; doc1=null;_ as well. Similar for other objects you create. – PaulF Sep 01 '17 at 08:28

1 Answers1

0

just put your code in a

try
{
   //your code here
}
catch (Exception e)
{
   //your behavior when the file is opened 
}
Mehdi Ben Hamida
  • 893
  • 4
  • 16
  • 38
  • Using exception handling in this case is rather a last resort - at least it should distinguish between the file access exception / IO and other exceptions. – Filburt Aug 31 '17 at 07:54
  • See my comment to the question - at this point Word is not opening the file but showing a dialog (unfortunately the application is hidden) - so no exception occurs. – PaulF Aug 31 '17 at 08:50