Help with script please RRS feed

  • Question

  • here is the scenario I am wanting to complete:

    I have a form that I fill out for each client that walks through the door.  It is a checklist for the accountant that is going to be helping them.  What I would like to do is be able to select the client in Business Contact Manager 2013, have a macro run to print out the form populated with that client's name, id # address etc.  I would also like to have a buttom in the ribbon to automate this.

    Here is the script that I have.  Whenever I try to run it, I get an error message that it can't find my form, but I know the location I have is right.  So maybe it's the script I have before it?  Could someone please give me some suggestions?  Also, if I am going about this totally wrong, please let me know.

    Public Sub SendAddressToWord()
     'Dim oContact
     If TypeName(ActiveExplorer.Selection.Item(1)) = "ContactItem" Then
     Set oContact = ActiveExplorer.Selection.Item(1)

     ' Declare the variable.
     Dim oWord As Word.Application

     ' Set the variable (runs new instance of Word).
     Set oWord = CreateObject("Word.Application")

     oWord.Documents.Add Template:="C:\Users\Beth\AppData\Roaming\Microsoft\Templates\PreparerChecklist.dotm"
     With oWord

     ' Find the bookmark, insert an Outlook field
     .Selection.GoTo What:=wdGoToBookmark, Name:="FullName"
     .Selection.TypeText Text:=oContact.FullName

     .Selection.GoTo What:=wdGoToBookmark, Name:="ClientId"
     .Selection.TypeText Text:=oContact.UserProperties("ClientId")

     .Selection.GoTo What:=wdGoToBookmark, Name:="Address1"
     .Selection.TypeText Text:=oContact.Address1

     .Selection.GoTo What:=wdGoToBookmark, Name:="City1"
     .Selection.TypeText Text:=oContact.City1

     .Selection.GoTo What:=wdGoToBookmark, Name:="State1"
     .Selection.TypeText Text:=oContact.State1

     .Selection.GoTo What:=wdGoToBookmark, Name:="ZipCode"
     .Selection.TypeText Text:=oContact.ZipCode
     .Selection.GoTo What:=wdGoToBookmark, Name:="Email2"
     .Selection.TypeText Text:=oContact.Email2
     .Selection.GoTo What:=wdGoToBookmark, Name:="PrimaryPhone"
     .Selection.TypeText Text:=oContact.PrimaryPhone

     ' Show the Word document
     .Visible = True
     End With

     ' Clear the variable from memory.
     Set oWord = Nothing

     MsgBox "Sorry, you need to select a contact"
     End If

     End Sub

    Monday, January 27, 2014 3:48 PM

All replies

  • Did you try to debug the code? What line of code fires the exception?

    Also please make sure that the "C:\Users\Beth\AppData\Roaming\Microsoft\Templates\PreparerChecklist.dotm" document exists.

    Finally, I'd recommend breaking the chain of calls into separate lines:


    The Documents property of the Application class from the Word object models returns an instance of the Documents class. Then the Add method returns an instance of the Document class.

    .Selection.GoTo What:=wdGoToBookmark, Name:="FullName"
    .Selection.TypeText Text:=oContact.FullName
    The Selection property of the Application class returns the Selection object that represents a selected range or the insertion point. Note, each time you ask for an object.
    Tuesday, January 28, 2014 11:53 AM