[dba-Tech] Word VBA question

Susan Harkins ssharkins at gmail.com
Sat Aug 31 07:40:54 CDT 2019


Shamil! How are you???

I haven't been back to that macro since I posted that -- I will take a look.
Thank you!

Susan H. 


Hi Susan --

I'm not sure what method do you use to find the abbreviation - here are two
possible solution cases:

*** First one ***

Sub Macro1()
Dim searchWord As String
searchWord = "BBB"
Dim sel As word.Selection
Set sel = word.Application.Selection
sel.ClearFormatting
With sel.Find
.Text = searchWord
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Dim wordFound As Boolean
wordFound = sel.Find.Execute
If wordFound Then
Dim wordLength As Integer
wordLength = Strings.Len(searchWord)
Dim wdoc As word.Document
Set wdoc = word.Application.ActiveDocument

Dim wordIndex As Integer
wordIndex = GetWordIndex(wdoc, sel.Words.First.Start) If (wordIndex > -1)
Then Dim rangeToCopy As word.Range Set rangeToCopy =
wdoc.Range(wdoc.Words(wordIndex - wordLength - 1).Start,
wdoc.Words(wordIndex - 1).Start) Dim strToCopy As String strToCopy =
Strings.Trim(rangeToCopy) Debug.Print "'" + strToCopy + "'"
End If
End If
End Sub
Function GetWordIndex(wdoc As word.Document, wordStartPosition As Integer)
As Integer Dim wordIndex As Integer wordFound = False For wordIndex = 1 To
wdoc.Words.Count If (wdoc.Words(wordIndex).Start = wordStartPosition) Then
GetWordIndex = wordIndex Exit Function End If Next wordIndex

GetWordIndex = -1
End Function
*** Second one ***

Sub Macro2()
Dim searchWord As String
searchWord = "BBB"

Dim wdoc As word.Document
Dim wordIndex As Integer
Dim wordFound As Boolean

Set wdoc = word.Application.ActiveDocument wordFound = False For wordIndex =
1 To wdoc.Words.Count If (wdoc.Words(wordIndex) = searchWord) Then wordFound
= True Exit For End If Next wordIndex

If wordFound Then
Dim wordLength As Integer
wordLength = Strings.Len(searchWord)
Dim rangeToCopy As word.Range
Set rangeToCopy = wdoc.Range(wdoc.Words(wordIndex - wordLength - 1).Start,
wdoc.Words(wordIndex - 1).Start) Dim strToCopy As String strToCopy =
Strings.Trim(rangeToCopy) Debug.Print "'" + strToCopy + "'"

End If
End Sub HTH.

-- Shamil

P.S. I'm a rare guest here these days - if you have any questions on the
subject please e-mail me directly.


>Tuesday, August  6, 2019 12:21 AM +03:00 from Susan Harkins
<ssharkins at gmail.com>:
>
>Hi everyone!
>
>I need to copy a range to a new Word document, but it's a complicated 
>search. When the search pattern find a match, I need to select the 
>words to the left -- using the length of the match (which can change, 
>I'm using wildcards). I'm stumped.
>
>An example: 
>
>The search string matches (BBB) at the text, Better Business Bureau 
>(BBB). I need to copy the definition of the acronym, to the left of 
>BBB, and the acronym. The acronym is easy. But I can't figure out for 
>the life of me how to set a range to "three words to the left of BBB".
>
>I'd like to avoid using Selection because the editors will be working 
>with big documents. If that's the only way it can be done, that's the 
>route I'll go. But I'd rather set a range -- just can' figure out how to do
it.
>
>Thanks!
>Susan H. 
>
>_______________________________________________
>dba-Tech mailing list
>dba-Tech at databaseadvisors.com
>http://databaseadvisors.com/mailman/listinfo/dba-tech
>Website:  http://www.databaseadvisors.com


--
Salakhetdinov Shamil
_______________________________________________
dba-Tech mailing list
dba-Tech at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-tech
Website: http://www.databaseadvisors.com



More information about the dba-Tech mailing list