excel - 在 Word 中的页面范围内进行 VBA 搜索
问题描述
我正在尝试从第 5 页开始在 Word 文档中进行 Excel VBA 搜索,一旦找到特定关键字,它应该只针对第一个遇到的表格,并将 Word 表格中的一些单元格返回 Excel,如下面的代码所示。我正在尝试在其中介绍您的 Option1,但目前我不能。知道为什么吗?
Option Explicit
Sub Testt()
Dim ws As Worksheet
Dim Selection As Object
Dim objWord As Word.Application
Dim i As Integer
Dim strValue As String
Dim wdDoc As Word.Document
Dim wdFileName As Variant
Dim TableNo As Integer 'table number in Word
Dim myTableRange As Word.Range ' formerly variable 'a'
Dim oWordApp As Object, oWordDoc As Object
Dim pgNo As Long
Dim FlName As String
Dim SearchText As String
Dim IopenedWord As Boolean
Const wdMainTextStory As Integer = 1
Const wdActiveEndPageNumber As Integer = 3
Const wdStory As Integer = 6
Const wdFindContinue As Integer = 1
Set objWord = New Word.Application
Set wdDoc = objWord.Documents.Open("C:\Users\Nigel\Desktop\Testt.docx")
objWord.Visible = True
With wdDoc.StoryRanges(wdMainTextStory)
With .Find
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = True
.Wrap = wdFindContinue
.Text = "Test"
.Execute
Do While objWord.Selection.Find.Execute = True
'~~> Get the page number
pgNo = objWord.Selection.Information(wdActiveEndPageNumber)
'~~> Check if the page number is >= 5
If pgNo >= 5 Then
Debug.Print "Search text found in page " & pgNo
End If
Loop
End With
If .Find.Found Then
MsgBox "Found"
Else
MsgBox "Not found"
Exit Sub
End If
Set myTableRange = .Duplicate.Next(unit:=wdTable)
Dim rowNb As Long
Dim ColNb As Long
Dim x As Long
Dim y As Long
x = 8
y = 1
With myTableRange.Tables(1)
For rowNb = 1 To 1 '
For ColNb = 2 To 2
Cells(x, y) = WorksheetFunction.Clean(.Cell(rowNb, ColNb).Range.Text)
y = y + 1
Next ColNb
y = 1
x = x + 1
Next rowNb
End With
x = x + 2
End With
End Sub
解决方案
在上面的评论中,我提到了实现你想要的 3 种方法。我相信还有其他方法可以给猫剥皮。
这是一个关于如何从页面开始搜索文本的示例(选项 1 )。5
我已经评论了代码。不过,如果您不明白,请随时发表评论,如果我能回复,我会的。
Option Explicit
Const wdMainTextStory As Integer = 1
Const wdActiveEndPageNumber As Integer = 3
Const wdStory As Integer = 6
Const wdFindContinue As Integer = 1
Sub Sample()
Dim oWordApp As Object, oWordDoc As Object
Dim pgNo As Long
Dim FlName As String
Dim SearchText As String
Dim IopenedWord As Boolean
'~~> Establish an Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set oWordApp = CreateObject("Word.Application")
IopenedWord = True
End If
Err.Clear
On Error GoTo 0
oWordApp.Visible = True
'~~> Sample File
FlName = "C:\Users\routs\Desktop\Sample.Docm"
Set oWordDoc = oWordApp.Documents.Open(FlName)
'~~> Search Text. Change as applicable
SearchText = "Siddharth"
'~~> Move to the begining of the document
oWordDoc.Bookmarks("\StartOfDoc").Select
oWordApp.Selection.Find.ClearFormatting
With oWordApp.Selection.Find
.Text = SearchText
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
'~~> Loop and find the search text
Do While oWordApp.Selection.Find.Execute = True
'~~> Get the page number
pgNo = oWordApp.Selection.Information(wdActiveEndPageNumber)
'~~> Check if the page number is >= 5
If pgNo >= 5 Then
Debug.Print "Search text found in page " & pgNo
End If
Loop
End With
oWordDoc.Close (False)
If IopenedWord = True Then oWordApp.Quit
End Sub
输出
如果我改变
If pgNo >= 5 Then
Debug.Print "Search text found in page " & pgNo
End If
至
Debug.Print "Search text found in page " & pgNo
然后我得到这个
推荐阅读
- python - 将 UTF-8 字符串转换为 XML/HTML 字符串时遇到问题
- javascript - Vuetify 树视图
- java - JavaFX 11 多线程 NPE
- spring - 使用 OAuth2 时 Spring-boot 应用程序未进行身份验证(更新 #2)
- updates - 在集群之前更新 ceph 客户端?
- c# - 将字符串转换为新格式
- javascript - 添加点击事件以映射标记信息窗口
- flutter - 如何在颤动中使用CustomPainter模糊图片中的特定位置
- android - AndroidX:'com.google.android.material:material*' 库正在调用 androidx.cardview:cardview:1.0.0 但这不会下载
- google-cloud-platform - GCP批量解压维护文件结构