Microsoft Word Insert Code Snippets

An add-in is a supplemental program that adds custom commands or features to an application. On this website, you will find information about and links to Word add-ins I have developed. The add-ins are available at wordaddins.com. Some of the add-ins may contain only a few macros whereas others contain hundreds of macros. First of all, open the MS Word document in which you want to insert the code snippet. Then place the cursor where you want the snippet to be inserted. Now, go to Insert tab and then click on the Object option. A new box will appear with a list of objects that you can insert in your MS Word document.

Many users want to be able to make small screen tips in Word that work like the screen tips known from Websites, i.e. small boxes with information that appear when you hover the mouse over a text like here. For example, such screen tips may be used to show definitions of terms. Word has no functionality specifically for that but you can make use of the hyperlink functionality and obtain something similar.

You can create such 'screen tips hyperlinks' manually. However, this includes a number of steps: You must select text, add a bookmark with an appropriate name, create a hyperlink and enter the screen tip text, and format the text.

Here you will find a set of macros that automate the process. One macro lets you add screen tips hyperlinks in a structured way and another one lets you easily remove a screen tip hyperlink again if you wish.

Instead of installing the macro solution available on this page you may be interested in the ready-to-use add-in DocTools ScreenTipManager. You will find an introduction and a link to that add-in below.

Quick Navigation

DocTools ScreenTipManager - advanced add-in for Word

DocTools ScreenTipManager makes it fast and easy to create and edit screen tips in Word with up to 2040 characters.

Screen tips you create using the free macro solution on this page are limited to 255 characters. The add-in also includes many other features.

The add-in works with Microsoft Word 2010 and newer versions on PC/Windows.

You can access the functionality of the add-in from a custom tab in the Ribbon.

How to get the DocTools ScreenTipManager add-in

This advanced add-in is not free but the return on investment is almost immediate. It will make all your future work with screen tips fast and easy.

Click the button below to learn more about DocTools ScreenTipManager or to buy the add-in.

You can try DocTools ScreenTipManager risk-free for 7 days.

Macro solution

Cached

Read below if you want to use the macro instead of the add-in.

How the screen tips macros work

The macro module you can download via the link below contains three macros:

  • AddScreenTipToText
  • GetBookmarName (used by the macro above)
  • RemoveScreenTipFromText

For easy access, you can assign keyboard shortcuts to the macros. Alternatively, you can assign the macros to a toolbar (Word 2003 and earlier versions) or the Quick Access Toolbar (QAT) (Word 2007 and later versions).

To add a screen tip hyperlink, select the relevant text, run the macro AddScreenTipToText. In the dialog box that opens, enter the text you want to appear in the screen tip. Click OK.

The text you entered will now appear in a screen tip when a user hovers the mouse over the text. Shading will be applied to the hyperlinked text in order to make it easy for the user to identify text with screen tips. The normal Hyperlink style will automatically be removed from the hyperlink so that the user can distinguish screen tip hyperlinks from ordinary hyperlinks. You can change the shading color in the macro if you wish.

See full list on owlcation.comMicrosoft Word Insert Code Snippets

If you wish to break your screen tip text into multiple lines, follow the instructions in the dialog box. In the example illustrated below, a screen tip with two lines of text is created.

To remove a screen tip, select the screen tip hyperlink or just click in it. Then run the macro RemoveScreenTipFromText.

Note that the screen tip text cannot exceed 255 characters.

For further details about how the macros work, please refer to the comments in the macros.

The illustration below shows the dialog box in which you specify the screen tip text. A sample text has been inserted:

The illustration below shows how screen tips hyperlinks will appear in the document. The displayed screen tip contains the text entered in the dialog box illustrated above:

Additional hints about how the screen tips work

You do not need to read the following in order to use the macros. However, it may help you understand how it works.

You only need the macros in order to add/remove screen tips hyperlinks. No macros need to be available to the end user of the document. This means that you can store the macros e.g. in your Normal.dot/Normla.dotm or in a global template placed in your Word Startup folder.

Since the screen tips are actually made as hyperlinks, you will find the standard hyperlink text Ctrl + click to follow link or Click to follow link at the bottom of the screen tips. That text cannot be removed. Whether Ctrl + click or only Ctrl is shown depends on whether or not the option Use Ctrl + click to follow hyperlink is turned on – here shown as it appears in Word 2003; in Word 2007 and 2010 you will find the option in the Word Options dialog, below the Advanced category:

The Word option for displaying screen tips must be turned on. In Word 2003, you will find the option here:
Tools > Options > View tab > Show group > Screen Tips.

In Word 2007 you will find the option here:
Office button > Word Options > Display category > Show document tooltips on hover.

In Word 2010, Word 2013 and Word 2016, you will find the option here:
File > Options > Display category > Show document tooltips on hover.

Note the screen tips are automatically turned on by the AddScreenTipToText macro. If the screen tips do not appear on a user's PC, ask the user to turn on the option on his/her PC.

How to Insert Source Code Into a Word Document

Microsoft Word For Mac Insert Code Snippet

In order to make the selection remain unchanged if a user Ctrl+clicks or clicks the hyperlink, a bookmark is added around the hyperlink itself and the hyperlink is defined to go to that bookmark, i.e. the cursor does not jump to another place.

Hyperlinks are fields. A field contains field code, i.e. instructions to Word about what to do. You can display the field codes by pressing Alt+F9. If you display field codes, you will see that the screen tips hyperlinks look as illustrated below. Note that formatting marks are shown so that you can se both the paragraph mark in the two-line screen tip and the spaces in the field code:

The field consists of these parts:

  • HYPERLINK is the field type
  • l tells Word to go to the bookmark named '_ScreenTip_4'
  • o tells Word to display the text that follows as screen tip text

Note that all the bookmarks created by the AddScreenTipToText macro are named '_SrceenTip_X' where X is a number. Bookmarks that start with an underscore are treated as hidden. If you open the Bookmark dialog box, you need to turn on Hidden bookmarks to see the bookmarks in the list. You cannot manually create hidden bookmarks. They can only be added programmatically. It you do not want the bookmarks to be hidden, you must change the macro code.

If you copy a screen tip hyperlink to another place in the same document, the copy points to the bookmark in the original screen tip since the bookmark name in the field code has not been changed. Thus, if a user clicks such copied screen tip hyperlink, the cursor will now jump to the original screen tip. The easiest way to correct this may be to remove the copied screen tip hyperlink using the RemoveScreenTipFromText macro and then create a new one using the AddScreenTipToText macro. The AddScreenTipToText macro always creates a uniquely named bookmark in the correct place.

Snippets

Screen tips hyperlinks can only be seen in the Word document. For example, they will not appear if you create a PDF file.

Microsoft Word Insert Code Snippet

The macros

Below, you will find the macro code. If you need help on installing macros, see How to install a macro.

Related information

How To Insert Source Code Into A Word Document

See About VBA Macros and Code Snippets and How to Install a Macro for misc. information that may help you in your work with macros and for information about how to install macros.