从一个Word Doc添加复杂标题到另一个

时间:2016-06-01 20:05:16

标签: c# openxml

我正在使用https://msdn.microsoft.com/en-us/library/office/cc546917.aspx中的AddHeaderFromTo功能。问题是它没有采取可能在文档的标题部分中找到的任何图像或表格。有没有办法从标题到另一个文档?

2 个答案:

答案 0 :(得分:0)

下面的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

答案 1 :(得分:0)

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

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);