简体   繁体   English

Word VBA删除带书签的表

[英]Word VBA to Delete Tables with Bookmarks

I have a Word Document with multiple Tables. 我有一个带有多个表的Word文档。 Some of them have bookmarks and the other don't. 其中一些带有书签,而另一些则没有。 Is there a way to implement ConvertToText command ONLY on the tables with the bookmarks on them using VBA (regardless of the bookmark name....I have over 40 bookmarks)? 有没有一种方法可以仅使用VBA在带有书签的表上实现ConvertToText命令(与书签名称无关。...我有40多个书签)? Basically, I'm trying to get rid of the bookmarked tables and keep just the text in those. 基本上,我试图摆脱加书签的表,只保留其中的文本。 The bookmarked tables are inserted INSIDE other tables WITHOUT bookmarks. 带书签的表将插入到没有书签的其他表中。 I'm using MS Word 2007. 我正在使用MS Word 2007。

Thanks in Advance! 提前致谢! Seb 塞布

Yes, there is quite simple solution. 是的,有一个非常简单的解决方案。 You need to loop through all bookmarks and check if one is wdWithInTable . 您需要遍历所有书签,并检查其中一个是否为wdWithInTable If so, convert whole table into text. 如果是这样,将整个表转换为文本。 Here is a code for you: 这是给您的代码:

Sub ConvertTablesWithBookmarks()


    Dim BK As Bookmark
    For Each BK In ActiveDocument.Bookmarks
        If BK.Range.Information(wdWithInTable) Then
            'uncomment for test just to check if working as expected
            'BK.Range.Tables(1).Select

            BK.Range.Tables(1).ConvertToText
        End If
    Next

End Sub

After additional explanation in comment below there are some hints for you to solve your problem. 在下面的注释中进行了其他解释之后,有一些提示可以帮助您解决问题。

To find a table in which your bookmark is: 要查找您的书签所在的表:

Activedocument.bookmarks(1).Range.Tables(1)

To find first table in above table you use this code 要在上表中找到第一个表,请使用此代码

Activedocument.bookmarks(1).Range.Tables(1).Tables(1)

but I think you can have a few tables inside you parent table. 但我认为您的父表中可以有一些表。 Therefore i suggest to run a loop like this (pseudo code, requires some adjustment to your needs): 因此,我建议运行这样的循环(伪代码,需要对您的需求进行一些调整):

dim parentTable as Table
dim BK as Bookmark     'or a reference to one from loop above
set BK = Activedocument.bookmarks(1)
set parentTable = BK.Range.Tables(1)
dim i as integer
for i=1 to parentTable.Tables.Count
    if parenttable.tables(i).range.start >=bk.range.start and _
       parenttable.tables(i).range.end <=bk.range.end Then
       'here we know that tables(i) is inside bookmark 1 (BK)

       parenttable.tables(i).converttotext
     end if
next i

I didn't have an oportunity to test it but hope it will work. 我没有机会进行测试,但希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM