![]() |
#1
|
|||
|
|||
![]()
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 Cheers |
#2
|
|||
|
|||
![]()
I don't' have time to code this for you, but the first thing I would do is abandon the selection approach:
Code:
Sub Z_BCR3ContentNewX() Dim Reference As String Dim strTopLine As String Dim HOR As String Dim ToAll As String Dim oRng As Word.Range Dim oFld As Field Set oRng = Selection.Range With oRng With .Font .Size = 10 .Name = "Arial" End With strTopLine = InputBox("Top Line?, If blank remember to remove line", "") If strTopLine <> vbNullString Then .Text = strTopLine & vbCr .Paragraphs.Last.SpaceAfter = 12 End If .Collapse wdCollapseEnd Set oFld = Fields.Add(oRng, wdFieldEmpty, "MERGEFIELD LQCASE_NAME", True) .End = oFld.Result.End .Collapse wdCollapseEnd .Text = vbCr .Collapse wdCollapseEnd Set oFld = Fields.Add(oRng, wdFieldEmpty, "MERGEFIELD ADD", True) .End = oFld.Result.End .Collapse wdCollapseEnd .Text = vbCr .Paragraphs.Last.SpaceAfter = 12 .Collapse wdCollapseEnd HOR = InputBox("Highlighted OR?", "") If HOR <> vbNullString Then .Text = HOR & vbCr .HighlightColorIndex = wdTurquoise .Paragraphs.Last.SpaceAfter = 36 End If .Collapse wdCollapseEnd .Text = "Our Ref:" .Bold = True .Collapse wdCollapseEnd 'and so on End With |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
AllusiveKitten | Word | 2 | 07-20-2015 04:00 PM |
Highlighting Text After Sync | lvautier | OneNote | 1 | 02-07-2015 04:02 PM |
![]() |
magpie | Word | 7 | 01-24-2012 06:21 AM |
![]() |
icsjohn | Word VBA | 2 | 12-07-2011 06:44 PM |
HELP: Automatically highlighting problem | unhappyuser | PowerPoint | 0 | 05-03-2011 02:52 PM |