none
Code for mail merge letterhead getting error message RRS feed

  • Question

  • Hi there, I am getting a compile error on a mail merge printing module.  When I try to print the document I get a compile error saying User-defined type not defined on the Normal and a Module called mUtilities.  Code below (it doesnt like the 5th line down Private appWord As ThisApp):

    .

    .

    .

    .

    Option Explicit
    Public Const DOCPROP_HOTDOCS_GENERATED_NAME As String = "HotDocsGeneratedDoc"
    Public Const DOCPROP_HOTDOCS_GENERATED_VALUE As String = "HotDocs Generated Document"
    'used for capturing Word Events
    Private appWord As ThisApp
    '-----------------------------------------------------------------------------------------------
    'On launching this addin, initialize our ThisApp class
    '-----------------------------------------------------------------------------------------------
    Public Sub OffAutoExec()
     
      MsgBox "autoexec2"
      On Error GoTo l_err
      'start our event code, but don't create a new object if it's already in memory
      If m_appWord Is Nothing Then
        Set m_appWord = New ThisApp
      End If

    l_exit:
      On Error Resume Next
      'any changes we make during autoexec shouldn't trigger this as a "dirty" template
      ThisDocument.Saved = True
      Exit Sub
    l_err:
      'ignore errors in autoexec
      Resume l_exit
    End Sub
    '-----------------------------------------------------------------------------------------------
    ' See if the passed doc is a HotDocs generated document, with an option to make it so
    '-----------------------------------------------------------------------------------------------
    Public Function f_IsHotDocsGenerated(oDoc As Document, _
                                         Optional bMakeMe As Boolean) As Boolean
     ' MsgBox "here"
      Dim prop As DocumentProperty
      Dim bExists As Boolean
      On Error GoTo l_err
      With oDoc
        For Each prop In oDoc.CustomDocumentProperties
          'if it already exists, just modify the value
          If prop.Name = DOCPROP_HOTDOCS_GENERATED_NAME Then
            bExists = True
            prop.Value = DOCPROP_HOTDOCS_GENERATED_VALUE
          End If
        Next
        'if it already existed, then let us know
        If bExists Then
          f_IsHotDocsGenerated = True
        End If
        If bMakeMe Then
          'if we didn't see it above, create it and put in our value
          If bExists = False Then
            .CustomDocumentProperties.Add Name:=DOCPROP_HOTDOCS_GENERATED_NAME, _
                                          Value:=DOCPROP_HOTDOCS_GENERATED_VALUE, _
                                          Type:=msoPropertyTypeString, _
                                          LinkToContent:=False
           
            f_IsHotDocsGenerated = True
          End If
        End If
      End With
    l_exit:
      Exit Function
    l_err:
      'f_ErrorTrapping Err.Number, MODULE_NAME & "::I_am_HOTDOCS_doc", Err.Description, False
    End Function

    .

    .

    .

    .

    And then it runs another Module called Printing which also has a compile error saying Variable not defined.  Code below (it doesnt like the 16th line down i = 1):

    .

    .

    .

    Public Sub SetMailMergeLetterhead()

    Dim sec As Section
    Dim myType As String
    Dim intpos As Integer
    Dim myStr As String
    Dim myPrinter As String
    Dim myPrinterLower As String

    Dim Section As Collection

        myType = Dialogs(wdDialogFilePrintSetup).Printer
       
        intpos = InStr(1, myType, " ")
        myStr = Left(myType, intpos - 1)
        intpos = InStr(3, myStr, "\")
       
        myPrinter = Mid(myStr, intpos + 1)
        myPrinterLower = LCase(myPrinter)
        'ActiveDocument.PageSetup.Orientation = wdOrientPortrait
       
        i = 1
        For i = 1 To ActiveDocument.Sections.Count
            'Colourcopiers
            If Mid(myPrinterLower, 4, 12) = "colourcopier" Then
                With ActiveDocument.PageSetup
                    .FirstPageTray = 259
                    .OtherPagesTray = 258
                    With ActiveDocument.Sections(i).PageSetup
                        .FirstPageTray = 259
                        .OtherPagesTray = 258
                    End With
                End With
            'Akl19Copier4 - 7000
            ElseIf myPrinterLower = "akl19copier4" Then
                With ActiveDocument.PageSetup
                    .FirstPageTray = 260
                    .OtherPagesTray = 262
                    With ActiveDocument.Sections(i).PageSetup
                        .FirstPageTray = 260
                        .OtherPagesTray = 262
                    End With
                End With
      
            '55's ie l20copier1
            ElseIf Mid(myPrinterLower, 4, 6) = "copier" Then
                With ActiveDocument.PageSetup
                    .FirstPageTray = 260
                    .OtherPagesTray = 259
                    With ActiveDocument.Sections(i).PageSetup
                        .FirstPageTray = 260
                        .OtherPagesTray = 259
                    End With
                End With
            '5010's: ie AklL20Copier1
            ElseIf Mid(myPrinterLower, 6, 6) = "copier" Then
                With ActiveDocument.PageSetup
                    .FirstPageTray = 262
                    .OtherPagesTray = 263
                    With ActiveDocument.Sections(i).PageSetup
                        .FirstPageTray = 262
                        .OtherPagesTray = 263
                    End With
                End With
            'FollowMe with 5010 driver
            ElseIf InStr(myPrinterLower, "followme") Then
                With ActiveDocument.PageSetup
                    .FirstPageTray = 262
                    .OtherPagesTray = 263
                    With ActiveDocument.Sections(i).PageSetup
                        .FirstPageTray = 262
                        .OtherPagesTray = 263
                    End With
                End With
            Else
                With ActiveDocument.PageSetup
                    .FirstPageTray = wdPrinterLargeCapacityBin
                    .OtherPagesTray = wdPrinterLargeCapacityBin
                    With ActiveDocument.Sections(i).PageSetup
                        .FirstPageTray = wdPrinterLowerBin
                        .OtherPagesTray = wdPrinterLargeCapacityBin
                    End With
                End With
            End If
        Next i
       
    Exit Sub
    ErrorHandler:
        Call ProcessErrors("MERWCommon.SetMailMergeLetterhead", Err.Number, Err.Source, Err.Description, Err.LastDllError)
    End Sub

    .

    .

    .

    Sorry I dont know much about vba - can anyone see an obvious issue here.

    Cheers




    • Edited by emalsamac Tuesday, October 22, 2013 4:51 AM
    Tuesday, October 22, 2013 4:46 AM

All replies

  • Well, what is ThisApp supposed to represent? Is it defined anywhere? You don't show us a public class ThisApp and apparently Word can't find such a class...

    Usually, I'd expect this to be Word.Application


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 22, 2013 6:20 AM
    Moderator
  • Hi Cindy

    Thanks a lot for your reply.  I have changed ThisApp to Word.Application and that no longer comes up as an error but I am still getting a variable not defined error on the i = 1.  Are you able to suggest a solution here?

    Many thanks

    Tuesday, October 22, 2013 8:02 PM
  • You need to first delcare ("Dim") the variable i, same as any other variable...

    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, October 23, 2013 10:49 AM
    Moderator
  • Thanks Cindy, well I am not sure what I am doing to be honest.  I changed it to Dim i As Integer and I no longer get any error messages, but it doesnt do what it is supposed to do, which is print each first page section of a letter on headed paper and any subsequent pages on plain.

    I don't know if definining it as an integer would do the job or not.  It just got rid of the error message.  My new code looks like this

    Option Explicit
    Public Const DOCPROP_HOTDOCS_GENERATED_NAME As String = "HotDocsGeneratedDoc"
    Public Const DOCPROP_HOTDOCS_GENERATED_VALUE As String = "HotDocs Generated Document"
    'used for capturing Word Events
    Private appWord As Word.Application

    '-----------------------------------------------------------------------------------------------
    'On launching this addin, initialize our ThisApp class
    '-----------------------------------------------------------------------------------------------
    Public Sub OffAutoExec()
     
      MsgBox "autoexec2"
      On Error GoTo l_err
      'start our event code, but don't create a new object if it's already in memory
      If m_appWord Is Nothing Then
        Set m_appWord = New Word.Application

      End If

    l_exit:
      On Error Resume Next
      'any changes we make during autoexec shouldn't trigger this as a "dirty" template
      ThisDocument.Saved = True
      Exit Sub
    l_err:
      'ignore errors in autoexec
      Resume l_exit
    End Sub
    '-----------------------------------------------------------------------------------------------
    ' See if the passed doc is a HotDocs generated document, with an option to make it so
    '-----------------------------------------------------------------------------------------------
    Public Function f_IsHotDocsGenerated(oDoc As Document, _
                                         Optional bMakeMe As Boolean) As Boolean
     ' MsgBox "here"
      Dim prop As DocumentProperty
      Dim bExists As Boolean
      On Error GoTo l_err
      With oDoc
        For Each prop In oDoc.CustomDocumentProperties
          'if it already exists, just modify the value
          If prop.Name = DOCPROP_HOTDOCS_GENERATED_NAME Then
            bExists = True
            prop.Value = DOCPROP_HOTDOCS_GENERATED_VALUE
          End If
        Next
        'if it already existed, then let us know
        If bExists Then
          f_IsHotDocsGenerated = True
        End If
        If bMakeMe Then
          'if we didn't see it above, create it and put in our value
          If bExists = False Then
            .CustomDocumentProperties.Add Name:=DOCPROP_HOTDOCS_GENERATED_NAME, _
                                          Value:=DOCPROP_HOTDOCS_GENERATED_VALUE, _
                                          Type:=msoPropertyTypeString, _
                                          LinkToContent:=False
           
            f_IsHotDocsGenerated = True
          End If
        End If
      End With
    l_exit:
      Exit Function
    l_err:
      'f_ErrorTrapping Err.Number, MODULE_NAME & "::I_am_HOTDOCS_doc", Err.Description, False
    End Function

    .

    .

    .

    .

    And then it runs another Module called Printing which also has a compile error saying Variable not defined.  Code below (it doesnt like the 16th line down i = 1):

    .

    .

    .

    Public Sub SetMailMergeLetterhead()

    Dim sec As Section
    Dim myType As String
    Dim intpos As Integer
    Dim myStr As String
    Dim myPrinter As String
    Dim myPrinterLower As String
    Dim i As Integer

    Dim Section As Collection

        myType = Dialogs(wdDialogFilePrintSetup).Printer
       
        intpos = InStr(1, myType, " ")
        myStr = Left(myType, intpos - 1)
        intpos = InStr(3, myStr, "\")
       
        myPrinter = Mid(myStr, intpos + 1)
        myPrinterLower = LCase(myPrinter)
        'ActiveDocument.PageSetup.Orientation = wdOrientPortrait
       
        i = 1
        For i = 1 To ActiveDocument.Sections.Count
            'Colourcopiers
            If Mid(myPrinterLower, 4, 12) = "colourcopier" Then
                With ActiveDocument.PageSetup
                    .FirstPageTray = 259
                    .OtherPagesTray = 258
                    With ActiveDocument.Sections(i).PageSetup
                        .FirstPageTray = 259
                        .OtherPagesTray = 258
                    End With
                End With
            'Akl19Copier4 - 7000
            ElseIf myPrinterLower = "akl19copier4" Then
                With ActiveDocument.PageSetup
                    .FirstPageTray = 260
                    .OtherPagesTray = 261                With ActiveDocument.Sections(i).PageSetup
                        .FirstPageTray = 260
                        .OtherPagesTray = 261
                    End With
                End With
      
            '55's ie l20copier1
            ElseIf Mid(myPrinterLower, 4, 6) = "copier" Then
                With ActiveDocument.PageSetup
                    .FirstPageTray = 260
                    .OtherPagesTray = 259
                    With ActiveDocument.Sections(i).PageSetup
                        .FirstPageTray = 260
                        .OtherPagesTray = 259
                    End With
                End With
            '5010's: ie AklL20Copier1
            ElseIf Mid(myPrinterLower, 6, 6) = "copier" Then
                With ActiveDocument.PageSetup
                    .FirstPageTray = 262
                    .OtherPagesTray = 263
                    With ActiveDocument.Sections(i).PageSetup
                        .FirstPageTray = 262
                        .OtherPagesTray = 263
                    End With
                End With
            'FollowMe with 5010 driver
            ElseIf InStr(myPrinterLower, "followme") Then
                With ActiveDocument.PageSetup
                    .FirstPageTray = 262
                    .OtherPagesTray = 263
                    With ActiveDocument.Sections(i).PageSetup
                        .FirstPageTray = 262
                        .OtherPagesTray = 263
                    End With
                End With
            Else
                With ActiveDocument.PageSetup
                    .FirstPageTray = wdPrinterLargeCapacityBin
                    .OtherPagesTray = wdPrinterLargeCapacityBin
                    With ActiveDocument.Sections(i).PageSetup
                        .FirstPageTray = wdPrinterLowerBin
                        .OtherPagesTray = wdPrinterLargeCapacityBin
                    End With
                End With
            End If
        Next i
       
    Exit Sub
    ErrorHandler:
        Call ProcessErrors("MERWCommon.SetMailMergeLetterhead", Err.Number, Err.Source, Err.Description, Err.LastDllError)
    End Sub

    Wednesday, October 23, 2013 8:44 PM