How to I create a MS Word macro to fill in a form with custom document properties

Dear community,

I am complete coding beginner so I'd be very grateful for some friendly assistance please.

Specifically, I need to create a macro for a MS Word 2013 file so that when a user opens the template as a new document for the first time they are prompted to fill in a form to populate four custom document properties (CDPs).

The four CDPs are "customer", "country", "project" and "type".

Once the user has completed the pop-up form, the four responses should appear as respective values in the Custom tab of the Advanced Properties of the Document Information Panel.

In the Developer> Visual Basic area, I have set up a new module in the current document, called "CustomDocProperties" and I've added the following code;

Private Sub Document_Close()
Dim StrOldCustomer As String, StrNewCustomer As String
Dim StrOldCountry As String, StrNewCountry As String
Dim StrOldProject As String, StrNewProject As String
Dim StrOldType As String, StrNewType As String
Dim bSaved As Boolean
With ActiveDocument
  bSaved = .Saved
  StrOldTitle = .CustomDocumentProperties("Customer").Value
  StrOldAuthor = .CustomDocumentProperties("Country").Value
  StrOldSubject = .CustomDocumentProperties("Project").Value
  StrOldStatus = .CustomDocumentProperties("Type").Value
  StrNewTitle = InputBox("Enter the Customer Name:", "Customer", StrOldCustomer)
  StrNewAuthor = InputBox("Enter the Country:", "Country", StrOldCountry)
  StrNewSubject = InputBox("Enter the Project:", "Project", StrOldProject)
  StrNewStatus = InputBox("Enter the Type:", "Type", StrOldType)
  If StrOldTitle <> StrNewTitle Then _
    .CustomDocumentProperties("Customer").Value = StrNewCustomer
  If StrOldAuthor <> StrNewAuthor Then _
    .CustomDocumentProperties("Country").Value = StrNewCountry
  If StrOldSubject <> StrNewSubject Then _
    .CustomDocumentProperties("Project").Value = StrNewProject
  If StrOldStatus <> StrNewStatus Then _
    .CustomDocumentProperties("Type").Value = StrNewType
  If bSaved = True And .Saved = True Then Exit Sub
End With
End Sub

When I run this code (F5) a pop-up box appears one after another requesting the user to fill in each of the four custom document properties. This is basically what I want, but I would like some help to refine it please.

Three questions:

1. How can this VBA process be automated so that the user is prompted to fill a form about the four custom document properties (CDP) when they first create/open a new document based on the master template?

2. How can the CDP values that a user types be made to appear as values in the Custom tab of the Advanced Properties dialog box (because each of these are currently blank)?

3. Would it be better to change the name of the fourth CPD to a different name than "Type" because this term is used elsewhere and maybe confusing?

Do you have any other advice?

Many thanks in advance for your kind expertise.
london-userAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

regmigrantCommented:
here's some basic answers, come back on any you need more detail for:-
1. Put the macro itself (or a call to the macro if you prefer) into a sub called 'auto_open'  in your template and it will execute every time a document is opened. you will need to trap the case where the details have already been filled in if you don't want them requested every time its opened. Alternatively use AutoNew for something that executes only when you create a new one,

2. Not clear on what you are asking here. In the advanced properties page you click on each of the custom properties and it should display the value of that property. If the issue is that the information above is not being recorded then make sure the document is 'saved' and consider this example from MS which comes from here:

https://msdn.microsoft.com/en-us/library/office/ff862806.aspx

With ActiveDocument.CustomDocumentProperties 
    .Add Name:="CustomNumber", _ 
        LinkToContent:=False, _ 
        Type:=msoPropertyTypeNumber, _ 
        Value:=1000 
    .Add Name:="CustomString", _ 
        LinkToContent:=False, _ 
        Type:=msoPropertyTypeString, _ 
        Value:="This is a custom property." 
    .Add Name:="CustomDate", _ 
        LinkToContent:=False, _ 
        Type:=msoPropertyTypeDate, _ 
        Value:=Date 
End With

Open in new window


3. As these are custom properties their names are entirely up to you but the point is a good one, 'type' is too generic to be useful in most cases so find a name that works within the process you are trying to support. eg: look at the context of 'Type' and find a word that makes it more obvious; so if its about reporting then use 'Report_Type', etc
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.