Share via

Word Autoexec Macro syntax

Anonymous
2022-04-12T20:36:37+00:00

Word has several command settings that I would like to change the default for. I just can't figure out the syntax.

.

I want the Paragraph Shading color to default to yellow and the Table Borders command to default to All Borders.

.

I want these commands set when I open a file.

I recorded a macro to create the commands, but they depend on something being selected. Is there any syntax I can use without selecting?

The original command for shading included "Selection.", that didn't work so tried dropping it, still didnt' work.

Or do I have to go through process of inserting a dummy object (a 1cell table) turning on the settings, then deleting the dummy table?

.

Sub AutoExec()
'
' AutoExec Macro
'
'
Shading.Texture = wdTextureNone
Shading.ForegroundPatternColor = wdColorAutomatic
Shading.BackgroundPatternColor = wdColorYellow

With Selection.Borders(wdBorderTop)  
    .LineStyle = wdLineStyleSingle  
    .LineWidth = Options.DefaultBorderLineWidth  
    .Color = Options.DefaultBorderColor  
End With  
With Selection.Borders(wdBorderLeft)  
    .LineStyle = wdLineStyleSingle  
    .LineWidth = Options.DefaultBorderLineWidth  
    .Color = Options.DefaultBorderColor  
End With  
With Selection.Borders(wdBorderBottom)  
    .LineStyle = wdLineStyleSingle  
    .LineWidth = Options.DefaultBorderLineWidth  
    .Color = Options.DefaultBorderColor  
End With  
With Selection.Borders(wdBorderRight)  
    .LineStyle = wdLineStyleSingle  
    .LineWidth = Options.DefaultBorderLineWidth  
    .Color = Options.DefaultBorderColor  
End With  
With Selection.Borders(wdBorderVertical)  
    .LineStyle = wdLineStyleSingle  
    .LineWidth = Options.DefaultBorderLineWidth  
    .Color = Options.DefaultBorderColor  
End With  
With Selection.Borders(wdBorderHorizontal)  
    .LineStyle = wdLineStyleSingle  
    .LineWidth = Options.DefaultBorderLineWidth  
    .Color = Options.DefaultBorderColor  
End With  

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Charles Kenyon 167.8K Reputation points Volunteer Moderator
    2022-04-12T23:15:10+00:00

    The paragraph shading can be best handled using a paragraph style, not a macro.

    Are you trying to set up Table Styles?

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2022-04-13T00:25:28+00:00

    If you create the following macro in the template from which you create the documents,

    Sub AutoNew()

    With Selection

    .Shading.ForegroundPatternColor = wdColorAutomatic

    .Shading.BackgroundPatternColor = wdColorYellow

    End With

    End Sub

    when you create a new document, you wlll get:

    Image

    Is that really what you want to happen?

    By default, tables are created with all borders applied.

    No, I'm trying to avoid that highlighted paragraph. I just want the shading color to be set to yellow when I need it, because that is the color I use. As it stands, every time I open a new document and want to highlight something I first have to select the color. I'm lazy.

    So it appears I have to use the Selection mode.

    What is the command to delete current selection.

    Looks like I can't do what I want.

    .

    I copy a lot of content from the web that is formatted as tables. They mostly come in as no borders. Again, I have to select the all borders option before it can be applied the first time. It stays selected after that until the document is closed.

    Was this answer helpful?

    0 comments No comments
  3. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2022-04-12T23:23:36+00:00

    If you create the following macro in the template from which you create the documents,

    Sub AutoNew()

    With Selection

    .Shading.ForegroundPatternColor = wdColorAutomatic 
    
    .Shading.BackgroundPatternColor = wdColorYellow 
    

    End With

    End Sub

    when you create a new document, you wlll get:

    Is that really what you want to happen?

    By default, tables are created with all borders applied.

    Was this answer helpful?

    0 comments No comments