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

后端 未结 3 945
忘了有多久
忘了有多久 2021-01-02 18:02

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

For paragraph that are not in a table, this prop

相关标签:
3条回答
  • 2021-01-02 18:35

    You can use the Information property:

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

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

    0 讨论(0)
  • 2021-01-02 18:36

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

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

    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
    
    0 讨论(0)
  • 2021-01-02 18:38

    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.

    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).

    0 讨论(0)
提交回复
热议问题