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

    copy word document in c#

    Hi,

    I have a add-in written in c#( vs2005 ) for MSWord( 2003 ).

    I am trying to find a technique to copy the contents of loaded word document ( images, formatting etc. ) and create a new document from c# and paste the contents into this new document. This is what i got so far:

    Code:
                Word.Application oWord = new Word.Application();
    
                Word.Document oWordDoc = new Word.Document();
                Object oMissing = System.Reflection.Missing.Value;
                oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
    
                oWordDoc.ActiveWindow.Selection.WholeStory();
                oWordDoc.ActiveWindow.Selection.Copy();
    
                oWord.ActiveDocument.Select();
                
                oWordDoc.ActiveWindow.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
    To test i follow this procedure:

    Set startup app in VStudio to MSWord.
    Hit F5.
    VS open a new word doc.
    Put some text and an image in it.
    Save the document.
    Click the Pluin button.

    When the code above to copy contents runs, MSWord comes up with an error saying cannot read contents of this document. Repair this document.

    Code does not crash or anything.

    Is there a way to copy a word document loaded from c#.
    Last edited by 2kaud; November 16th, 2018 at 09:09 AM. Reason: Fixed code tags

  2. #2
    Join Date
    Aug 2004
    Posts
    391

    Re: copy word document in c#

    sorry. My bad. It works. I had the loading path wrong.

  3. #3
    Join Date
    Aug 2004
    Posts
    391

    Re: copy word document in c#

    Another problem with same component. It only works if you start MSWord and if it opens a new document.

    e.g If you go to program files->office->msword, word starts and loads a new document by default. The add-in button is available in commandbar and if user clicks, it behaves as it should.

    But if you double click on an existing document or start word and open an existing file, clicking on the add-in button does nothing!!!

    Can anyone please help regarding this. I have been searching web about this for over 3 days, with no results. I am really stuck on this , as without the plug-in being functional from a Word template, it is useless.

  4. #4
    Join Date
    Aug 2004
    Posts
    391

    Re: copy word document in c#

    36 views and no suggestions?

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: copy word document in c#

    Quote Originally Posted by venAdder
    36 views and no suggestions?
    What do you think that means?

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