Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Macro to insert a Quick Part in MS Word Document

Posted on 2013-01-19
Medium Priority
Last Modified: 2013-01-20
Is it possible to create a macro that inserts a quick part on the page? For example, the shortcut key would insert the "Author" Quick Part on the page.
Question by:obautista
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 76

Expert Comment

ID: 38796759
You can easily record such a macro. It would be something like this:
    ActiveDocument.AttachedTemplate.BuildingBlockEntries("Author").Insert _
        Where:=Selection.Range, RichText:=True

Open in new window

LVL 40

Accepted Solution

als315 earned 2000 total points
ID: 38796767
You can record any macro and assign it to some key. In Word 2010 it is in view tab on ribbon (Macros - Record macro). If you open recorded macro, you will see that it is empty - Word does not record Quick Parts on any reason. Add this line to macro if you like to add author:
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"DOCPROPERTY Author ", PreserveFormatting:=True

Author Comment

ID: 38796779
Is it safe to assume the VBA could be saved to a Word Macro-Enabled Template (*.dotm)?  

Thanks for the tip -
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 40

Expert Comment

ID: 38796790
Normally it is saved to Normal.dotm on your computer, so you can use it in any document (doc, docx, etc), but only on your computer

Author Comment

ID: 38796852
I created a custom property named IM Code.  How would I include that in VBA code?

When I go to Quick Parts > Document Property I see it there, but cannot get it to work in the VBA code.  I receive "Error! Unknown document property name".
LVL 40

Assisted Solution

als315 earned 2000 total points
ID: 38797839
It was not very good Idea to use space in Property name. In this case you should enclose property name into double quotes:
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"DOCPROPERTY " & Chr(34) & "IM Code" & Chr(34), PreserveFormatting:=True

Open in new window

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "DOCPROPERTY  ""IM Code"" ", PreserveFormatting:=True

Open in new window

You can save code to macro if you will add Field instead of Document properties in Macro recorder (also from Quick Parts):
Custom property

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question