Solved

Macro to insert a Quick Part in MS Word Document

Posted on 2013-01-19
6
3,100 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

803 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