Word-VBA: apply shading in specific Range?
I create a function for finding a text in a range of document and apply a shading on the text if found.
I running the code as follow, but it will find whole document text and apply a shading.
The document like :
How to make the function work? Thanks!
Public Function myFun_findTxt2addShading( _
str_findTxt As String, _
range_myRange, _
str_repTxt As String, _
str_ShadingColor As String) As Boolean
Dim boolean_checkFound As Boolean
boolean_checkFound = False
range_myRange.Select
With Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find.Text = str_findTxt
.Find.Replacement.Text = str_repTxt
.Find.Forward = True
.Find.Replacement.Font.ColorIndex = str_RepFontColor
.Find.Wrap = wdFindStop
Do While .Find.Execute
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = str_ShadingColor
boolean_check = True
Loop
.Find.Format = False
.Find.MatchCase = False
.Find.MatchWholeWord = False
.Find.MatchByte = False
.Find.MatchWildcards = False
.Find.MatchSoundsLike = False
.Find.MatchAllWordForms = False
End With
findTxt_Shading = boolean_checkFound
End Function
Sub test()
With Selection
.HomeKey Unit:=wdStory
.Find.Execute findText:="bookmark1", Forward:=True, Wrap:=wdFindStop
.MoveDown Unit:=wdLine
.HomeKey Unit:=wdLine
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="sybStart"
.Find.Execute findText:="bookmark2", Forward:=True, Wrap:=wdFindStop
.HomeKey Unit:=wdLine
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="sybEnd"
End With
Set sybRange = ActiveDocument.Range
sybRange.Start = sybRange.Bookmarks("sybStart").Range.End
sybRange.End = sybRange.Bookmarks("sybEnd").Range.Start
a = myFun_findTxt2addShading("pp", sybRange, "pp", wdColorYellow)
End Sub
vba ms-word
add a comment |
I create a function for finding a text in a range of document and apply a shading on the text if found.
I running the code as follow, but it will find whole document text and apply a shading.
The document like :
How to make the function work? Thanks!
Public Function myFun_findTxt2addShading( _
str_findTxt As String, _
range_myRange, _
str_repTxt As String, _
str_ShadingColor As String) As Boolean
Dim boolean_checkFound As Boolean
boolean_checkFound = False
range_myRange.Select
With Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find.Text = str_findTxt
.Find.Replacement.Text = str_repTxt
.Find.Forward = True
.Find.Replacement.Font.ColorIndex = str_RepFontColor
.Find.Wrap = wdFindStop
Do While .Find.Execute
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = str_ShadingColor
boolean_check = True
Loop
.Find.Format = False
.Find.MatchCase = False
.Find.MatchWholeWord = False
.Find.MatchByte = False
.Find.MatchWildcards = False
.Find.MatchSoundsLike = False
.Find.MatchAllWordForms = False
End With
findTxt_Shading = boolean_checkFound
End Function
Sub test()
With Selection
.HomeKey Unit:=wdStory
.Find.Execute findText:="bookmark1", Forward:=True, Wrap:=wdFindStop
.MoveDown Unit:=wdLine
.HomeKey Unit:=wdLine
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="sybStart"
.Find.Execute findText:="bookmark2", Forward:=True, Wrap:=wdFindStop
.HomeKey Unit:=wdLine
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="sybEnd"
End With
Set sybRange = ActiveDocument.Range
sybRange.Start = sybRange.Bookmarks("sybStart").Range.End
sybRange.End = sybRange.Bookmarks("sybEnd").Range.Start
a = myFun_findTxt2addShading("pp", sybRange, "pp", wdColorYellow)
End Sub
vba ms-word
1
What's theSelection.Range
you're giving it? Does it represent the specificSelection
you mean to work with? Please edit your post to fill in the blanks, there's currently not enough information to make a good Minimal, Complete, and Verifiable example.
– Mathieu Guindon
Nov 12 '18 at 15:39
add a comment |
I create a function for finding a text in a range of document and apply a shading on the text if found.
I running the code as follow, but it will find whole document text and apply a shading.
The document like :
How to make the function work? Thanks!
Public Function myFun_findTxt2addShading( _
str_findTxt As String, _
range_myRange, _
str_repTxt As String, _
str_ShadingColor As String) As Boolean
Dim boolean_checkFound As Boolean
boolean_checkFound = False
range_myRange.Select
With Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find.Text = str_findTxt
.Find.Replacement.Text = str_repTxt
.Find.Forward = True
.Find.Replacement.Font.ColorIndex = str_RepFontColor
.Find.Wrap = wdFindStop
Do While .Find.Execute
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = str_ShadingColor
boolean_check = True
Loop
.Find.Format = False
.Find.MatchCase = False
.Find.MatchWholeWord = False
.Find.MatchByte = False
.Find.MatchWildcards = False
.Find.MatchSoundsLike = False
.Find.MatchAllWordForms = False
End With
findTxt_Shading = boolean_checkFound
End Function
Sub test()
With Selection
.HomeKey Unit:=wdStory
.Find.Execute findText:="bookmark1", Forward:=True, Wrap:=wdFindStop
.MoveDown Unit:=wdLine
.HomeKey Unit:=wdLine
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="sybStart"
.Find.Execute findText:="bookmark2", Forward:=True, Wrap:=wdFindStop
.HomeKey Unit:=wdLine
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="sybEnd"
End With
Set sybRange = ActiveDocument.Range
sybRange.Start = sybRange.Bookmarks("sybStart").Range.End
sybRange.End = sybRange.Bookmarks("sybEnd").Range.Start
a = myFun_findTxt2addShading("pp", sybRange, "pp", wdColorYellow)
End Sub
vba ms-word
I create a function for finding a text in a range of document and apply a shading on the text if found.
I running the code as follow, but it will find whole document text and apply a shading.
The document like :
How to make the function work? Thanks!
Public Function myFun_findTxt2addShading( _
str_findTxt As String, _
range_myRange, _
str_repTxt As String, _
str_ShadingColor As String) As Boolean
Dim boolean_checkFound As Boolean
boolean_checkFound = False
range_myRange.Select
With Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
.Find.Text = str_findTxt
.Find.Replacement.Text = str_repTxt
.Find.Forward = True
.Find.Replacement.Font.ColorIndex = str_RepFontColor
.Find.Wrap = wdFindStop
Do While .Find.Execute
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = str_ShadingColor
boolean_check = True
Loop
.Find.Format = False
.Find.MatchCase = False
.Find.MatchWholeWord = False
.Find.MatchByte = False
.Find.MatchWildcards = False
.Find.MatchSoundsLike = False
.Find.MatchAllWordForms = False
End With
findTxt_Shading = boolean_checkFound
End Function
Sub test()
With Selection
.HomeKey Unit:=wdStory
.Find.Execute findText:="bookmark1", Forward:=True, Wrap:=wdFindStop
.MoveDown Unit:=wdLine
.HomeKey Unit:=wdLine
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="sybStart"
.Find.Execute findText:="bookmark2", Forward:=True, Wrap:=wdFindStop
.HomeKey Unit:=wdLine
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="sybEnd"
End With
Set sybRange = ActiveDocument.Range
sybRange.Start = sybRange.Bookmarks("sybStart").Range.End
sybRange.End = sybRange.Bookmarks("sybEnd").Range.Start
a = myFun_findTxt2addShading("pp", sybRange, "pp", wdColorYellow)
End Sub
vba ms-word
vba ms-word
edited Nov 13 '18 at 10:49
Cindy Meister
15.5k102337
15.5k102337
asked Nov 12 '18 at 15:00
user3901528user3901528
3215
3215
1
What's theSelection.Range
you're giving it? Does it represent the specificSelection
you mean to work with? Please edit your post to fill in the blanks, there's currently not enough information to make a good Minimal, Complete, and Verifiable example.
– Mathieu Guindon
Nov 12 '18 at 15:39
add a comment |
1
What's theSelection.Range
you're giving it? Does it represent the specificSelection
you mean to work with? Please edit your post to fill in the blanks, there's currently not enough information to make a good Minimal, Complete, and Verifiable example.
– Mathieu Guindon
Nov 12 '18 at 15:39
1
1
What's the
Selection.Range
you're giving it? Does it represent the specific Selection
you mean to work with? Please edit your post to fill in the blanks, there's currently not enough information to make a good Minimal, Complete, and Verifiable example.– Mathieu Guindon
Nov 12 '18 at 15:39
What's the
Selection.Range
you're giving it? Does it represent the specific Selection
you mean to work with? Please edit your post to fill in the blanks, there's currently not enough information to make a good Minimal, Complete, and Verifiable example.– Mathieu Guindon
Nov 12 '18 at 15:39
add a comment |
1 Answer
1
active
oldest
votes
Do yourself some favors.
Put 'option explicit' at the top of each module.
In the VBA IDE go Tools.Options.Editor and make sure all the boxes in the Code Settings group are ticked.
In the VBA IDE, placing the cursor on a Keyword and pressing F1 brings up the MS help page for that keyword. Try it for the .Find method.
I tidied up your code a little and used more sensible naming (only a little more sensible). The code below will now highlight each word in the selection in the document.
Please note that I have deliberately used two with groupings so that you can take other actions each time the findTxt is found. If you just wanted to highlight the text you could omit the second With group and change .Format from False to True.
Public Function AddShadingToFoundText( _
findTxt As String, _
repTxt As String, _
ShadingColor As WdColor) As Boolean
Dim findTxtFound As Boolean
findTxtFound = False
If myRange.Characters.Count < Len(findTxt) Then
' No point in searching if the selected text is
' smaller than the search text.
Exit Function
End if
With myRange.Duplicate
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = findTxt
.Replacement.Text = findTxt
.Forward = True
' str_RepFontColor
'.Find.Replacement.Font.ColorIndex = str_RepFontColor
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
' Make sure there is still room for the search text
Do While .Find.Found And .Start < myRange.End - Len(findTxt)
.Shading.Texture = wdTextureNone
.Shading.ForegroundPatternColor = WdColor.wdColorAutomatic
.Shading.BackgroundPatternColor = ShadingColor
.Collapse Direction:=wdCollapseEnd
.Move unit:=wdCharacter, Count:=1
.Find.Execute
findTxtFound = True
Loop
End With
AddShadingToFoundText = findTxtFound
End Function
Sub test()
Dim a As Boolean
a = AddShadingToFoundText("row", Selection.Range, "row", WdColor.wdColorRed)
End Sub
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264820%2fword-vba-apply-shading-in-specific-range%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Do yourself some favors.
Put 'option explicit' at the top of each module.
In the VBA IDE go Tools.Options.Editor and make sure all the boxes in the Code Settings group are ticked.
In the VBA IDE, placing the cursor on a Keyword and pressing F1 brings up the MS help page for that keyword. Try it for the .Find method.
I tidied up your code a little and used more sensible naming (only a little more sensible). The code below will now highlight each word in the selection in the document.
Please note that I have deliberately used two with groupings so that you can take other actions each time the findTxt is found. If you just wanted to highlight the text you could omit the second With group and change .Format from False to True.
Public Function AddShadingToFoundText( _
findTxt As String, _
repTxt As String, _
ShadingColor As WdColor) As Boolean
Dim findTxtFound As Boolean
findTxtFound = False
If myRange.Characters.Count < Len(findTxt) Then
' No point in searching if the selected text is
' smaller than the search text.
Exit Function
End if
With myRange.Duplicate
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = findTxt
.Replacement.Text = findTxt
.Forward = True
' str_RepFontColor
'.Find.Replacement.Font.ColorIndex = str_RepFontColor
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
' Make sure there is still room for the search text
Do While .Find.Found And .Start < myRange.End - Len(findTxt)
.Shading.Texture = wdTextureNone
.Shading.ForegroundPatternColor = WdColor.wdColorAutomatic
.Shading.BackgroundPatternColor = ShadingColor
.Collapse Direction:=wdCollapseEnd
.Move unit:=wdCharacter, Count:=1
.Find.Execute
findTxtFound = True
Loop
End With
AddShadingToFoundText = findTxtFound
End Function
Sub test()
Dim a As Boolean
a = AddShadingToFoundText("row", Selection.Range, "row", WdColor.wdColorRed)
End Sub
add a comment |
Do yourself some favors.
Put 'option explicit' at the top of each module.
In the VBA IDE go Tools.Options.Editor and make sure all the boxes in the Code Settings group are ticked.
In the VBA IDE, placing the cursor on a Keyword and pressing F1 brings up the MS help page for that keyword. Try it for the .Find method.
I tidied up your code a little and used more sensible naming (only a little more sensible). The code below will now highlight each word in the selection in the document.
Please note that I have deliberately used two with groupings so that you can take other actions each time the findTxt is found. If you just wanted to highlight the text you could omit the second With group and change .Format from False to True.
Public Function AddShadingToFoundText( _
findTxt As String, _
repTxt As String, _
ShadingColor As WdColor) As Boolean
Dim findTxtFound As Boolean
findTxtFound = False
If myRange.Characters.Count < Len(findTxt) Then
' No point in searching if the selected text is
' smaller than the search text.
Exit Function
End if
With myRange.Duplicate
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = findTxt
.Replacement.Text = findTxt
.Forward = True
' str_RepFontColor
'.Find.Replacement.Font.ColorIndex = str_RepFontColor
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
' Make sure there is still room for the search text
Do While .Find.Found And .Start < myRange.End - Len(findTxt)
.Shading.Texture = wdTextureNone
.Shading.ForegroundPatternColor = WdColor.wdColorAutomatic
.Shading.BackgroundPatternColor = ShadingColor
.Collapse Direction:=wdCollapseEnd
.Move unit:=wdCharacter, Count:=1
.Find.Execute
findTxtFound = True
Loop
End With
AddShadingToFoundText = findTxtFound
End Function
Sub test()
Dim a As Boolean
a = AddShadingToFoundText("row", Selection.Range, "row", WdColor.wdColorRed)
End Sub
add a comment |
Do yourself some favors.
Put 'option explicit' at the top of each module.
In the VBA IDE go Tools.Options.Editor and make sure all the boxes in the Code Settings group are ticked.
In the VBA IDE, placing the cursor on a Keyword and pressing F1 brings up the MS help page for that keyword. Try it for the .Find method.
I tidied up your code a little and used more sensible naming (only a little more sensible). The code below will now highlight each word in the selection in the document.
Please note that I have deliberately used two with groupings so that you can take other actions each time the findTxt is found. If you just wanted to highlight the text you could omit the second With group and change .Format from False to True.
Public Function AddShadingToFoundText( _
findTxt As String, _
repTxt As String, _
ShadingColor As WdColor) As Boolean
Dim findTxtFound As Boolean
findTxtFound = False
If myRange.Characters.Count < Len(findTxt) Then
' No point in searching if the selected text is
' smaller than the search text.
Exit Function
End if
With myRange.Duplicate
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = findTxt
.Replacement.Text = findTxt
.Forward = True
' str_RepFontColor
'.Find.Replacement.Font.ColorIndex = str_RepFontColor
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
' Make sure there is still room for the search text
Do While .Find.Found And .Start < myRange.End - Len(findTxt)
.Shading.Texture = wdTextureNone
.Shading.ForegroundPatternColor = WdColor.wdColorAutomatic
.Shading.BackgroundPatternColor = ShadingColor
.Collapse Direction:=wdCollapseEnd
.Move unit:=wdCharacter, Count:=1
.Find.Execute
findTxtFound = True
Loop
End With
AddShadingToFoundText = findTxtFound
End Function
Sub test()
Dim a As Boolean
a = AddShadingToFoundText("row", Selection.Range, "row", WdColor.wdColorRed)
End Sub
Do yourself some favors.
Put 'option explicit' at the top of each module.
In the VBA IDE go Tools.Options.Editor and make sure all the boxes in the Code Settings group are ticked.
In the VBA IDE, placing the cursor on a Keyword and pressing F1 brings up the MS help page for that keyword. Try it for the .Find method.
I tidied up your code a little and used more sensible naming (only a little more sensible). The code below will now highlight each word in the selection in the document.
Please note that I have deliberately used two with groupings so that you can take other actions each time the findTxt is found. If you just wanted to highlight the text you could omit the second With group and change .Format from False to True.
Public Function AddShadingToFoundText( _
findTxt As String, _
repTxt As String, _
ShadingColor As WdColor) As Boolean
Dim findTxtFound As Boolean
findTxtFound = False
If myRange.Characters.Count < Len(findTxt) Then
' No point in searching if the selected text is
' smaller than the search text.
Exit Function
End if
With myRange.Duplicate
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = findTxt
.Replacement.Text = findTxt
.Forward = True
' str_RepFontColor
'.Find.Replacement.Font.ColorIndex = str_RepFontColor
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
' Make sure there is still room for the search text
Do While .Find.Found And .Start < myRange.End - Len(findTxt)
.Shading.Texture = wdTextureNone
.Shading.ForegroundPatternColor = WdColor.wdColorAutomatic
.Shading.BackgroundPatternColor = ShadingColor
.Collapse Direction:=wdCollapseEnd
.Move unit:=wdCharacter, Count:=1
.Find.Execute
findTxtFound = True
Loop
End With
AddShadingToFoundText = findTxtFound
End Function
Sub test()
Dim a As Boolean
a = AddShadingToFoundText("row", Selection.Range, "row", WdColor.wdColorRed)
End Sub
edited Nov 12 '18 at 18:14
CharlesPL
694
694
answered Nov 12 '18 at 17:25
FreeflowFreeflow
547128
547128
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264820%2fword-vba-apply-shading-in-specific-range%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What's the
Selection.Range
you're giving it? Does it represent the specificSelection
you mean to work with? Please edit your post to fill in the blanks, there's currently not enough information to make a good Minimal, Complete, and Verifiable example.– Mathieu Guindon
Nov 12 '18 at 15:39