我有一个从Excel编辑器运行的宏,它在Word文档中搜索字符串,复制该字符串并将其粘贴到.Activesheet.Range("E27")中的一个单元格中。
所以我有这段代码,我已经接近让它工作了,但是我需要连接四个字符来使它工作,段落符号(),& "2“&”。
Sub AdjusFindFirstName()
Sub AdjFindFirstName()
'Variables declaration
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim ExcelApp As Excel.Application
Dim Rng As Word.Range
Dim ws As Worksheet
Dim TextToFind As String
Dim ApartmentPrice As String
Dim StartPos As Long
Dim EndPos As Long
Dim FullName As String
Application.ScreenUpdating = False
'Assigning object variables
Set WordApp = GetObject(, "Word.Application")
Set WordDoc = WordApp.ActiveDocument
Set Rng = WordApp.ActiveDocument.Content
'Searching
With Rng.Find
'.Text = TextToFind
.Text = "REGON 364061169, NIP 951-24-09-783,"
.Execute '?Rng = REGON 364061169, NIP 951-24-09-783,
If .Found = True Then
Rng.MoveEndUntil Cset:="2" & ". " 'expands Rng variable until first "2. "
Rng.MoveEnd wdWord, 3 'expands Rng variable wdWord, 3 further
'wdParagraph, wdLine, wdSentence, wdScreen
If Rng.Words.Last.Next.Text = "-" Then 'I want the text of the word following the last word in my range
Rng.MoveEnd wdWord, 2 'expands Rng variable wdWord, 2 further
End If
StartPos = InStr(1, Rng.Text, "2. ")
FullName = Mid(Rng.Text, StartPos + 3)
End If
End With
Debug.Print FullName
End Sub事实上,我甚至不能执行Range.MoveEndUntil Cset:= "2. "
我甚至试过Range.MoveEndUntil Cset:= "2" & ". "
这些行不会用任何单词或字符展开我的Rng变量。
但我真正需要的是找到/移动终端直到Cset:= "¶" & "2" & ". "
这里是printscreen,我选择了需要.MoveEndUntil的字符串

也许您可以告诉我如何将我的Rng变量展开到当前段落的末尾?
发布于 2020-01-28 17:15:01
仔细阅读MoveEndUntil的帮助主题可以发现,它只查看单个字符,而不是字符组合:
将指定范围的结束位置移动到文档中找到指定字符的任何为止。
没有以这种方式工作的“移动”方法。然而,Word的Find功能可以定位字符组合。添加一个从第一个“发现”项中运行的附加Find将定位问题中指定的信息。例如
Dim WordDoc As Word.Document
Dim rngStart As Word.Range, rngEnd As Word.Range
Set WordDoc = WordApp.ActiveDocument
Set rngStart = WordDoc.Content
'Searching
With rngStart.Find
'.Text = TextToFind
.Text = "REGON 364061169, NIP 951-24-09-783,"
.Execute '?Rng = REGON 364061169, NIP 951-24-09-783,
If .found = True Then
'Instantiate a Range for the next search, starting with the first "found"
Set rngEnd = rngStart.Duplicate
'Extend that range to the end of the document
rngEnd.End = WordDoc.Content.End
With rngEnd.Find
.Text = vbCr & "2. "
.Forward = True
.Execute
If .found Then
rngEnd.MoveEnd wdWord, 3
If rngEnd.Words.Last.Next.Text = "-" Then 'I want the text of the word following the last word in my range
rng.MoveEnd wdWord, 2 'expands Rng variable wdWord, 2 further
End If
End If
End With
startPos = InStr(1, rngEnd.Text, "2. ")
FullName = Mid(rngEnd.Text, startPos + 3)
End If
End Withhttps://stackoverflow.com/questions/59952686
复制相似问题