ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Problem with running sub from Excel (https://www.excelbanter.com/excel-programming/427231-problem-running-sub-excel.html)

Raj[_2_]

Problem with running sub from Excel
 
Hi,

When I run the following sub from Word, it highlights all the instance
of the input string. However, when word is invoked from excel, a word
document opened and the sub is called, the word "find" is highlighted
and "Argument not optional" error appears.

Sub HighlightWord()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
rspFind = InputBox("Input the string for highlighting")
With rDcm.Find
.Text = rspFind
While .Execute
rDcm.HighlightColorIndex = wdYellow
Wend
End With

End Sub

Please help. Thanks in Advance.


Jacob Skaria

Problem with running sub from Excel
 
You should be having the Application object and the Document object for MS
Word; and try this.

wrdApp.Options.DefaultHighlightColorIndex = wdYellow
With wrdDoc.Range.Find
.Text = strSearchString
.Replacement.Text = strSearchString
.Replacement.Highlight = True
.MatchCase = False
.MatchWholeWord = False
.Execute Replace:=wdReplaceAll
End With

I have modified the code around find and highlight..and posted..Please check
and feedback..

If this post helps click Yes
---------------
Jacob Skaria


"Raj" wrote:

Hi,

When I run the following sub from Word, it highlights all the instance
of the input string. However, when word is invoked from excel, a word
document opened and the sub is called, the word "find" is highlighted
and "Argument not optional" error appears.

Sub HighlightWord()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
rspFind = InputBox("Input the string for highlighting")
With rDcm.Find
.Text = rspFind
While .Execute
rDcm.HighlightColorIndex = wdYellow
Wend
End With

End Sub

Please help. Thanks in Advance.




All times are GMT +1. The time now is 05:27 PM.

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