英文:
Narrowing the range to add Hyperlinks
问题
我提取了包含类似于23/25的模式的文档句子。使用正则表达式完成。
问题是:当在使用正则表达式提取的句子中找到特定单词时,我想要添加到这个23/25的超链接内容会发生变化。
我找到的添加超链接的代码不允许我将范围缩小到找到的句子,并要求我使用ActiveDocument.range,因此是整个文档内容。
使用下面的代码,所有的23/25都将以相同的超链接结束。
是否有一种方法可以将范围设置/缩小到使用正则表达式找到的句子?
我知道下面的代码肯定写得不正确,我在这里挣扎。提前感谢您的帮助。
Set objRegex = New RegExp
With objRegex
.Pattern = "(\d{2}/\d{2})([a-zA-Zé /'/., ]{2,250})"
.Global = True
.IgnoreCase = True
Set matches = .Execute(Txte)
For Each fnd In matches
a = fnd.SubMatches.Count
For i = 0 To a - 1
If InStr(fnd, "cdtional word") Then
resul = fnd.SubMatches.Item(1)
Set Rng = ActiveDocument.Range{resul}
With Rng.Find
Do While .Execute(findText:=resul, Forward:=False) = True
Rng.MoveEndUntil (" ")
ActiveDocument.Hyperlinks.Add _
Anchor:=Rng, _
Address:="https://bla.org/" & resul
Rng.Collapse wdCollapseStart
Loop
Next
(结束标签)
<details>
<summary>英文:</summary>
I extract sentences of a document containing a pattern resembling to 23/25.
Done with reg. expressions.
The issue : having the content of the hyperlink I want to add to this 23/25 change when a specific word is found in the sentence extracted with the regexes.
The code I found to add a hyperlink doesn't let me narrow the range to the very sentence found and has me use the Activedocument.range, so the entire doc content.
With the code hereunder, all occurrences of say 23/25 will end up with the same hyperlink.
Is there a way to set/narrow the range specifically to the sentence found with the regexes?
I know the code hereunder is definitely not properly written, I'm struggling here. Thanks for your kind help in advance.
Set objRegex = New RegExp
With objRegex
.Pattern = "(\d{2}/\d{2})([a-zA-Zé /'/., ]{2,250})"
.Global = True
.IgnoreCase = True
Set matches = .Execute(Txte)
For Each fnd In matches
a = fnd.SubMatches.Count
For i = 0 To a - 1
If InStr(fnd, "cdtional word") Then
resul = fnd.SubMatches.Item(1)
Set Rng = ActiveDocument.Range{resul}
With Rng.Find
Do While .Execute(findText:=resul, Forward:=False) = True
Rng.MoveEndUntil (" ")
ActiveDocument.Hyperlinks.Add _
Anchor:=Rng, _
Address:="https://bla.org/" & resul
Rng.Collapse wdCollapseStart
Loop`
(closing tags)
</details>
# 答案1
**得分**: 0
您的代码不完整,请修复它。谢谢。我只能使用您提供的内容,并临时处理它作为答案。如果不满足您的需求,请原谅我。
而且我没有用于测试的文本,所以无法进行实际测试。就像这些行一样,我不知道您在做什么:
```vba
If InStr(fnd, "cdtional word") Then
resul = fnd.SubMatches.Item(1)
所以我认为代码应该是这样的:
Sub VBA__MSWORD__Narrowing_the_range_to_add_Hyperlinks()
Dim objRegex As VBScript_RegExp_55.RegExp, matches As VBScript_RegExp_55.MatchCollection, fnd As VBScript_RegExp_55.match
Dim Txte As String, d As Document, resul As String, a As Long, i As Long, rng As Range
Set d = ActiveDocument
Txte = d.Content.Text
Set objRegex = New RegExp
With objRegex
.Pattern = "(\d{2}/\d{2})([a-zA-Zé /'., ]{2,250})"
.Global = True
.IgnoreCase = True
Set matches = .Execute(Txte)
End With
For Each fnd In matches
a = fnd.SubMatches.Count
For i = 0 To a - 1
If InStr(fnd, "cdtional word") Then
resul = fnd.SubMatches.Item(1)
Set rng = d.Range
With rng.Find
Do While .Execute(findText:=resul, Forward:=True, Wrap:=wdFindStop) = True
rng.MoveEndUntil (" ")
rng.Hyperlinks.Add _
Anchor:=rng, _
Address:="https://bla.org/" & resul
rng.Collapse wdCollapseEnd
If rng.Next Is Nothing Then Exit Do
rng.SetRange rng.Next.Start, d.Content.End
Loop
End With
End If
Next i
Next fnd
End Sub
您应该根据自己的实际需求修改我的代码。
英文:
Your code is incomplete, please fix it. Thanks. I can only use what you have given and deal with it temporarily as an answer. If it does not meet your needs, please forgive me.
And I don't have a text for testing, so I can't do the actual testing. As in these lines, I don't know what you are doing:
If InStr(fnd, "cdtional word") Then
resul = fnd.SubMatches.Item(1)
So the code I suppose should be:
Sub VBA__MSWORD__Narrowing_the_range_to_add_Hyperlinks()
Dim objRegex As VBScript_RegExp_55.RegExp, matches As VBScript_RegExp_55.MatchCollection, fnd As VBScript_RegExp_55.match
Dim Txte As String, d As Document, resul As String, a As Long, i As Long, rng As Range
Set d = ActiveDocument
Txte = d.Content.Text
Set objRegex = New RegExp
With objRegex
.Pattern = "(\d{2}/\d{2})([a-zA-Zé /'/., ]{2,250})"
.Global = True
.IgnoreCase = True
Set matches = .Execute(Txte)
End With
For Each fnd In matches
a = fnd.SubMatches.Count
For i = 0 To a - 1
If InStr(fnd, "cdtional word") Then
resul = fnd.SubMatches.Item(1)
' Set Rng = ActiveDocument.Range{resul}
Set rng = d.Range
With rng.Find
'Do While .Execute(findText:=resul, Forward:=False) = True
Do While .Execute(findText:=resul, Forward:=True, Wrap:=wdFindStop) = True
rng.MoveEndUntil (" ")
' ActiveDocument.Hyperlinks.Add _
' Anchor:=rng, _
' Address:="https://bla.org/" & resul
' rng.Collapse wdCollapseStart
Rem Try to use rng instead of ActiveDocument
rng.Hyperlinks.Add _
Anchor:=rng, _
Address:="https://bla.org/" & resul
rng.Collapse wdCollapseEnd
If rng.Next Is Nothing Then Exit Do
Rem Reset the range to be searched:
rng.SetRange rng.Next.Start, d.Content.End
Loop
End With
End If
Next i
Next fnd
End Sub
You should modify my code to apply according to your actual needs.
20230603 Without Find object
No need to use the Find object
- the Test File
- How to download the .docm (the MS Word document file with VBA code) file from Google Drive.
Option Explicit
'Private linkDict As New Scripting.Dictionary
Private linkDict As Object
Rem https://stackoverflow.com/questions/76386857/vba-msword-narrowing-the-range-to-add-hyperlinks
Rem Dear Omar: the sample text and result is this : Document 25/23 from company X is to be reviewed. Document 25/23 from Company Z as well. –
Rem I just need the VBA to 1) look for all sentences containing 25/23 , 2) add 1 hyperlink to 25/23 : if the word "Company X" is in that sentence, the hyperlink has to be Http://docs.org/Startups/25/23. But if the word "Company Z" is in that sentence, then the hyperlink has to be Https://docs.org/ngos/25/23 instead. That is all I need really –
Rem All string comparisons are case-sensitive
Sub VBA_MSWORD_Narrowing_the_range_to_add_Hyperlinks()
Dim objRegex As VBScript_RegExp_55.RegExp, matches As VBScript_RegExp_55.MatchCollection, fnd As VBScript_RegExp_55.match
Dim Txte As String, d As Document, resul As String, a As Long, i As Long, rng As Range, ur As UndoRecord, s As Long
Set ur = Word.Application.UndoRecord
ur.StartCustomRecord "VBA_MSWORD_Narrowing_the_range_to_add_Hyperlinks"
Set d = ActiveDocument
Txte = d.Content.Text
Set rng = d.Range
Set objRegex = New RegExp 'Set regex = CreateObject("VBScript.RegExp")
Research:
With objRegex
' .Pattern = "(\d{2}/\d{2})([a-zA-Zé /'/., ]{2,250})"
.Pattern = "(\d{2}/\d{2})([a-zA-Zé /'/., ]{2,250})(Company [A-Z])"
.Global = True
.IgnoreCase = True
'.MultiLine=False 'default
Set matches = .Execute(Txte)
End With
For Each fnd In matches
a = fnd.SubMatches.Count
For i = 0 To a - 1
' If InStr(fnd, "Company ") Then ' this can be omitted because regex is already applying to
resul = fnd.SubMatches.Item(2)
With rng
.SetRange fnd.FirstIndex + s, fnd.FirstIndex + s + VBA.Len(fnd.SubMatches.Item(0))
'.Select 'just for test watching only
.Hyperlinks.Add _
Anchor:=rng, _
Address:=GetLink(resul)
'rng.Collapse wdCollapseEnd
'If rng.Next Is Nothing Then Exit Sub to end this procedure
If rng.Next Is Nothing Then
ur.EndCustomRecord
Exit Sub
End If
Rem Reset the range to be searched:
s = rng.Next.Start
rng.SetRange s, d.Content.End
Txte = rng.Text
GoTo Research
End With
' End If
Next i
Next fnd
ur.EndCustomRecord
End Sub
Property Get GetLink(company As String) As String
'If linkDict.Count = 0 Then
If linkDict Is Nothing Then
Set linkDict = CreateObject("Scripting.Dictionary")
With linkDict
.Add "Company X", "http://docs.org/Startups/25/23" ' "http"? did you lost the "s" ?
.Add "Company Z", "https://docs.org/ngos/25/23"
End With
End If
GetLink = linkDict(company)
End Property
Is this what you really want? If there are other relevant details you did not or forgot to mention, you should try to make adjustments yourself. I should have done already my best to fulfill what you said to me.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论