Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-25-2016, 03:01 AM
ChrisJ83 ChrisJ83 is offline Problem with highlighting text within a Macro Windows 7 64bit Problem with highlighting text within a Macro Office 2013
Novice
Problem with highlighting text within a Macro
 
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
  #2  
Old 02-25-2016, 09:09 AM
gmaxey gmaxey is offline Problem with highlighting text within a Macro Windows 7 32bit Problem with highlighting text within a Macro Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with highlighting text within a Macro Unwanted text highlighting AllusiveKitten Word 2 07-20-2015 04:00 PM
Highlighting Text After Sync lvautier OneNote 1 02-07-2015 04:02 PM
Problem with highlighting text within a Macro How to solve highlighting problem in Template magpie Word 7 01-24-2012 06:21 AM
Problem with highlighting text within a Macro Macro for highlighting specific number of words icsjohn Word VBA 2 12-07-2011 06:44 PM
HELP: Automatically highlighting problem unhappyuser PowerPoint 0 05-03-2011 02:52 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:25 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft