Search code examples
vbams-wordheaderms-officebookmarks

Word VBA - CopyPaste Image-header in a new Document


I've a function that copies a specified table into a new document, but i cant figure out how to export the header, which is an image, and set it as header for the document's pages.

The table is selected in base of the actual value of the combobox (ProjectsList.Value), which refers to a bookmark and the table contained in it is succesfly copy-pasted in a new document

For each pasted page, i want to copy the header too.

my function in which i would integrate the header part:

Sub CopyPaste()
Dim Source As Document
Dim Target As Document
Dim tbl As Table
Dim tr As Range
Dim hRange As Word.Range

Set Source = ActiveDocument
Set Target = Documents.Add
Target.SaveAs FileName:=ProjectsList.Value

For Each tbl In Source.Bookmarks(ProjectsList.Value).Range.Tables
    Set tr = Target.Range
    tr.Collapse wdCollapseEnd
    tr.FormattedText = tbl.Range.FormattedText
    tr.Collapse wdCollapseEnd
    tr.Text = vbCrLf
Next

End Sub

Solution

  • For example:

    Sub Replicate()
    Application.ScreenUpdating = False
    Dim Source As Document, Target As Document
    Dim Tbl As Table, HdFt As HeaderFooter, Rng As Range
    Set Source = ActiveDocument: Set Target = Documents.Add
    With Target
        For Each Tbl In Source.Bookmarks(ProjectsList.Value).Range.Tables
            Set Rng = .Range.Characters.Last
            Rng.FormattedText = Tbl.Range.FormattedText
            Rng.InsertAfter vbCr
        Next
        For Each HdFt In Source.Sections.First.Headers
            With HdFt
                Set Rng = Target.Sections.First.Headers(.Index).Range
                Rng.FormattedText = .Range.FormattedText
                Rng.Characters.Last.Delete
            End With
        Next
        .SaveAs FileName:=ProjectsList.Value
    End With
    Application.ScreenUpdating = True
    End Sub