【发布时间】:2016-08-04 07:31:03
【问题描述】:
我有以下代码在文档中查找带有“pm”的句子 - 如果找到它会将其替换为值是主文档表 2 单元格 2,2。
由于某种原因,IF 语句不起作用并且总是转到 ELSE IF 而不替换文档中的文本 - 任何人都可以帮助我解决这个问题,因为我整个早上都被卡住了。
timeon = wdDocSrc.Tables(2).Cell(2, 2).Range.Text
If timeon = "a" Then 'ERROR HERE
TimeReplace = wdDocSrc.Tables(2).Cell(3, 1)
TimeText = TimeReplace
wdDocTgt.Activate
With wdDocTgt
Selection.Find.Execute FindText:="pm", Forward:=True
If Selection.Text = "pm" Then
With Selection
' Collapse current selection to insertion point.
.Collapse
' Turn extend mode on.
.Extend
' Extend selection to word.
.Extend
' Extend selection to sentence.
.Extend
End With
With Selection
'Selection.Text = "We are now open Christmas DAY!!. "
Selection.Text = TimeText
End With
Else
MsgBox "No timing in this letter"
End If
End With
Else
MsgBox "ERROR" 'AWAYS JUMPS TO THIS
End If
【问题讨论】:
-
你不能检查
timeon包含那个点吗? -
Timeon 包含指定单元格中的值(检查即时窗口时)。它旁边有一个黑色圆圈 - 这会影响它吗?如果是,我该如何将它从桌子上移除?
-
这是一个换行符,后跟表格单元格终止符。您可能想在比较之前删除它们。
标签: vba if-statement ms-word