PDA

View Full Version : [SOLVED:] Using a highlighted string (a word + an integer in ()) in searching as findtext



ImreAga
02-22-2022, 03:10 AM
I'm trying to create a Word 2016 macro that uses a highlighted word (called Headword in my macro) for subsequent (repeated) searching as .Find Text. The searching is to be repeated as far as the highlighted word occurs. During recording of the macro, I've tried to use the highlighted word as Selection for substituting  the Text in Find but it wouldn’t work anyhow. In other words, whatever I tried nothing was recorded in my macro for the find operation even though the cursor moved and highlighted the found word. Can anybody tell me how to record a Find text operation in a Word macro or else tell me what the correct VBA code is to do this? Thanks in advance! Here's the beginning and the problematic part of my macro:



Sub Összevonó()
'
' Összevonó Makró / Joining Entries
' Option Explicit

Dim Headword As Variable
'...

'Highlighting (selecting)the found Headword that looks like "a word(an integer)".


Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Headword = Selection
Selection.Text = Headword
Selection.MoveRight Unit:=wdCharacter, Count:=1

' Repeate serching of the same Headword.


While Selection.Find.Found = True
Selection.Find.ClearFormatting
With Selection.Find
.Text = Headword
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Selection.Find.Execute
If Selection.Find.Found = False Then GoTo Start

'If the recent Headword is found, it is deleted and by deleting the previous parahraph-sign the two entries joins.


Selection.Expand wdWord
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeBackspace

Wend

Ending:


End Sub

ImreAga
02-22-2022, 07:00 AM
Hi, there,
Meanwhile I could solve the problem. Sorry for tirering anybody. But if somebody could offer a simplier and more professional solution, welcome. (Me, as an amateur struggled with the issue almost for a week!)
With best wishes,
ImreAga