2

i currently have a word doc. file merging program and am attempting to open a specific word doc. depending on user selection.

// sample of code used: 
string outputFolder = null;
...
// file selection
...  
string outcomeFolder = outputFolder;
string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".docx";
string outputFileName = Path.Combine(outcomeFolder, outputFile);

in the program, outputFolder is selected by the user via a fileBrowserDialog

currently, the program runs correctly and merges the files in the folder selected by the user however it fails to open Microsoft Word as well as the outcome document merged.

i've attempted to use:

Microsoft.Office.Interop.Word.Application officeApp = 
new Microsoft.Office.Interop.Word.Application();
...
// merging code
...   
Document documentTest = officeApp.Documents.Open(outputFileName);

i've noticed that though the program fails to launch Word, Task Manager continues to create a new instance of Word. The merged document formed also cannot be deleted as it claims the file is currently in use. It's as if program it's opening in the background however not physically launching. The Task Manager instance of Word must then be killed before the merged file can be edited / deleted

Any suggestions as to remedy this? Am i missing something simplistic or is the issue due to the non-static file path? - if any additional information is required please ask. thank you

Update 1: Since implementing the officeApp.Visible = true; the program now launches the file created which can then be edited / re-saved etc. However if i immediately run the program again, attempting to create another merged file within the same folder etc. I am presented with "RPC server is unavailable. (Exception from HRESULT: 0x800706BA)"

Update 2: As listed above, I was getting a generic HRESULT error code which I have now since remedied. I moved the "new officeApp" into the "Merge" handler which seems to be allowing multiple merges in quick succession without throwing errors.

Update 3: To make things more simplistic, i've experimented with implementing Process.Start(outputFileName); to open the document. This is due to the additional check box I am now introducing which allows the user to decide whether the merged doc. will be launched / presented once created. This new code also prevents the additional Word.exe's from being created if the file visibility is set to false.

thank you all for your suggestions and help.

cgraham720
  • 147
  • 1
  • 1
  • 11
  • Is your program running as the user or is it a service that runs as a service user, such as Local System? Perhaps your code is correct but the user is wrong. Check the extra Word.exe process. What user is running it? – Rhyous Mar 30 '16 at 18:42
  • 1
    Possible duplicate of [Open and modify Word Document](http://stackoverflow.com/questions/16253215/open-and-modify-word-document) – peinearydevelopment Mar 30 '16 at 18:49
  • Take a look at your code.. do you even debug it or proof read the code that you have written.. `outputFolder` you initialize to null then `outputFileName` you are doing `Path.Combine(null, outputFile)` you never assign `outputFolder` – MethodMan Mar 30 '16 at 18:50
  • stackoverflow.com/questions/16253215/1874522/#16298284 – peinearydevelopment Mar 30 '16 at 18:51
  • @methodman - outputFolder is displayed a null to begin with, but changes depending on user selection. Once the user selects their outcome destination for the merged file, the outputFolder will become that destination. – cgraham720 Mar 30 '16 at 18:55
  • well based on what you have posted one would not see that you should post all relevant code pertaining to your problem and or issue – MethodMan Mar 30 '16 at 18:56
  • @methodman - in my first code submission under //sample of code used: it already states string outputFolder = null, however i will try to add more information now. – cgraham720 Mar 30 '16 at 18:57

1 Answers1

4

Have you tried making Word visible?

officeApp.Visible = true;
Ric Gaudet
  • 898
  • 6
  • 16
  • as simple as your response was, it worked perfectly! thank you! program now launches as intended. – cgraham720 Mar 30 '16 at 19:11
  • quick reply again, i've updated my original question with the new scenario and outcome i am facing. if i run the program twice with the same input / output, i am presented with an error code "RPC server is unavailable. (Exception from HRESULT: 0x800706BA) – cgraham720 Mar 30 '16 at 19:23
  • Depends on what you are trying to do, but you need to release COM objects after using them, and if you are running several merges, then you might consider just creating one instance of officeApp, and creating new documents using the same object. There are many reasons why you could get that error, including firewall issues, but I seem to remember having that problem because I was not properly handling my COM calls. If it works the first time and then fails on the second run, I would focus my attention on COM issues first. – Ric Gaudet Mar 30 '16 at 19:35