将复杂 Header 从一个 Word 文档添加到另一个

Adding Complex Header From One Word Doc to Another

我正在使用 https://msdn.microsoft.com/en-us/library/office/cc546917.aspx 中的 AddHeaderFromTo 函数。问题是它没有获取可能在文档的 header 部分中找到的任何图像或表格。有没有办法将 header 中的所有内容放入另一个文档?

下面的 VBA 脚本应该可以为您完成。 运行 来自 Word 文档。修改以适应。

Sub editAll()
    Dim Doc
    Dim i As Integer
    Dim docToOpen As FileDialog
    Set docToOpen = Application.FileDialog(msoFileDialogFilePicker)
    docToOpen.Show
    For i = 1 To docToOpen.SelectedItems.Count
        'Open each document
        Set Doc = Documents.Open(FileName:=docToOpen.SelectedItems(i))

        With ActiveDocument.Sections(1)
            'insert new headers
            Call editheader
        End With
    Next i
End Sub


Sub editheader()

    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
        ActivePane.View.Type = wdOutlineView Then
        ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    With Selection.PageSetup
        .LineNumbering.Active = False
        .Orientation = wdOrientPortrait
        .TopMargin = InchesToPoints(1)
        .BottomMargin = InchesToPoints(1)
        .LeftMargin = InchesToPoints(1)
        .RightMargin = InchesToPoints(1)
        .Gutter = InchesToPoints(0)
        .HeaderDistance = InchesToPoints(0.1)
        .FooterDistance = InchesToPoints(0.5)
        .PageWidth = InchesToPoints(8.5)
        .PageHeight = InchesToPoints(11)
        .FirstPageTray = wdPrinterDefaultBin
        .OtherPagesTray = wdPrinterDefaultBin
        .SectionStart = wdSectionNewPage
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .VerticalAlignment = wdAlignVerticalTop
        .SuppressEndnotes = False
        .MirrorMargins = False
        .TwoPagesOnOne = False
        .BookFoldPrinting = False
        .BookFoldRevPrinting = False
        .BookFoldPrintingSheets = 1
        .GutterPos = wdGutterPosLeft
    End With
    Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
        LinkToPrevious
    Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
        LinkToPrevious
    Selection.InlineShapes.AddPicture FileName:= _
        "C:\Users\rshuell001\Desktop\test\asher.jpg", LinkToFile:=False, _
        SaveWithDocument:=True
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

使用 OpenXMLPowerTools 我能够使用以下代码从一个文件中获取 headers 并将它们插入到另一个文件中:

var to = @"C:\Users\dheale\Desktop\Word Docs\_Blank_Skeleton.DOCX";
var from = @"C:\Users\dheale\Desktop\Word Docs\Letterheads\StandardLetterhead3.DOCX";
var outDoc = @"C:\Users\dheale\Desktop\Word Docs\test.DOCX";

var sources = new List<Source>
{
    new Source(new WmlDocument(to), false),
    new Source(new WmlDocument(from), true)
};

DocumentBuilder.BuildDocument(sources, outDoc);