2017-03-16 254 views
0

我有很多时间研究该代码后发现:VBA的Excel - 取消页眉和页脚在Word

objWord.ActiveDocument.Fields.Unlink 

只是断开在通过链接的Word文档的正文中字段“选择性粘贴>粘贴链接“到Excel,但保留页眉和页脚链接。

什么是解除标题和页脚字段的代码?

我已经发现了一些代码网上,可能是沿着正确的道路和一些工作.....

Dim oField As Field 
Dim oSection As Section 
Dim oHeader As HeaderFooter 
Dim oFooter As HeaderFooter 

For Each oSection In ActiveDocument.Sections 

    For Each oHeader In oSection.Headers 
     If oHeader.Exists Then 
      For Each oField In oHeader.Range.Fields 
       oField.Unlink 
      Next oField 
     End If 
    Next oHeader 

    For Each oFooter In oSection.Footers 
     If oFooter.Exists Then 
       For Each oField In oFooter.Range.Fields 
        oField.Unlink 
      Next oField 
     End If 
    Next oFooter 

Next oSection 

我理解了上面将无法工作只是复制并粘贴到用户窗体模块因为它是用于Word的代码。我没有修改它的知识,所以它可以在Excel中使用。

回答

0

为了解决这个问题,我将我在问题中提出的代码放到名为UnlinkHeader的子文档的Word文档的VBA中,而不是Excel中,并且只是从Excel中调用它。

Call objWord.Run("UnlinkHeader")