CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2004
    Posts
    239

    [RESOLVED] Preview a Word Document...

    I am trying to Preview Word Documents in a RichTextBox. The code works fine, with the following two exceptions.

    (1) When I open the file, I get the following Error Message Dialog:

    File in Use

    ABCFile.doc is locked for editing

    by 'Username'

    Click 'Notify to open a read-only copy of the document and
    receive notification when the document is no longer in use.

    The only one using this document is my program!

    (2) Even though I have set appWord.Visible = false, Word still shows up.

    C# Code:
    Word.ApplicationClass appWord;
    Word.Document docWord;
    Object filename = path;

    Object ConfirmConversions = System.Reflection.Missing.Value;
    Object ReadOnly = true;
    Object AddToRecentFiles = false;
    Object PasswordDocument = System.Reflection.Missing.Value;
    Object PasswordTemplate = System.Reflection.Missing.Value;
    Object Revert = System.Reflection.Missing.Value;
    Object WritePasswordDocument = System.Reflection.Missing.Value;
    Object WritePasswordTemplate = System.Reflection.Missing.Value;
    Object Format = System.Reflection.Missing.Value;
    Object Encoding = System.Reflection.Missing.Value;
    Object Visible = false;

    Object saveChanges = false;
    Object originalFormat = null;
    Object routeDoc = null;

    appWord = new Word.ApplicationClass();
    appWord.Visible = false;
    docWord = appWord.Documents.Open(ref filename, ref ConfirmConversions, ref ReadOnly,
    ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
    ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate,
    ref Format, ref Encoding, ref Visible);
    docWord.ActiveWindow.Selection.WholeStory();
    docWord.ActiveWindow.Selection.Copy();
    IDataObject data = Clipboard.GetDataObject();
    _RichTextBox.Rtf = data.GetData(DataFormats.Rtf).ToString();
    docWord.ActiveWindow.Close(ref saveChanges, ref routeDoc);
    appWord.Quit(ref saveChanges, ref originalFormat, ref routeDoc);

  2. #2
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Preview a Word Document...

    You are not releasing the COM components properly. So, the word application won't quit when you say so (appWord.Quit() won't work).

    Follow these rules

    1. Declare a variable exclusively when you try to use a COM object in your code.

    Ex:

    docWord = appWord.Documents.Open(...) // Wrong


    // Right
    WordDocuments(?) docs = appWord.Documents;
    docWord = docs.Open(...);

    2. Release the child object first before releasing the parent. If you don't release the child, the parent will survive for atleast 2 GC cycle (It will wait for the child to be released by GC). Ex: Release the "docWord" first before releasing the "docs".

    3. Use Marshal.ReleaseComObject() method to release the COM references.

    If you do it correctly, you won't see Word instance running in your task manager (process tab).

  3. #3

    Re: Preview a Word Document...

    1) if your automation cancels or if you don't close the word document (it seems you close it before quiting wordapp) after running your application then the word file stays locked. You should check and clean your processes from WINDOWS.EXE when your application fails to complete.
    David Domingues at [email protected]. Feel free to visit http://www.webrickco.com

  4. #4
    Join Date
    Nov 2004
    Posts
    239

    Cool Re: Preview a Word Document...

    Got it to work doing the following:

    Word._Application appWord = null;
    Word._Document docWord = null;

    Object filename = path;
    Object ConfirmConversions = System.Reflection.Missing.Value;
    Object ReadOnly = true;
    Object AddToRecentFiles = System.Reflection.Missing.Value;
    Object PasswordDocument = System.Reflection.Missing.Value;
    Object PasswordTemplate = System.Reflection.Missing.Value;
    Object Revert = System.Reflection.Missing.Value;
    Object WritePasswordDocument = System.Reflection.Missing.Value;
    Object WritePasswordTemplate = System.Reflection.Missing.Value;
    Object Format = System.Reflection.Missing.Value;
    Object Encoding = System.Reflection.Missing.Value;
    Object Visible = false;

    Object saveChanges = false;
    Object originalFormat = null;
    Object routeDoc = null;

    appWord = new Word.Application();
    appWord.Visible = false;
    docWord = appWord.Documents.Open(ref filename, ref ConfirmConversions, ref ReadOnly,
    ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
    ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate,
    ref Format, ref Encoding, ref Visible);
    docWord.ActiveWindow.Selection.WholeStory();
    docWord.ActiveWindow.Selection.Copy();
    IDataObject data = Clipboard.GetDataObject();
    _RichTextBox.Rtf = data.GetData(DataFormats.Rtf).ToString();
    appWord.Documents.Close(ref saveChanges, ref originalFormat, ref routeDoc);
    appWord.Quit(ref saveChanges, ref originalFormat, ref routeDoc);


    Even though I have appWord.Visible set to 'false' it pops up when I Close docWord. Any ideas?

  5. #5
    Join Date
    Nov 2004
    Posts
    239

    Smile Resolved: Preview a Word Document...

    Here's the final code...

    Word._Application appWord = null;
    Word._Document docWord = null;

    Object filename = path;
    Object ConfirmConversions = System.Reflection.Missing.Value;
    Object ReadOnly = true;
    Object AddToRecentFiles = System.Reflection.Missing.Value;
    Object PasswordDocument = System.Reflection.Missing.Value;
    Object PasswordTemplate = System.Reflection.Missing.Value;
    Object Revert = System.Reflection.Missing.Value;
    Object WritePasswordDocument = System.Reflection.Missing.Value;
    Object WritePasswordTemplate = System.Reflection.Missing.Value;
    Object Format = System.Reflection.Missing.Value;
    Object Encoding = System.Reflection.Missing.Value;
    Object Visible = false;

    Object saveChanges = false;
    Object originalFormat = null;
    Object routeDoc = null;

    appWord = new Word.Application();
    appWord.Visible = false;
    docWord = appWord.Documents.Open(ref filename, ref ConfirmConversions, ref ReadOnly,
    ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
    ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate,
    ref Format, ref Encoding, ref Visible);
    if (docWord.ProtectionType == Word.WdProtectionType.wdNoProtection)
    {
    docWord.ActiveWindow.Selection.WholeStory();
    docWord.ActiveWindow.Selection.Copy();
    IDataObject data = Clipboard.GetDataObject();
    _RichTextBox.Rtf = data.GetData(DataFormats.Rtf).ToString();
    }
    else
    {
    _RichTextBox.Rtf = "";
    }
    docWord.ActiveWindow.Close(ref saveChanges, ref routeDoc);
    appWord.Quit(ref saveChanges, ref originalFormat, ref routeDoc);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured