Solved

Macro to insert a Quick Part in MS Word Document

Posted on 2013-01-19
6
2,954 Views
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.
0
Comment
Question by:obautista
  • 3
  • 2
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
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

0
 
LVL 39

Accepted Solution

by:
als315 earned 500 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
(http://vbadud.blogspot.ru/2009/01/how-to-insert-document-properties-in.html)
0
 

Author Comment

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

Thanks for the tip -
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 39

Expert Comment

by:als315
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
0
 

Author Comment

by:obautista
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".
0
 
LVL 39

Assisted Solution

by:als315
als315 earned 500 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

Or
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now