Link to home
Create AccountLog in
Avatar of london-user

asked on

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.
Avatar of regmigrant
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.