Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Macro to insert a Quick Part in MS Word Document

Posted on 2013-01-19
6
Medium Priority
?
3,812 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 40

Accepted Solution

by:
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
(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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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) …
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Suggested Courses

926 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