[dba-Tech] Word VBA question
Jim Lawrence
accessd at shaw.ca
Sat Aug 31 13:38:13 CDT 2019
Hi Shamil:
Longtime no see.
A fine piece of code.
Jim
----- Original Message -----
From: "Discussion of Hardware and Software issues" <dba-tech at databaseadvisors.com>
To: "Discussion of Hardware and Software issues" <dba-tech at databaseadvisors.com>
Cc: "Salakhetdinov Shamil" <mcp2004 at mail.ru>
Sent: Saturday, August 31, 2019 5:08:45 AM
Subject: Re: [dba-Tech] Word VBA question
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