1

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
MrTea
  • 96
  • 1
  • 2
  • 9

5 Answers5

5

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.

Nachiket
  • 511
  • 5
  • 13
3

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);
    }
Community
  • 1
  • 1
V-SHY
  • 3,925
  • 4
  • 31
  • 47
  • Thanks V-SHY. Execution stops at CurrDoc = app.Documents.Open(@"C:\donkey.doc"); – MrTea Mar 03 '14 at 11:04
  • sorry, pressed enter. Code stops at line before app.Visible = true; and even if put this line earlier it makes no difference. thanks for suggestion though – MrTea Mar 03 '14 at 11:05
  • First try to run as **Console Application** with **app.Visible = true** see if it works? OR may be you can give a try on **accepted answer** of this post http://stackoverflow.com/questions/10837437/interop-word-documents-open-is-null – V-SHY Mar 03 '14 at 11:14
  • Thanks V-SHY. There isn't an accepted answer there but there is something to try which I will. I'm currently re-installing word 2003 so will post if that fixes it. – MrTea Mar 03 '14 at 11:26
  • sorry not accepted answer but **highest vote post** in that question. – V-SHY Mar 03 '14 at 11:31
  • Thanks, unfortunately setting that property breaks it. Re-installing word 2003 has helped though in that I now have an error thrown. Will follow that up and post anything useful. – MrTea Mar 03 '14 at 11:57
  • 1
    Highest post didn't cause problem. My Word2003 must have been corrupt previously. I guess the answer to my question was to re-install 2003. The error now is System.Runtime.InteropServices.COMException 'Command Failed' but that is a separate issue which I'll work on myself before bothering the community. Thanks for all your help V-SHY. – MrTea Mar 03 '14 at 12:16
  • @MrTea, if cannot work try create a **Desktop** folder in the path **C:\Windows\SysWOW64\config\systemprofile** because i saw many people solved their Word app problem in C# with this change, such as http://stackoverflow.com/questions/4031797/comexception-0x800a13e9-word-interop-services If found the solution, please update or answer your own question. thanks – V-SHY Mar 03 '14 at 12:57
  • Dude... I thank you immensely. You saved my life. Had a problem with interop for 3 days, and visible flag fixed the issue – Povilas Dirse Apr 25 '23 at 11:14
0

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.

danish
  • 5,550
  • 2
  • 25
  • 28
  • Thanks danish, I'm only openin – MrTea Mar 03 '14 at 10:29
  • sorry, pressed enter. I only opened the file to prove it's created ok. Usually I don't do that so it's not a factor in the core problem. thanks though. – MrTea Mar 03 '14 at 10:30
0

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.

MrTea
  • 96
  • 1
  • 2
  • 9
0

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.

Jesse
  • 3,522
  • 6
  • 25
  • 40