Solved

Macro to insert a Quick Part in MS Word Document

Posted on 2013-01-19
6
3,364 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
[X]
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
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 40

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
LVL 40

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 40

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

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

751 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