在VBA中复制没有页眉和页脚的Microsoft Word文件

时间:2015-03-12 19:50:10

标签: vba ms-word word-vba

我有一个带有超链接的Microsoft Word 2010文档(对其他文档的引用)。我目前正在解析这些链接,并用链接文件的内容替换超链接。现在我遇到的问题是有大量部分的文件。这些部分包含标题。粘贴此类内容时,“主”文档的标题会发生变化。但我想保留顶级主文档的标题样式。粘贴文件时没有部分和标题,一切都很好。

Sub hyperlink_replace()

Dim strPath As String
Dim strFileName As String
Dim oLink As Hyperlink
Dim oPar As Paragraph

'for nested documents
Do While ActiveDocument.Hyperlinks.Count > 0
For Each oPar In ActiveDocument.Paragraphs
    If oPar.Style.ListLevelNumber > 0 Then
        CurrentListLevel = oPar.Style.ListLevelNumber
    End If

    For Each oLink In oPar.Range.Hyperlinks
        If Not oLink.Name Like "_Toc*" Then
            strPath = ActiveDocument.Path
            strFileName = oLink.Address          

            'select hyperlink
            oLink.Range.Select
            'replace hyperlink with content of sub doc
            openAndCopy strPath, strFileName, CurrentListLevel
        End If
    Next oLink
Next oPar
Loop
End Sub

Sub openAndCopy(ByVal strPath As String, ByVal strFileName As String)  
Dim docFileName As String
docFileName = strFileName

If InStr(1, strFileName, "\") Then
  docFileName = Right(strFileName, Len(strFileName) - InStrRev(strFileName, "\"))
Else
    If InStr(1, strFileName, "/") Then
        docFileName = Right(strFileName, Len(strFileName) - InStrRev(strFileName, "/"))
    End If
End If

'open sub doc
ChangeFileOpenDirectory strPath
Documents.Open strPath & "\" & docFileName, ConfirmConversions:=True, ReadOnly:=False, AddToRecentFiles:=False, _
        PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
        WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
        wdOpenFormatAuto, XMLTransform:=""

 'select content and copy selection
 ActiveWindow.Document.Content.Select
 Selection.Copy
'close linked document
ActiveWindow.Close savechanges:=False
'Paste in Master document
Selection.PasteAndFormat (wdUseDestinationStylesRecovery)      
End Sub

有没有人提示我的问题?如何在没有页眉和页脚,页面样式和所有其他元数据的情况下复制内容?

1 个答案:

答案 0 :(得分:0)

您没有在超链接文档中保存更改,因此您可以在复制信息之前运行删除页眉/页脚的子例程。本文详细介绍了如何执行此操作以及子例程的完整示例:http://word.tips.net/T001777_Deleting_All_Headers_and_Footers.html