Word Macros

What's covered?

A selection of Word macros that I have written, been helped with or found.

Word Version Compatibility

These macros were created for the version of Word current at the time. They will most likely work in later versions but as with running any macro, create a back up of your file before you start.

How to Add a Macro

Click on any link below to download the required zip file.

Depending on your operating system and security settings, you will either be prompted to save the zip file or your zip application (e.g. WinZip) will open and display the contents of the zip file.

The zip files each contain a bas file for import into Word via the VBA import function.

In Word, locate the VBA Editor. Try Tools | Macros | VBA Editor OR Alt F11.

Create a new module as shown below. Call it whatever you want.



Then right click the module and select Import. Import whichever macros you want to use.

Then either go to Tools | Macro | Macros, select the macro and click Run OR customise a toolbar and add the macro there.

If you want to see the content of the bas file before importing it into Word, open the file with a text editor such as Notepad.

The Macros

Word Macro

Action

Add Brackets

Puts selection in round brackets ()

Add Quotes

Puts selection in quote marks

Add Tags

Puts selection in html tags <>

Add Page Numbers after hyperlinks in RoboHelp Printed Documentation

Classic Versions only

In your printed documentation, you may have something like Figure 1 which is fine for anyone viewing the document on a PC. They can click the hyperlink and see Program Access. But what about someone reading hard copy? It's not much use to them is it? They have to go to the Table of Contents and locate "Program Access".

Figure 1


Now thanks to HansV, a long term supporter of Woody's Lounge, the macro below will amend your document so that it reads as shown in Figure 2.

Figure 2


Select the text in the cell below and then press CTRL + C to add the code to the clipboard. Then create the macro in Word.

Sub
                    InsertPageRefs()
                    'Written by HansR of Woody's Lounge.
                    'Visit http://www.wopr.com and follow the link to Woody's
                    Lounge
                    'The InsertAfter Text amended by Peter Grainge Dim i As
                    Integer Dim h As Hyperlink Dim r As Range For i =
                    ActiveDocument.Hyperlinks.Count To 1 Step -1 Set h =
                    ActiveDocument.Hyperlinks(i) Set r = h.Range If
                    InStr(h.SubAddress, "_Toc") = 0 And h.Address = "" Then
                    r.InsertAfter Text:=" (See page " r.Collapse
                    Direction:=wdCollapseEnd r.InsertAfter Text:=")" r.Collapse
                    Direction:=wdCollapseStart ActiveDocument.Fields.Add
                    Range:=r, Text:="PAGEREF " & h.SubAddress End If Next i
                    MsgBox "Page numbers have been added."
                    End Sub

 

The lines r.InsertAfter allow you to define the text surrounding the page number to whatever you want. This requires some thought as you need to find a single string of text that will always work. The string works for my documents regardless of whether the hyperlink is followed by a full stop (period) or a space. If the hyperlink is already followed by something in brackets, then you will end up with text as in Figure 3.

Figure 3


You will need to run the macro a few times and check the results to see what text works best for you.

The hyperlink is left working for the benefit of anyone viewing the Word document on their PC. Also removing it would destroy the context of your documents. Imagine the examples shown without the hyperlinks being left in place.

The usual warning, backup your document before you run the macro.

UPDATE 19 Aug 2012.

This is what I see after running the macro.

Mary Moore advised me that the link was extending to include (See page.

I have retested the macro above and do not have that problem but if you do, then you might want to try an alternative macro that was posted at WindowsSecrets.com by Paul Edstein, Microsoft Word MVP. Paul claims this code will also run faster.

Click in the field below and then press CTRL + C to add the code to the clipboard. Then create the macro in Word.

Sub
                    InsertPageRefs()
                    ' Macro written by Paul Edstein, Microsoft Word MVP
                    Application.ScreenUpdating = False
                    Dim hLnk As Hyperlink, Rng As Range
                    For Each hLnk In ActiveDocument.Hyperlinks With hLnk If
                    InStr(.SubAddress, "_Toc") = 0 And .Address = "" Then Set
                    Rng = .Range With Rng .Collapse Direction:=wdCollapseEnd
                    .InsertAfter Text:=" (See page #)" .Font.Underline =
                    wdUnderlineNone End With ActiveDocument.Fields.Add
                    Range:=Rng.Characters(InStr(Rng, "#")), Text:="PAGEREF "
                    & .SubAddress End If End With
                    Next
                    Set Rng = Nothing
                    Application.ScreenUpdating = True
                    Application.ScreenRefresh
                    MsgBox ActiveDocument.Hyperlinks.Count & " page numbers
                    have been added.", vbOKOnly
                    End Sub

Thanks to both HansR and Paul Edstein.

Delete Unused Styles

When you import a Word document into RoboHelp, the mapping dialog will show all the styles available in the document. In documents that have passed from person to person, these can be high in number. I have seen one report of 300 styles!

You only need to map the used styles rather than those available but not used. Run this macro before you import to remove the unused styles.

Fill Blank Rows

With cursor in a cell with text, the macro copies text to each blank cell below until it finds another value. That value is then copied and the process repeats.

Format Tables

Seven macros

  1. Sets all rows to "Allow row to break across pages = false"
  2. Applies required properties to a table. User given option as to whether each table found is to be formatted or skipped. User can also choose to start from current position or the start of the document.
  3. The same as (2) except the user has no choice as to where it starts. It always runs from the start of the document.
  4. The same as (2) except it only works on the next table after the start point.
  5. Applies required properties to all tables in the document without giving user option to include or exclude.
  6. Scrolls through tables in the document.
  7. Selects the first row of each table and applies the Heading setting.

The above macros have clearly marked sections that apply the formatting so that you can substitute what you want in place of what I use.

Printing

Prints current page.

Remove blank line from RoboHelp printed output

Sometimes you will see a heading style one line down from the top of the page when you generate printed output from RoboHelp. Run this macro to remove just those blank lines.

Remove Bookmarks

Removes all bookmarks from a document. Use with care.

Remove Space

Two macros

  1. Removes any space before the paragraph.
  2. Removes any space after the paragraph.

Set all images to 100%

Word will reduce image sizes to make them fit the page width. In older versions of RoboHelp, any image that is not set to 100% will get converted to a JPG when you or RoboHelp use the Save As Web Page Filtered option.
This macro will reset all images in the document to 100%.

Style Changer

This zip file also contain a frm file. See the readme text file.

Run the Style Changer macro to display the Style Changer dialog.

The dialog doesn't do anything more than the standard Find and Replace, it just makes it a lot quicker.

Word's Find and Replace function requires ten clicks before the operation starts whereas this requires one click to display the dialog box, then select the two styles and one more click to start the operation.

Also Word's function requires you to select from the list or you can type just the first letter of the style. If that does not find the style, you have to scroll down to locate it. This method allows you to keep typing the style name until it is found.

Donations

If you find the information and tutorials on my site save you time figuring it out for yourself and help improve what you produce, please consider making a small donation.