VBA Excel-取消标题和链接Word中的页脚 [英] VBA Excel - Unlink headers & footers in Word

查看:157
本文介绍了VBA Excel-取消标题和链接Word中的页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过数小时的调查,我发现该代码:

I have discovered after many hours of investigating that the code:

objWord.ActiveDocument.Fields.Unlink

仅取消通过选择性粘贴>粘贴链接"链接到Excel的Word文档正文中的字段的链接,而使页眉和页脚保持链接状态.

Only unlinks the fields in the body of a Word document that is linked via "Paste Special > Paste Link" to Excel but leaves the headers and footers linked.

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

What would be the code to unlink header and footer fields as well?

我在网上找到了一些可能正确的代码,并且可以使用某些代码.....

I have found some code online that could be along the right path and something to work with.....

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中使用它.

I understand the above won't work just copied and pasted into the userform module as it is code intended for Word. I do not have the knowledge to modify it so that it works from Excel.

推荐答案

为解决此问题,我将问题中提出的代码放在了名为UnlinkHeader而不是Excel的子文件中的Word文档的VBA中,并且只需从Excel调用它即可.

To resolve this, I placed the code I poseted in the question into the VBA of the Word document in a sub called UnlinkHeader, rather than Excel, and simply called it from Excel.

Call objWord.Run("UnlinkHeader")

这篇关于VBA Excel-取消标题和链接Word中的页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆