none
Select another word document and without openint it, copy its content into present document

    Question

  • In a document level word application (not application level add-in), i am trying to achieve the following:

    1. Add a button in ribbon in current document for selecting the document path to be pasted in current document.
    2. Copy the content of the selected document "without" opening it.
    3. Paste the content in the current document.
      Private Sub btnSelectDoc_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnSelectDoc.Click<br/>
    <br/>
        Dim custFile As New OpenFileDialog<br/>
    <br/>
        Dim sourceDocPath As String<br/>
    <br/>
        If custFile.ShowDialog() = DialogResult.OK Then<br/>
    <br/>
          sourceDocPath = custFile.FileName<br/>
    <br/>
        End If<br/>
    <br/>
        Dim wordApp As New Word.Application<br/>
    <br/>
        Dim sourceDoc As Word.Document = wordApp.Documents.Open(sourceDocPath)<br/>
    <br/>
        sourceDoc.Range.Copy()<br/>
    <br/>
        Globals.ThisDocument.Application.Selection.Paste()<br/>
    <br/>
      End Sub
    

    The code is working, however, the document that needs to be copied is also getting opened. How to copy its content without opening it?

    Thanks,

    Ajit.

    Monday, July 12, 2010 9:49 AM

All replies

  • Hi

    If you need to insert the total contents into the document you can do the following;

    InsertFile which is linked to the Range and Selection objects of a document. this will insert the file at the specified area and will not cause the document to loaded.

    Hope this helps.

    Regards


    Mike Walker MVP - Visual Developer VSTO - Please mark the best replies as Answers ! Owner and CTO FWBS Ltd http://www.fwbs.net Twitter: www.twitter.com/mikewalker74
    Monday, July 12, 2010 10:22 AM
  • Thanks for the reply. However, i wish to retain the format of the source (inserted) document. When i insert the file, its format is not coming through.
    Monday, July 12, 2010 10:40 AM
  • The source document would import as intended if the styles were of a different name, from memory. In the document you wish to select to be copied what is the issue with this document being loaded and closed?. InsertFile is the most logical route for this but understand if the layout is not as you so desire.

    Regards


    Mike Walker MVP - Visual Developer VSTO - Please mark the best replies as Answers ! Owner and CTO FWBS Ltd http://www.fwbs.net Twitter: www.twitter.com/mikewalker74
    Monday, July 12, 2010 10:44 AM
  • I am writing an document level add-in where i can select another word documents and import its content along with format without opening it. The InsertFile solved the issue of inserting the content without opening it. However, it is not bringing in the format of the inserted doc. Any alternate ideas?
    Monday, July 12, 2010 11:07 AM
  • I am writing an document level add-in where i can select another word documents and import its content along with format without opening it. The InsertFile solved the issue of inserting the content without opening it. However, it is not bringing in the format of the inserted doc. Any alternate ideas?


    Hello Ajit

    Which version of Word is involved, here?

    FWIW, Mike is correct that, when using InsertFile, any text coming in that's formatted with a style of the same name in both the source and the target document will take on the style definition formatting of the TARGET document, as far as character- and paragraph-level formatting go. If we're talking about section-level formatting, things get even more complex.

    Unfortunately, you do not specify what kind of formatting you're concerned about. That makes it difficult to be much more specific about what you'd have to do with the source file in order to import its contents into a target file without losing the source formatting.

    FWIW if we're talking about Word 2007 or later, your best bet would be to extract the information from the OpenXML file and drop it into the target document using the InsertXML method. OpenXML file formats are not on-topic in the VSTO forum. For more information, I refer you to OpenXMLDeveloper.org, as well as the Open XML SDK for Visual Studio and the corresponding OpenXML SDK forum.


    Cindy Meister, VSTO/Word MVP
    Monday, July 12, 2010 3:48 PM
  • You can Open document as

     Dim
     sourceDoc As
     Word.Document = wordApp.Documents.Open(FileName:=sourceDocPath, Visable:=False)

    In that way user will not see that new document is opened. If that is only thing you wanted to accomplish.

    Best regards,

    Janko

    Wednesday, September 29, 2010 11:24 AM