VBA Excel - 取消链接标题& Word中的页脚

时间:2017-03-16 09:00:48

标签: vba excel-vba ms-word excel

经过几个小时的调查后我发现了代码:

objWord.ActiveDocument.Fields.Unlink

仅取消链接通过“选择性粘贴>粘贴链接”链接到Word文档的Word文档正文中的字段,但将页眉和页脚链接起来。

取消链接页眉和页脚字段的代码是什么?

我在网上发现了一些可以沿着正确的道路行进的代码,可以使用......

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 

据我所知,上述内容无法复制并粘贴到userform模块中,因为它是用于Word的代码。我没有修改它的知识,因此它可以在Excel中运行。

1 个答案:

答案 0 :(得分:0)

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

Call objWord.Run("UnlinkHeader")