View Single Post
 
Old 10-31-2021, 10:38 AM
laith93 laith93 is offline Windows 10 Office 2019
Competent Performer
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Post Searching in main body document and text boxes at the same time

Hi,
This code was developed by Doug Robbins - MVP
Code:
Sub FormatScientificNamesByExcel()

Dim xlApp As Object
Dim xlbook As Object
Dim xlsheet As Object
Dim MyArray As Variant
Dim FD As FileDialog
Dim strSource As String
Dim i As Long, lognum As Long

Set FD = Application.FileDialog(msoFileDialogFilePicker)

With FD

.Title = "Select the workbook that contains the terms to be italicized"

.Filters.Clear

.Filters.Add "Excel Workbooks", "*.xlsx"

.AllowMultiSelect = False

If .Show = -1 Then

strSource = .SelectedItems(1)

Else

MsgBox "You did not select the workbook that contains the data"

Exit Sub

End If

End With

On Error Resume Next

Set xlApp = GetObject(, "Excel.Application")

If Err Then

bstartApp = True

Set xlApp = CreateObject("Excel.Application")

End If

On Error GoTo 0

Set xlbook = xlApp.Workbooks.Open(strSource)

Set xlsheet = xlbook.Worksheets(1)

MyArray = xlsheet.range("A1").CurrentRegion.Value

If bstartApp = True Then

xlApp.Quit

End If

Set xlApp = Nothing
Set xlbook = Nothing
Set xlsheet = Nothing

For i = LBound(MyArray) To UBound(MyArray)

Selection.HomeKey wdStory

Selection.Find.ClearFormatting

With Selection.Find

Do While .Execute(findText:=MyArray(i, 1), Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True

Set Rng = Selection.range

Selection.Collapse wdCollapseEnd

Rng.Font.Italic = True

Loop

End With

Next i

End Sub
This code searches for text (that is already present in the excel file), and changes the formatting of the founded text in the word file to italic.

The code works fine, but the only problem is searching in the main body document only, and it ignores the text in text boxes

Hence, I want to include text boxes also in searching

Mr. Andrew, I want your help please, as you fixed my problem in this post (same idea):
https://www.msofficeforums.com/163297-post20.html
if not possible with this code, any other codes to do my requirement?

You can download the sample and excel file in the attachment to apply on it.
Thank you
Attached Files
File Type: docx Sample.docx (23.4 KB, 6 views)
File Type: xlsx MicrobialNameDatabase.xlsx (11.0 KB, 6 views)
Reply With Quote