Question

Please can someone help with a problem opening a Word2003 file in code using Microsoft.Office.Interop.Word?

My code is below. The document is created fine and if I pause the code after creating it I can open the file via explorer. The code freezes on the final line. At this point one can see a file locking metafile appear in explorer as well as the original. There is no error generated that I can see. Maybe there is an invisible dialog but otherwise I'm stumped. Thanks in advance.

  1. Firstly write a byte array to a file

    var tmpFile = @"C:\donkey.doc";
    File.WriteAllBytes(tmpFile, binary_document);
    
  2. Open the file as a document object of some type

    Application app = new Application();
    Document CurrDoc = app.Documents.Open(@"C:\donkey.doc");
    

Solution to freeze was re-installing Word2003 although I have actually abandoned the approach altogether due to the server issues identified here http://support.microsoft.com/kb/257757. Thanks for all help.

Was it helpful?

Solution 4

Solution to freeze was re-installing Word2003 although I have actually abandoned the approach altogether due to the server issues identified here http://support.microsoft.com/kb/257757. Thanks for all help.

OTHER TIPS

Try this it may help you.

Create a new "Desktop" directory inside of "C:\Windows\SysWOW64\config\systemprofile\" it works for me after a long long long day searching for the solution.

It seams to be a profile problem.

What I will check in same situation

  1. Permission access
  2. Create a file outside C# and only remain the file open part
  3. When stuck at open command, task manager has Microsoft Word exe running?

Suggestion to Solve

1) Run as Console Application (those posts I mentioned they work well in Console)

2) Try to put CurrDoc.Activate() after CurrDoc = app.Documents.Open(@"C:\donkey.doc");

3) Try to declare byte[] binary_document = { 112 }; but not using your current array to let File.WriteAllBytes() finish its work faster.

4) Try Highest vote post of Interop.Word Documents.Open is null

5) Try Suggestion for XP (search "xp") in Word 2007 Documents.Open returns null in ASP.NET

6) Try catch the exception (but seem like your case is not exception)

try
{
    CurrDoc = app.Documents.Open(tmpFile);
}
catch (Exception eX)
{
    //MessageBox.Show(eX.ToString());
    Console.WriteLine(eX);
}

Sorry hope I'm not confusing you.


Work For Me

Refer to @Mike Miller, the main point is app.Visible is not set to true; The app is active but Only it is NOT visible!! Learn something new. thanks.

I am using Microsoft Word 2010 and Windows 7 Home Premium 64 bit.

    Document CurrDoc;
    //avoid ambiguity so put in missing argument
    object missing = System.Reflection.Missing.Value;
    Microsoft.Office.Interop.Word.Application app;

    private void btnMakeandOpenDoc_Click(object sender, EventArgs e)
    {
        //put in some byte value into the array
        byte[] binary_document = { 112, 132, 32, 33,231,125,87 };
        var tmpFile = @"C:\donkey.doc";
        File.WriteAllBytes(tmpFile, binary_document);
        app = new Microsoft.Office.Interop.Word.Application();
        CurrDoc = app.Documents.Open(@"C:\donkey.doc");
        //main point
        app.Visible = true;
    }

    //close the opening doc file also
    private void btnCloseDoc_Click(object sender, EventArgs e)
    {
        CurrDoc.Close(ref missing, ref missing, ref missing);
        app.Application.Quit(ref missing, ref missing, ref missing);
    }

While debugging, if you manually open this file through explorer, the last line will also try and do the same thing. Now, I cannot remember Office 2003 behaviour any more but 2010 does prompt that you are trying to open same file again (or at least it does that to me). This could well be the reason.

I created a desktop folder but in this folder: C:\Windows\System32\config\systemprofile and gave the service account access to the folder. Do not know if the access is neccessary but it worked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top