Please Note: This article is written for users of the following Microsoft Word versions: 2007 and 2010. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Copying Custom Properties.

Copying Custom Properties

by Allen Wyatt
(last updated April 2, 2013)

9

Custom document properties are a great way to store unique information that is associated with a document. For instance, you may have a company-assigned document number that needs to be stored with a document. A custom property fits the bill quite nicely for this purpose.

As you add custom properties to a document, you may start wondering if there is an easy way to copy them from one document to another. Unfortunately, there is no way to do this. (In my estimation, this capability would make a fine addition to the Organizer.) You can, however, create a macro that will do the copying for you. The following macro will do just that:

Sub CopyDocProps()
    Dim dp() As DocumentProperty
    Dim CustomPropCount As Integer
    Dim i As Integer
    Dim intResponse As Integer

    If Windows.Count > 2 Then
        MsgBox "There are more than two windows. Please " & _
          "close the others and re-run the macro.", , _
          "Too many windows"
        Exit Sub
    End If

    On Error GoTo Err_Handler

    intResponse = MsgBox("Are you currently in the source document?", _
      vbYesNoCancel, "Copy Custom Properties")

    If intResponse = vbNo Then Application.Run MacroName:="NextWindow"

    CustomPropCount = ActiveDocument.CustomDocumentProperties.Count
    ReDim dp(1 To CustomPropCount)

    For i = 1 To CustomPropCount
        Set dp(i) = ActiveDocument.CustomDocumentProperties(i)
    Next i

    Application.Run MacroName:="NextWindow"

    For i = 1 To CustomPropCount
        If dp(i).LinkToContent = True Then
            ActiveDocument.CustomDocumentProperties.Add _
              Name:=dp(i).Name, _
              LinkToContent:=True, _
              Value:=dp(i).Value, _
              Type:=dp(i).Type, _
              LinkSource:=dp(i).LinkSource
        Else
            ActiveDocument.CustomDocumentProperties.Add _
              Name:=dp(i).Name, _
              LinkToContent:=False, _
              Value:=dp(i).Value, _
              Type:=dp(i).Type
        End If
    Next i

    MsgBox "The properties have been copied."
    Exit Sub

Err_Handler:
    ' if Word raises an error, then allow the user
    ' to update the custom document property
    
    intResponse = MsgBox("The custom document property (" & _
      dp(i).Name & ") already exists." & vbCrLf & vbCrLf & _
      "Do you want to update the value?", vbYesNoCancel, _
      "Copy Custom Properties")

    Select Case Response
        Case vbCancel
            End
        Case vbYes
            ActiveDocument.CustomDocumentProperties(dp(i).Name).Value _
              = dp(i).Value
            Resume Next
        Case vbNo
            Resume Next
    End Select
End Sub

This code is an example of how to copy custom properties, but it is not bulletproof. For instance, it does not check to see if there are actually any custom properties in the source document; it just assumes that there are. Such coding could be easily added, however.

In order to use the macro, make sure that you have only the source and target documents open, and you should only have one window open per document. When the macro is finished, you will need to save the target document.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11671) applies to Microsoft Word 2007 and 2010. You can find a version of this tip for the older menu interface of Word here: Copying Custom Properties.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Can't Merge Alphanumeric Data Correctly

When you merge data from Excel into a Word document, you may need to do some conditional processing based on the data you ...

Discover More

Allowing Only Form Field Changes

Word allows you to create forms that other people can use to enter information. One of the last steps normally taken with ...

Discover More

Rounding Up to the Next Half

When processing data it is not unusual to need to round that data in some way. For instance, you may need to round a ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Complex Searches for Documents

When working with lots of documents, you may have need from time to time to discover which of those documents contain ...

Discover More

Using Seek In a Macro

When processing non-document text files in a macro, you have a wide range of commands available for your use. One of ...

Discover More

Renaming a Document

Want to rename a document that is already on your hard drive? You can, of course, do it in Windows, but you can also do ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 3?

2017-12-19 11:30:37

Bob Whitehead

What if you want to copy ALL the properties (default and custom) to another document?


2017-02-17 11:49:50

Brian V

Never mind.
Ian McK 15 Oct 2015, 11:39 - this tip does fix the issue.

It would be useful if the post can be edited with the fix and then most of the comments could be deleted.


2017-02-17 11:23:58

Brian V

I'm having the same issue as "Ryan Z 15 Apr 2013, 16:29" When it encounters a duplicate value and pops up the dialog box asking to copy it and continue it just seems to stop and doesn't copy anymore. Aside from that minor issue it works great


2016-04-22 08:53:16

Malte S

Thanks a lot!

Thanks to Ian McK too for his hint to the typo!


2015-10-15 11:39:25

Ian McK

There's a typo in the script.

Select Case Response

Should be:

Select Case intResponse

Without that it will just error every time.


2014-10-15 04:47:15

Mads U

I have the exact same problem as
Ryan Z.

Any solution as to how to get the macro to continue to "next i" instead of just exiting?


2014-07-07 08:33:12

Henk

Good afternoon,

Copying in source document... copy to clipboard? Copy to XML file?

How do I paste it to the target file?


Best regards, Henk


2014-03-31 11:52:17

AndreA


It is working also in Word 2011 for Mac Os X.

Thanks a lot.


2013-04-15 16:29:38

Ryan Z

Hi, this is exactly what I'm trying to do. However, it didn't work for me; once it hit the Error Handler function due to a duplicative property name (same name in host and target docs), the macro stopped and did not copy the value over regardless of whether or not I clicked "Yes" or "No" on the "Do you want to update the value?" prompt. I would appreciate it if you could double-check this code and see if it needs to be tweaked (sorry, I'm a bit of a novice myself). Thank you.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.