excel - VBA编译错误,在word文档上执行.Find方法时参数不是可选的
问题描述
Stackoverflow 社区。我正在学习中。在 VBA 中查找方法。
我正在 Word 文档中搜索姓氏(100% 确定它在那里),但宏是从 Excel VBA 编辑器编写和午餐的。
我正在观看此视频并将代码重写到我的 VBA 编辑器中,我已经检查过它并且正在运行它。
我已经粘贴了下面的代码:
Sub UsingTheFindObject_Simple()
'Declare our variables
Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document
Dim srchResult As Boolean
'Grab the active document
Set wrdDoc = GetObject(, "Word.Application.ActiveDocument")
Set excelWrkbook = GetObject(, "Excel.Application.ActiveWorkbook")
'Define the content in this document
Set wrdRng = wrdDoc.Content
'Define the Find Object based on the range
Set wrdFind = wrdRng.Find 'this line gives the "Compile error, Argument not optional"
'Define the parameters of our search
With wrdFind
'Look for the phrase: TOKAJ-SMOCZKIEWICZ
.Text = "TOKAJ-SMOCZKIEWICZ"
.MatchWildcards = False
.MatchCase = False
.Forward = True
'Conduct the search if a match it returns TRUE else FALSE
srchResult = .Execute
End With
'If argument is found, display it
If srchResult = True Then
'Display message
Debug.Print "Found the word" & wrdRng.Find & ", now formatting."
'Change the font to bold
wrdRng.Bold = True
End If
End Sub
在宏开始之前,我在这一行中得到“编译错误,参数不是可选的”:
'Define the Find Object based on the range
Set wrdFind = wrdRng.Find 'this line gives the "Compile error, Argument not optional"
它看起来像这样:
您对如何使其首先发挥作用有任何想法吗?
为什么这个 .find 属性不适合那里?我发现这个网站说 .find 是选择的属性,在我的宏中,它被用作范围。找到一个属性,但是。Find 与 Range 完美配合,不是吗?
解决方案
找到下面的工作代码主要归功于@GSreg 注释和@Zac 指导。
Sub UsingTheFindObject_Simple()
'Declare our variables
Dim wrdApp As Word.Application
Dim exclApp As Excel.Application
Dim wrdFind As Find
Dim wrdRng As Word.Range
Dim wrdDoc As Word.Document
Dim mySheet As Excel.Worksheet
Dim exclWrkbook As Excel.Workbook
Dim srchResult As Boolean
'Grab the active document
Set wrdApp = GetObject(, "Word.Application") 'At its simplest, CreateObject creates an instance of an object,
Set exclApp = GetObject(, "Excel.Application") 'whereas GetObject gets an existing instance of an object.
Set wrdDoc = wrdApp.ActiveDocument
'Set wrdDoc = GetObject(, "Word.Application.ActiveDocument") 'This line alone doesn't work.
Set exclWrkbook = exclApp.ActiveWorkbook
'Set exclWrkbook = GetObject(, "Excel.Application.ActiveWorkbook") 'This line alone doesn't work.
Set mySheet = Application.ActiveWorkbook.ActiveSheet
'Define the content in this document
Set wrdRng = wrdDoc.Content
'Define the Find Object based on the range
Set wrdFind = wrdRng.Find
'Define the parameters of our search
With wrdFind
'Look for the phrase: TOKAJ-SMOCZKIEWICZ
.Text = "TOKAJ-SMOCZKIEWICZ"
.MatchWildcards = False
.MatchCase = False
.Forward = True
'Conduct the search if a match it returns TRUE else FALSE
srchResult = .Execute
'Debug.Print wrdFind 'Object doesn't support this property or method.
Debug.Print wrdRng
End With
'If argument is found, display it
If srchResult = True Then
'Display message
Debug.Print "Found the word " & wrdRng & ", now formatting."
'Change the font to bold
wrdRng.Bold = True
End If
End Sub
推荐阅读
- sql - in 运算符不使用 OrientDB 中的子查询
- java - 出现错误 DeploymentException:来自服务器 [400] 的 HTTP 响应不允许 HTTP 升级到 WebSocket
- c# - 在方法中改变属性
- python - 程序计算每行项目的税率等于增值税
- python - 为什么图 G2 不是图 G1 的子图同构
- kubernetes - 由于调度失败,Pod 处于挂起状态
- java - 部署后使用 marathon-api 自动关闭 Akka 集群中的节点
- python - UI 转换机器人的问题 - 'str' 对象不可调用 / lambda 停止工作 / Tkinter Py3
- r - 找到向量中相同值的最长序列,R
- reactjs - 更改 URL 的查询参数并覆盖路由器的历史记录