【问题标题】:If TABLE CELL VALUE = .... THEN (not working)如果 TABLE CELL VALUE = .... 那么(不工作)
【发布时间】: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


【解决方案1】:

删除黑色确实有效 - 使用此代码:

timeon = wdDocSrc.Tables(3).Cell(2, 2)
Timeconfig = timeon
Timeconfig = Left$(Timeconfig, Len(Timeconfig) - 2)
If Timeconfig = "Yes" Then
TimeReplace = wdDocSrc.Tables(2).Cell(2, 1)

TimeText = TimeReplace

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2022-07-25
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多