open specific word doc. depending on user selection c#

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.

c#
asked on Stack Overflow Mar 30, 2016 by cgraham720 • edited Mar 30, 2016 by cgraham720

1 Answer

4

Have you tried making Word visible?

officeApp.Visible = true;
answered on Stack Overflow Mar 30, 2016 by Ric Gaudet

User contributions licensed under CC BY-SA 3.0