Excel VBA for Word Docx

EMcK01

Board Regular
Joined
Jun 14, 2015
Messages
125
Hi,

I've got a simple macro for Word that adds a caption figure & number to all the images in a file. This is usually a photo appendix for reports for me.

I would like to host this a separate ideally excel (or word) file that I have other useful macros rather than the file with all the pictures, however I'm not sure what I need to do at the start of the code, presumably at the ActiveDocument line.

Can anyone help?

VBA Code:
Sub CaptionPictures()

Dim xPic As InlineShape
    For Each xPic In ActiveDocument.InlineShapes
        xPic.Select
        Selection.InsertCaption "Figure", "", "", wdCaptionPositionBelow, 0
        
            With ActiveDocument.Styles("Caption").Font
                .Name = "Century Gothic"
                .Size = 8
                .ColorIndex = wdautomatic
                .Italic = True
            End With
    Next

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
1. Create a blank Word document and copy your code to the VBE.
2. Save the document as a .dotm file (note that as soon as you choose the dotm from the dropdown, the file location changes; however, you can save it somewhere else like Documents - you need to get to it later, so make sure you know where it is saved). You are saving the document as a macro-enabled template.
3. Close Word doc
4. Open Exploring window to the Word STARTUP folder (e.g., C:\Users\USERNAME\AppData\Roaming\Microsoft\Word\STARTUP)
5. Copy or move dotm file there.

To use:
Putting dotm files in the STARTUP folder makes the macros available to any Word file. ActiveDocument is what you want to have, so leave it.

Open any Word document with the pictures you want to caption. Hit Alt + F8 to open the macros. It should be listed. If not, make sure to choose either "All active templates and documents" in the "Macros in:" dropdown or choose the dotm file by itself in this same dropdown.

Add other macros to the dotm file that you want to persist in all open documents.

Note that you cannot make changes to the dotm file itself while it is in the STARTUP folder (like changing macro or adding new ones). You can see the document in the VBE, but you can't expand it. Since it is being used, Word won't let you edit. I usually move from STARTUP to the desktop to make changes and then move back (or work on a master copy and then copy to STARTUP later - however, this can get confusing because you will have duplicately named documents in the VBE, though only one will be expandable/editable since the other one is in the STARTUP folder).

Also note that the ColorIndex constant is wdauto (not wdautomatic).
 
Upvote 0
Solution
By the way, if you're into adding/modifying the Ribbon such as adding a new tab to handle your global macros, putting the Ribbon changes in the dotm file in STARTUP will also make the Ribbon changes global and available to all documents.
 
Upvote 0
Hi shknbk2, thanks for your response and explanation, that was ideal and exactly what I was looking for.
 
Upvote 0

Forum statistics

Threads
1,215,708
Messages
6,126,363
Members
449,311
Latest member
accessbob

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top