Visual Studio Development Bookmark and Share   
 index > Visual Studio Tools for Office > Copy Page From "a.docx" to "b.docx" - With images, graphics ...
 

Copy Page From "a.docx" to "b.docx" - With images, graphics ...

Hello.

What is the easiest way to copy/insert a full page from a document in another one?.

I have tried this but It doesn't work 100% with images, tables, etc.

-----------------------------------------------------------------------------------
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim oWord As New Microsoft.Office.Interop.Word.Application
        Dim oMissing As Object
        oMissing = Type.Missing
        Dim inicio, fin As Object

        Dim sRuta1 As String
        Dim sRuta2 As String

        Dim doc1 As Microsoft.Office.Interop.Word.Document
        Dim doc2 As Microsoft.Office.Interop.Word.Document
        Dim doc3 As Microsoft.Office.Interop.Word.Document

        sRuta1 = "C:\Documents and Settings\Neruda\Escritorio\Prueba Word\Origen\01.docx"
        sRuta2 = "C:\Documents and Settings\Neruda\Escritorio\Prueba Word\Origen\02.docx"
        oWord.Visible = True
       
         doc1 = oWord.Documents.Open(FileName:=sRuta1)
        'doc2 = oWord.Documents.Open(FileName:=sRuta2)
        doc3 = oWord.Documents.Add


        doc1.Activate()
        'oWord.Selection.InsertBreak(WdBreakType.wdPageBreak)
        'doc1.Content.FormattedText.InsertAfter(doc1.Content.Text)
        inicio = doc1.Content.FormattedText.Start
        fin = doc1.Content.End - 1
        doc1.Range(Start:=inicio, End:=fin).FormattedText.Copy()
        doc3.Range.Paste()
        'doc3.Activate()
        'doc3.Range.Paste()
        'fin = doc1.Content.End - 1
        'doc1.Range(Start:=fin, End:=fin).FormattedText = doc2.Content.FormattedText
    End Sub
----------------------------------------------------------------------------------

I enable or disable the comments for trying new things but ... nothing.

No idea !
Pablo_Neruda  Wednesday, October 14, 2009 9:55 PM
Hi Pablo_Neruda!

Have you tried using Word.Document.Content.Copy()?

You can also use the range's XML (The following code is in C#):



object missing = Type.Missing;
Word.Document document1;
Word.Document document2;

string wordXML = document1.Content.get_XML(false); // OR document1.WordOpenXML;

document2.Content.InsertXML(wordXML, ref missing);

Jose Anton Bautista  Thursday, October 15, 2009 5:22 AM
Hello.

With your code... how knows what page has to copy ?

Last night i was trying to use Content but I didn't get it works.

I will try with your code.

Have you tested it ?

Thanks man

(In Spanish: Hola que creo que tu tb eres español).


Pablo_Neruda  Thursday, October 15, 2009 11:22 AM
Hi Pablo_Neruda!

Based on your code, I assumed you were working with just one-page documents.
Anyway, here's a link you can visit to extract content by pages:

http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/6d918de1-7d28-4a30-87f4-455e389fd7f1

When you already have an instance of the range to be copied,
you can use the Copy() or get_XML() method as shown in the code I provided earlier.

Jose Anton Bautista  Thursday, October 15, 2009 12:05 PM
Thanks.

 I will try what you say and everything I see in these threads.

I hope it works !

I want that copies images, tables, etc. And that respects positions, spaces, colors, etc.
Pablo_Neruda  Thursday, October 15, 2009 12:44 PM

Hello,

I am not sure that you want to copy the content of a page or a whole page. If the latter, here is an answer posted by Cindy you could refer to: How to copy a particular page in a word document into the clipboard (including all the contents like header, footer, etc).

Best regards,
Bessie


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Bessie Zhao  Friday, October 16, 2009 7:39 AM

You can use google to search for other answers

Custom Search

More Threads

• Best way to migrate an Outlook 2003 folder based solution to a COM Add-in?
• problem in retrieving contacts
• [SOLVED] How to supress message : "the master template will be used" when inserting a subdoc ?
• Error Checking XML customization code
• VSTO 2005 for Outlook links
• Automating Excel Workbook
• "Unable to load one or more of the requested types" error on compile. MSDEV 2008 Outlook 2007 add-in
• Locking of custom property TAB from Outside for a word Document
• EventTracker For Outlook Folders
• Excel 2007 - Ribbon Control - VB.net