ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Finding and selecting text in word from excel (https://www.excelbanter.com/excel-programming/368536-finding-selecting-text-word-excel.html)

[email protected]

Finding and selecting text in word from excel
 
Hello--I have written a simple macro trying to find and select certain
text in a word document (it is part of a larger project). When the
macro is finished running, I would like word to have the first instance
of the text selected. However, the following code is not doing the job.
I am sure I am missing something simple. Can anyone help?
-Abe

Sub find()

Set wdapp = GetObject(, "Word.Application")
Set wddoc = wdapp.ActiveDocument

With wddoc.Content.find
.Text = "Q1"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchAllWordForms = False
.Execute (findtext = "Q1")
End With

End Sub


[email protected]

Finding and selecting text in word from excel
 
This one is actually embarrassingly simple. I am missing the statement
Selection.Find.Execute at the end of the find code. I guess the find
property needs an additional step to be completed. The working code
goes:


Set wdapp = GetObject(, "Word.Application")
Set wddoc = wdapp.ActiveDocument

With wddoc.Content.find
.Text = "Q1"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchAllWordForms = False
.Execute (findtext = "Q1")
End With

selection.find.execute
End Sub


wrote:
Hello--I have written a simple macro trying to find and select certain
text in a word document (it is part of a larger project). When the
macro is finished running, I would like word to have the first instance
of the text selected. However, the following code is not doing the job.
I am sure I am missing something simple. Can anyone help?
-Abe

Sub find()





All times are GMT +1. The time now is 10:21 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com