如何使用 VBA 识别 MS Word 文档中的文本?

问题描述 投票:0回答:0

问题:为什么IF语句的第一段不识别下面描述的文字?我这周才开始研究 MS Word VBA 代码。我了解 MS Excel 中的一些基本编程,但这让我感到困惑。

整个第一段是活动文档中的“示例文本”(对于此示例)。我正在以一种简单的方式写这篇文章,所以我可以理解这个过程。谢谢。

Sub ParagraphNo1()
Dim Para1 As Paragraph
Set Para1 = ActiveDocument.Paragraphs(1)
If Para1.Range.Text = "Sample Text" Then
   MsgBox "Yes, that is correct...it is Sample Text." 'It fails to recognize Sample Text. I did not find a hidden space.
End If

MsgBox Para1.Range.Text
'But the message box above describes it as Sample Text (exactly as above), but it is not recognized within the If Statement.

End Sub

'********************************************* 如果我把它改成:

If Para1.Range.Text <> "示例文本" Then MsgBox "是的,这是正确的...它不是示例文本。" '这告诉我它识别出不同的东西。 结束如果

我也尝试过使用不同的文本,但我遇到了同样的问题。我也尝试在末尾添加一个空格,但这也没有帮助。

vba word
© www.soinside.com 2019 - 2024. All rights reserved.