问题描述
很抱歉问这个基本问题,但我是一名长期的 Access VBA 开发人员,我的屁股被 Word 踢了.
Sorry for the fundamental question but I am a LONGtime Access VBA developer who is getting my butt kicked by Word.
我正在为使用具有特定编号方案 (aaa-aaa-##-##-###) 的法律案件管理系统的人构建解决方案.我需要在文档中搜索连字符,然后获取找到连字符的整个段落.然后,我需要将该段落中的文本发送到一个单独的函数,该函数从该段落中解析出文件编号(该函数已经可用).
I am building a solution for someone who uses a Legal Case Management System with a specific numbering scheme (aaa-aaa-##-##-###). I need to search a document for hyphens and then grab the whole paragraph where a hyphen is found. I then need to send the text from that paragraph to a separate function which parses out the file number from the paragraph (this function already works).
如果该函数成功,则返回文件编号,否则返回NOT FOUND".所以,我需要:
If that function is successful, it returns the File Number, otherwise it returns "NOT FOUND". So, I need to:
查找所有连字符
Find all hyphens
用连字符捕获段落
将该文本传递给函数
转到下一个连字符如果函数返回未找到"
GO TO THE NEXT HYPHEN IF function returns "NOT FOUND"
我尝试了几十种选择都没有成功.我通常会陷入无限循环(并且似乎没有前进)或者我以错误的失败结束流程.
I have tried dozens of options without success. I typically get stuck in an infinite loop (and do not seem to move forward) or I get to the end of the process with a false failure.
我不知道如何移动到下一个出现的连字符并重复该过程.我也不知道如何将进程运行到文档末尾或在末尾停止(无需从头开始 - 因为连字符仍然存在,因为这不是替换进程).
I do not know how to move to the NEXT occurrence of a hyphen and repeat the process. I also do not know how to run the process to the end of the document or stop at the end (without starting all over - because the hyphens REMAIN since this is NOT a replace process).
我尝试了很多不同的版本,但我在下面列出了一个.
I have tried so many different versions, but I included one below.
感谢您的指导.我很感激.
Thanks for any guidance. I do appreciate it.
DGP
Public Sub TestFind77() '
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Text = "-"
.Execute Forward:=True
Do While .Found = True
.Parent.Expand Unit:=wdParagraph
Dim strWTF As String
strWTF = .Parent
'MsgBox strWTF
strResult = fnGetFileNumberFromString(strWTF) ' This works
If strResult <> "NOT FOUND" Then
GoTo success
End If
.Execute Forward:=True
Loop
End With
success:
MsgBox strResult
End Sub
推荐答案
我明白...良好的开端,你只遗漏了很小的部分.
I understand... Good start and you're missing only tiny pieces.
您需要的一件事是 Word 的 Range
对象.最好将其与 Find 结合使用 - 与 Selection
不同,您可以在代码中使用多个范围.
One thing you need is Word's Range
object. Best to use that with Find - unlike Selection
you can work with multiple Ranges in your code.
有时,在使用 Find 时,有必要返回原始范围(在您的情况下是整个文档).在这里,情况似乎并非如此,但无论如何,我已经根据一般原则将它内置了 - 所以如果结果证明你需要它,你就拥有它.
Sometimes, when working with Find it's necessary to refer back to the original Range (the entire document, in your case). That doesn't appear to be the case, here, but I've built it in, anyway, on general principle - so you have it if it turns out you need it.
我发现将 Find.Execute
的结果保存在一个布尔变量中比依赖于 .Found
更可靠,所以我把它放在了在,以及.
I've found it more reliable to save the result of Find.Execute
in a boolean variable, rather than relying on .Found
, so I've put that in, as well.
您可以使用 Range.Paragraphs(1)
选取 Range 所在的段落.我试图坚持你所拥有的,但如果你想进一步收紧你的代码,你可以这样做,只要你不需要其他任何段落:
You can pick up the paragraph in which the Range is located using Range.Paragraphs(1)
. I tried to stick to what you have, but if you want to tighten up your code even more, you could do this as long as you don't need the paragraph for anything else:
strWTF = rngSearch.Paragraphs(1).Range.Text
祝你好运!
Public Sub TestFind77()
Dim rngDoc as Word.Range
Dim rngSearch as Word.Range
Dim bFound as boolean
Dim para as Word.Paragraph
Set rngDoc = ActiveDocument.Range
Set rngSearch = rngDoc.Duplicate
With rngSearch.Find
.ClearFormatting
.Text = "-"
bFound = .Execute(Forward:=True)
Do While bFound = True
Set para = rngSearch.Paragraphs(1)
Dim strWTF As String
strWTF = para.Range.Text '???.Parent
'MsgBox strWTF
strResult = fnGetFileNumberFromString(strWTF) ' This works
If strResult <> "NOT FOUND" Then
GoTo success
End If
rngSearch.Collapse wdCollapseEnd 'search from after the found to the end of the doc
bFound = .Execute(Forward:=True)
Loop
End With
success:
MsgBox strResult
End Sub
这篇关于MS Word VBA 查找和循环(不替换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!