View Single Post
 
Old 02-25-2016, 03:01 AM
ChrisJ83 ChrisJ83 is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Nov 2015
Posts: 9
ChrisJ83 is on a distinguished road
Default Problem with highlighting text within a Macro

Hi all

I am trying to highlight one word which is entered via the Macro shown.

To simply if i enter "OR" in the HOR input and "TO ALL KNOWN CREDITORS" in the ToAll input I want OR to be highlighted and the TO ALL KNOWN CREDITORS to have no highlighting.

This works fine if the "ToAll" input is one word, if I enter multiple words like 4 for example for the "ToAll" the first 3 words are also highlighted and the 4th is not highlighted.

If I enter only one word for "ToAll" it isn't highlighted which is how I want it to behave with whatever is input whether that be a single word or multiple.

I have shown the code below in Bold which is having the issue, I assume I need to identify the range I want highlighting but I am unaware of how I would do that, I find it strange it works if I just use one word.

Code:
Sub Z_BCR3ContentNew()
'
' Z_BCR3ContentNew Macro
'
'
    Selection.Style = ActiveDocument.Styles("Normal")
    Dim Reference As String
    Reference = InputBox("Reference?", "")
    Dim TopLine As String
    TopLine = InputBox("Top Line?  If blank remember to remove line", "")
    Dim HOR As String
    HOR = InputBox("Highlighted OR?  If blank remember to remove line", "")
    Dim ToAll As String
    ToAll = InputBox("To all known xxxxx?  If blank remember to remove line", "")
    Selection.Font.Name = "Arial"
    Selection.Font.Size = 10
    Selection.TypeText Text:=TopLine
    Selection.TypeParagraph
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "MERGEFIELD LQCASE_NAME", PreserveFormatting:=True
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
    Selection.TypeParagraph
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "MERGEFIELD ADD", PreserveFormatting:=True
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
    Selection.TypeParagraph
    Selection.Range.HighlightColorIndex = wdNoHighlight
    Selection.TypeParagraph
    Selection.TypeText Text:=HOR
    Selection.Range.HighlightColorIndex = wdTurquoise
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.Font.Bold = wdToggle
    Selection.TypeText Text:=ToAll
    Selection.Range.HighlightColorIndex = wdNoHighlight
    Selection.TypeParagraph
    Selection.Font.Bold = wdToggle
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeText Text:="Our Ref:      "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "MERGEFIELD LQCASE_MAN_SEN", PreserveFormatting:=True
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
    Selection.TypeText Text:="/"
    Selection.TypeText Text:=Reference
    Selection.TypeText Text:="/"
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "MERGEFIELD LQCASE_CASECODE", PreserveFormatting:=True
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeText Text:="Your Ref:     "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "MERGEFIELD CREF", PreserveFormatting:=True
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "DATE  \@ ""dd MMMM yyyy"" ", PreserveFormatting:=True
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
End Sub
Any help/advice appreciated.
Cheers
Reply With Quote