Word template in startup folder issue and set tab stop position in VBA

As we knew Word 2007’s template file is a dotm file. It allow you to launch a document base on the template. However sometimes we want launch word document which not by clicking the template, for example, just open word and run a macro which we created in the template. In this way, you have to put the template dotm file under C:\Users\username\AppData\Roaming\Microsoft\Word\STARTUP folder, and it will show the macro on any word document.

However when the template drop into the startup folder, the style which referenced in the VBA code doesn’t work because it automatically use normal.dotm’s styles and cannot find the styles in this template. The following code will fail because it cannot find the style name at normal.dotm.

Selection.Style = ActiveDocument.Styles(“style123”)

There are two solutions to solve.
1. use code add a style.


        ActiveDocument.Styles.Add Name:="divisiontable", Type:=wdStyleTypeParagraph
        ActiveDocument.Styles("divisiontable").Font.Name = "Times New Roman"
        ActiveDocument.Styles("divisiontable").Font.Size = 10
        ActiveDocument.Styles("divisiontable").BaseStyle = "Normal"
        ActiveDocument.Styles("divisiontable").NextParagraphStyle = "Normal"
        ActiveDocument.Styles("divisiontable").ParagraphFormat.TabStops.Add Position:= _
        InchesToPoints(1.73), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
        ActiveDocument.Styles("Styletest").ParagraphFormat.TabStops.Add Position:= _
        InchesToPoints(3.45), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces

2. Just use the code to generate the paragraph format

   Selection.Font.Name = "Times New Roman"
   Selection.Font.Size = 10
   Selection.ParagraphFormat.TabStops.Add Position:= _
        InchesToPoints(1.73), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
   Selection.ParagraphFormat.TabStops.Add Position:= _
        InchesToPoints(3.45), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces

Attachment:

How to create a style in word

1. click the arrow on the right corner

2. Modify or create a new style on the float screen (red rectangle is the “create”)

3. in the following screen setup the format you want

4. click format to setup further, the tab setting also in this menu

5. setup tab stop position

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s