Content Control: Address Book Option?

I'm using content controls to create a form letter.  I would like a field that give the user the option to type in the addressee and address or to choose an address from his/her contacts list.  I can get the Address book into Word on the quick access toolbar.  is there a way to link that to an option in the drop down box?

Thank you.

Answer
Answer

Save the letter as either a macro-enabled document (*.docm) or a macro-enabled template (*.dotm). The template is preferable, because then you won't have to worry about accidentally overwriting the existing file with the newest version by using Save instead of Save As.

Open the Properties dialog of the content control that will receive the address, and enter NameAddress in the Tag box. Optionally, enter something in the Title box. If it's a plain text content control, also check the box for "Allow carriage returns (multiple paragraphs)". Click OK.

In the document or template, press Alt+F11 to open the macro editor. Expand the corresponding icon on the left side, and double-click the ThisDocument entry inside the Microsoft Word Objects folder icon. Paste the following code into the big empty pane on the right side, and then save, and close the macro editor.

            

Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
    ' don't execute for any other content control
    If CC.Tag <> "NameAddress" Then Exit Sub
    ' don't execute if user has entered anything in the content control
    If Not CC.ShowingPlaceholderText Then Exit Sub

    Dim strNameAddr As String
    Dim strAddrProperties As String

    ' create the "picture" of what parts of the address data to extract
    strAddrProperties = "<PR_GIVEN_NAME> <PR_SURNAME>" & vbCr & _
        "<PR_STREET_ADDRESS>" & vbCr & "<PR_LOCALITY>" & ", " & _
        "<PR_STATE_OR_PROVINCE>" & " " & "<PR_POSTAL_CODE>"

    ' show the dialog for choosing the desired addressee
    strNameAddr = Application.GetAddress( _
        AddressProperties:=strAddrProperties)

    ' if the user hasn't hit Cancel, put the data into the control
    If Len(strNameAddr) > 0 Then
        CC.Range.Text = strNameAddr
    End If
End Sub

The user should click in the NameAddress content control and either type the information or just move the cursor out of the control. If the control is still showing its placeholder ("Click here to enter text"), the address book dialog box will appear.

This isn't a perfect solution. If the chosen address book entry doesn't have all the parts listed in strAddrProperties, the carriage returns (vbCr) and the comma will still appear. It would be possible to add more code to fix that if necessary, or the user could just edit the contents of the control.

_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated August 7, 2023 Views 232 Applies to: