MacroButton Borders and Shading

D

Dave

When I create a MacroButton manually, I can format borders and
shading, then apply them to the text which makes the borders and
shading just surround the text.

When I record a macro which creates a MacroButton, I do not have the
option of applying borders and shading to text, only to paragraph.
Applying to the paragraph makes the shaded area go from margin to
margin. This can be confusing to the user as the macrobutton only
works when they double-click on the text.

What is the code I need to put in my macro to apply the borders and
shading to the text? I suspect it's a variation on "With
Selection.ParagraphFormat" but I can't find the correct option.

thanks in advance,

dave
 
J

Jean-Guy Marcil

Dave said:
When I create a MacroButton manually, I can format borders and
shading, then apply them to the text which makes the borders and
shading just surround the text.

When I record a macro which creates a MacroButton, I do not have the
option of applying borders and shading to text, only to paragraph.
Applying to the paragraph makes the shaded area go from margin to
margin. This can be confusing to the user as the macrobutton only
works when they double-click on the text.

What is the code I need to put in my macro to apply the borders and
shading to the text? I suspect it's a variation on "With
Selection.ParagraphFormat" but I can't find the correct option.

It cannot be ParagraphFormat, you want a border around specific text, not
the whole paragrpah...

For simple things like this, the macro recorder is useful to give you the
basic code you need, which is, for example:

With Selection.Font
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorGray125
End With
With .Borders(1)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
End With

All you need to do is adapt the code in order to use it with your range
object... I hope you are using a range object!
 
D

Dave

It cannot be ParagraphFormat, you want a border around specific text, not
the whole paragrpah...

For simple things like this, the macro recorder is useful to give you the
basic code you need, which is, for example:

With Selection.Font
    With .Shading
        .Texture = wdTextureNone
        .ForegroundPatternColor = wdColorAutomatic
        .BackgroundPatternColor = wdColorGray125
    End With
    With .Borders(1)
        .LineStyle = wdLineStyleSingle
        .LineWidth = wdLineWidth050pt
        .Color = wdColorAutomatic
    End With
End With

All you need to do is adapt the code in order to use it with your range
object... I hope you are using a range object!

Hi Guy,

Font is what I needed--I was looking for Text.

That gets me much closer but Word says that ."LineWidth =
wdLineWidth050pt" which is odd as that is what gets recorded. Also,
the shading and borders are not getting applied to the new
macrobutton. I'm just a recorder and adjuster, not a coder as you can
see from my clumbsy method or deleting the original macrobutton in the
code below.

Sub InsertPageBreak()

'Delete MacroButton that called this macro
Selection.Expand Unit:=wdParagraph
Selection.Delete
Selection.MoveLeft
Selection.TypeParagraph

' Insert the page break.
Selection.InsertBreak Type:=wdPageBreak

'Add a Remove Page Break MacroButton
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:= _
"MacroButton RemovePageBreak Double-Click to Remove Page
Break"
Selection.Fields.ToggleShowCodes

With Selection.Font
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorGray125
End With
With .Borders(1)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
End With

Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

End Sub

Thanks,

dave
 
J

Jean-Guy Marcil

Dave said:
Font is what I needed--I was looking for Text.

That gets me much closer but Word says that ."LineWidth =
wdLineWidth050pt" which is odd as that is what gets recorded. Also,

Why is it odd?
the shading and borders are not getting applied to the new
macrobutton. I'm just a recorder and adjuster, not a coder as you can
see from my clumbsy method or deleting the original macrobutton in the
code below.

When workiing with the Selection object, you have to make sure that the
selection is what you think it is... In this case, it isn't!
Hence the wrong results you observed...

This is why I try to work with range objects as much as possible... The
range object is more reliable and executes faster.
Try this code instead:


Option Explicit

Sub InsertPageBreak()

Dim rngMacroButton As Range

Set rngMacroButton = Selection.Range

With rngMacroButton
.Fields(1).Delete
.InsertBreak wdPageBreak
.Fields.Add rngMacroButton, wdFieldEmpty, _
"MacroButton RemovePageBreak Double-Click to " _
& "Remove Page Break", False
.MoveEnd wdWord, 1
With .Font
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorGray125
End With
With .Borders(1)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
End With
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Collapse wdCollapseEnd
.Select
End With

End Sub


Sub RemovePageBreak()

Dim rngMacroButton As Range

Set rngMacroButton = Selection.Range

With rngMacroButton
.Fields(1).Delete
.MoveStart wdCharacter, -1
.Delete
.Fields.Add rngMacroButton, wdFieldEmpty, _
"MacroButton InsertPageBreak Double-Click " _
& "to Add Page Break", False
.MoveEnd wdWord, 1
With .Font
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorGray125
End With
With .Borders(1)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
End With
.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Collapse wdCollapseEnd
.Select
End With

End Sub


But, if the user has field shading on (Tools > Options... > "View" tab >
Field Shading > "Always" or "When Selected"), the shading you apply with
these macros will not appear as you expect it...
 
D

Dave

Why is it odd?


When workiing with the Selection object, you have to make sure that the
selection is what you think it is... In this case, it isn't!
Hence the wrong results you observed...

This is why I try to work with range objects as much as possible... The
range object is more reliable and executes faster.
Try this code instead:

Option Explicit

Sub InsertPageBreak()

Dim rngMacroButton As Range

Set rngMacroButton = Selection.Range

With rngMacroButton
    .Fields(1).Delete
    .InsertBreak wdPageBreak
    .Fields.Add rngMacroButton, wdFieldEmpty, _
        "MacroButton RemovePageBreak Double-Click to " _
        & "Remove Page Break", False
    .MoveEnd wdWord, 1
    With .Font
        With .Shading
            .Texture = wdTextureNone
            .ForegroundPatternColor = wdColorAutomatic
            .BackgroundPatternColor = wdColorGray125
        End With
        With .Borders(1)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
    End With
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    .Collapse wdCollapseEnd
    .Select
End With

End Sub

Sub RemovePageBreak()

Dim rngMacroButton As Range

Set rngMacroButton = Selection.Range

With rngMacroButton
    .Fields(1).Delete
    .MoveStart wdCharacter, -1
    .Delete
    .Fields.Add rngMacroButton, wdFieldEmpty, _
        "MacroButton InsertPageBreak Double-Click " _
        & "to Add Page Break", False
    .MoveEnd wdWord, 1
    With .Font
        With .Shading
            .Texture = wdTextureNone
            .ForegroundPatternColor = wdColorAutomatic
            .BackgroundPatternColor = wdColorGray125
        End With
        With .Borders(1)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
    End With
    .ParagraphFormat.Alignment = wdAlignParagraphLeft
    .Collapse wdCollapseEnd
    .Select
End With

End Sub

But, if the user has field shading on (Tools > Options... > "View" tab >
Field Shading > "Always" or "When Selected"), the shading you apply with
these macros will not appear as you expect it...

Jean-Guy,

That works perfectly! I especially appreciate the second macro for
removing.

Thanks a bunch,

dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top