我在创建Microsoft-Word宏时遇到问题。这是我正在研究的宏。它成功地选择了word文档中的每个单独的表。
Sub FindSpecificTables()
Selection.WholeStory
Dim iResponse As Integer
Dim tTable As Table
'If any tables exist, loop through each table in collection.
For Each tTable In ActiveDocument.Tables
tTable.Select
If response = vbNo Then Exit For 'User chose to leave search.
Next
MsgBox prompt:="Search Complete.", buttons:=vbInformation
End Sub
但是,如果表包含指定的字符串,我只需要选择一个表。这应该很简单,但我无法弄清楚。如何在表中搜索特定字符串?
我尝试使用以下条件语句调整代码:
If tTable.Cell(1, 1) = "Adjusted:" Then tTable.Select
; 请参阅下面的示例。
Sub FindSpecificTables()
Selection.WholeStory
Dim iResponse As Integer
Dim tTable As Table
'If any tables exist, loop through each table in collection.
For Each tTable In ActiveDocument.Tables
If tTable.Cell(1, 1) = "MySpecifiedString:" Then tTable.Select
If response = vbNo Then Exit For 'User chose to leave search.
Next
MsgBox prompt:="Search Complete.", buttons:=vbInformation
End Sub
不幸的是,这不起作用。我的语法错了吗?你们有什么建议或建议吗?
答案 0 :(得分:2)
尝试使用不同的方法...而不是循环每个表循环search (find)
方法,并检查找到的文本是否在表中。这是一个简单的解决方案:
Sub Find_Text_in_table()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "donec"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
If Selection.Information(wdWithInTable) Then
Stop
'now you are in table with text you searched
'be careful with changing Selection Object
'do what you need here
End If
Loop
End Sub