Results 1 to 3 of 3

Thread: [RESOLVED] Preview a Word Document...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    195

    Resolved [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:
    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

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    195

    Cool Almost: Preview a Word Document...

    Got it to work doing the following:

    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);
    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?
    Last edited by tim8w; Feb 16th, 2007 at 03:34 PM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    195

    Smile Resolved: Preview a Word Document...

    Here's the final code...

    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