This code works in Word from versions 2003 thru 2013
Windows XP thru Windows 8.1 personal and enterprise equivalents.
This is not a technical issue with the code. More likely it is how you are attempting to use the code.... which you did
not explain per my last request.
>> The code should be placed in the document in question.
>> Make sure the "Active Document" is the document you want the code to act against. If you have more than one document open, this code will attempt to run against the currently active document.
>> Your macro security should be set to allow the VBA to execute. In newer versions of Office this is controlled in the "Trust Center" under "Macro Settings." I usually set this as "Disable All Macros With Notification" I find that the other settings are either too dangerous or too restrictive for my applications.
> Open your document
> Open the VBA Editor (VBE), [Alt][F11]
+ Once VBE is open, {Menu Bar\Tools\Options}
- IN the Options dialog, "Editor Tab"
- Unselect "Auto Syntax Check" -- don't worry, your errors will still flag "red" just no pop-up message for every error
-
Select "Require Variable Declaration" From now on, you will have to declaire the variables using a
Dim
(Read More)
-- Optional> increase tab width to 3 or 4. I like a little more pronounced code stepping.
- Select [OK]
++ Selecting the options as given will help tremendously with trouble shooting and proper coding. There is more information about this in the
trouble shooting section of [*]
> Before Posting (VBA or SQL) Code
> Insert a module in to the correct document
Usually along the right hand side of the VBE is the project pane that shows the tree that typically shows the
Normal
template object, the current document object
Project(documentname)
wherein "documentname" refers to the File Name of the document you are working with, and any other objects/addins associated with your installation.
+ Expand the (Project(documentname)) branch. You should see branches named "Microsoft Word Objects" and "References"
- Right click on the (Project(documentname)) branch and within the quick menu select {Insert\Module}
- Expand the (Project(documentname)) branch if you have not already done so, there should now be a "Modules" object. Expand the "Modules" object branch... there should be at least one "Module#" object where # is the sequence number of the inserted module - I want you to be using a new, empty module; thus, I will refer to this new, empty module as Module# - please interprete this as the new, empty module you just created replacing the # sign with the corresponding sequence number.
- Right click on Module#, select {View Code}
- You should see "[Module# (Code)]" in the VBE Title bar. If not then you are in the wrong code pane. Close all active code panes and then Right click on Module#, select {View Code}
- This pane should contain
Option Explicit
and
no other code.
- Insert the code as provided in
How to insert tag in word document as per content style - Post#10
- Press <Ctrl><G> to open the Immediate Pane.
> Minimize the VBE
> Bring the document you wish to run this code against to the front
>{Ribbon\View\Macros\Macros}
- Macros dialog box
-- Select [StyleToTag] in the list
-- Select Run
>> At this point you should see the style tags at the paragraph level. If not then maximize the VBE, see if there are any errors reported in the immediate pane:
For example, paragraphs within a table you might see:
- - - error 5251 This is not a valid action for the end of a row. Microsoft Word
as the code reaches the end of the table row. This occurs because tables are not paragraphs; thus, they do not fall under this code's scope.