vba 如何在 MS-Word 宏中检查段落是否在表格中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6436801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:31:50  来源:igfitidea点击:

How to check if a Paragraph is in a Table or not in MS-Word macro?

vbams-word

提问by Dhaliwal

The paragraph object in the Word has a property called Range. Within this Range object has a property called Cells.

Word 中的段落对象有一个名为 Range 的属性。在这个 Range 对象中有一个叫做 Cells 的属性。

For paragraph that are not in a table, this property Paragraph.Range.Cells is set to "". This can be seen in the Watches window in debug mode.

对于不在表格中的段落,此属性 Paragraph.Range.Cells 设置为“”。这可以在调试模式下的 Watches 窗口中看到。

For paragraph that are in a table, the property Paragraph.Range.Cells has other properties in it, for example it has a property called Count.

对于表格中的段落,属性 Paragraph.Range.Cells 中还有其他属性,例如它有一个名为 Count 的属性。

I am using this property of Paragraph.Range.Cells to determine if the paragraph is in a table or not. However, I cant seem to figure out how to test this.

我正在使用 Paragraph.Range.Cells 的这个属性来确定该段落是否在表格中。但是,我似乎无法弄清楚如何对此进行测试。

For example, I cannot simply test like this...

例如,我不能简单地像这样测试......

If paragraph.Range.Cells <> Null Then.... or even If IsNull(paragraph.Range.Cells) Then ...

If paragraph.Range.Cells <> Null Then.... 甚至 If IsNull(paragraph.Range.Cells) Then ...

It throws a Run-time error '5907' There is no table at this location

它抛出一个运行时错误“5907”这个位置没有表

So, how would I test for this? thanks

那么,我将如何测试呢?谢谢

回答by Philipp Sternberg

You can use the Informationproperty:

您可以使用该Information属性

If Selection.Information(wdWithInTable) Then
  'What ever you'd like to do
End If

Hence you don't need any manual error catching mechanisms.

因此,您不需要任何手动错误捕获机制。

回答by Gary McGill

You can't call the Cells method unless the paragraph is in a table. You need to use a different method to determine whether the range is in a table.

除非段落在表格中,否则您无法调用 Cells 方法。您需要使用不同的方法来确定范围是否在表中。

You can use either...

您可以使用任一...

paragraph.Range.Tables.Count > 0

...or...

...或者...

paragraph.Range.Information(wdWithinTable)

Note that the second one looks more obvious, but is actually slower (only a problem if you're doing this inside a loop).

请注意,第二个看起来更明显,但实际上更慢(如果您在循环中执行此操作,这只是一个问题)。

回答by Ahmad

*Edited (if Err=) changed to (If Err<>)

*编辑(如果 Err=)改为(如果 Err<>)

You can simply allow the error to happen and catch it using OnErrorstatement

您可以简单地允许错误发生并使用OnError语句捕获它

Dim ParagraphIsTable As Object

    OnError Resume Next        'allows errors to happen but execute next instruction
    ParagraphIsTable = paragraph.Range.Cells

  If Err <> 5907 Then '(this is to check for a specific error that might have happened)
          'No Error occured, this means that ParagraphIsTable variable must contain a value
          ' Do the rest of your code here
    Else
          ' an Error occured, this means that this is not a table
          ' do whatever
    End If
OnError Goto 0          ' This cancels the effect of OnError Resume Next
                  ' Which means if new errors happen, you will be prompt about them