Results 1 to 14 of 14
  1. #1
    Star Lounger
    Join Date
    May 2003
    Location
    Kansas City, Kansas, USA
    Posts
    76
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hello, I have used this for years and for some reason all of the sudden the silly things wants to throw errors up. I have a set up that uses DocProperty to merge to word to create letters. When I push the button that says "create Census Letter" I get an error message that reads Error No. -2147417851 Desc: Method 'Add' of objects 'documents' failed...... ANY HLEP????

  2. #2
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,624
    Thanks
    3
    Thanked 60 Times in 60 Posts
    What version of Office are you using now, and what were you using previously? If you were using 2000, and have switched to 2002, 2003 or 2007, the merge process has changed quite a bit and uses different technology.
    Wendell

  3. #3
    Star Lounger
    Join Date
    May 2003
    Location
    Kansas City, Kansas, USA
    Posts
    76
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by WendellB View Post
    What version of Office are you using now, and what were you using previously? If you were using 2000, and have switched to 2002, 2003 or 2007, the merge process has changed quite a bit and uses different technology.

    The database is 2003. The version has not changed. That is the weird thing! It was working correctly two days ago...

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    What is the code behind the "Create Census Letter" button?


  5. #5
    Star Lounger
    Join Date
    May 2003
    Location
    Kansas City, Kansas, USA
    Posts
    76
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    What is the code behind the "Create Census Letter" button?
    I used Helen Feddema's DocPropery Code.... Worked great....


    Private Sub CreateCensus_Click()

    'Written by Helen Feddema 4-22-98
    'Last modified 7-Aug-2007

    On Error GoTo ErrorHandler

    Dim appWord As Word.Application
    Dim docs As Word.Documents
    Dim strLetter As String
    Dim prps As Object
    Dim strDate As String
    Dim fso As New Scripting.FileSystemObject
    Dim fil As Scripting.File
    Dim strTemplate As String
    Dim strTemplatePath As String
    Dim strTemplateNameAndPath As String
    Dim doc As Word.Document
    Dim strTitle As String
    Dim strPrompt As String


    IF [GIType] = "Admin" Then

    strDate = CStr(Date)
    'Check whether template is found in the folder
    'Get User Templates path from Word Options dialog
    '(or replace with hard-coded path for your computer)
    Set appWord = GetObject(, "Word.Application")
    strTemplatePath = appWord.Options.DefaultFilePath(wdUserTemplatesPat h)
    Debug.Print "Template path: " & strTemplatePath
    strTemplatePath = strTemplatePath & "\Census, Engagement & Annual Trust\"
    strLetter = "Admin.dot"
    strTemplateNameAndPath = strTemplatePath & strLetter
    Debug.Print "Template and path: " & strTemplateNameAndPath

    On Error Resume Next

    Set fil = fso.GetFile(strTemplateNameAndPath)
    If fil Is Nothing Then
    strPrompt = "Can't find " & strLetter & " in " _
    & strTemplatePath & "; canceling"
    MsgBox strPrompt, vbCritical + vbOKOnly
    GoTo ErrorHandlerExit
    End If

    On Error GoTo ErrorHandler

    Set docs = appWord.Documents
    Set doc = docs.Add(strTemplateNameAndPath)

    Set prps = doc.CustomDocumentProperties

    With prps
    .Item("Contact").Value = Nz(Me![Contact])
    .Item("ClientSponsor").Value = Nz(Me![Client-Sponsor])
    .Item("Address").Value = Nz(Me![Address])
    .Item("Address2").Value = Nz(Me![Address2])
    .Item("City").Value = Nz(Me![City])
    .Item("State").Value = Nz(Me![State])
    .Item("Zip").Value = Nz(Me![Zip])
    .Item("PlanName").Value = Nz(Me![Plan Name])
    .Item("Greeting").Value = Nz(Me![Greeting])
    .Item("YearEnd").Value = Nz(Me![MoYrEnd])
    .Item("Annivinfo").Value = Nz(Me![Annivinfo])
    .Item("Excelinfo").Value = Nz(Me![Excelinfo])
    .Item("PBCinfo").Value = Nz(Me![PBCinfo])
    .Item("Note").Value = Nz(Me![Note])
    .Item("ComDefinfo").Value = Nz(Me![ComDefinfo])
    .Item("Exclu").Value = Nz(Me![Exclu])
    .Item("ExcluBon").Value = Nz(Me![ExcluBon])
    .Item("Excl1").Value = Nz(Me![Excl1])
    .Item("ComDefinfo2").Value = Nz(Me![ComDefinfo2])
    .Item("ExclBon1").Value = Nz(Me![ExclBon1])
    .Item("4k3").Value = Nz(Me![4k3])
    .Item("4k4").Value = Nz(Me![4k4])
    .Item("Entry").Value = Nz(Me![Entry])
    .Item("Entry2").Value = Nz(Me![Entry2])
    .Item("Entry3").Value = Nz(Me![Entry3])
    .Item("4kMatch").Value = Nz(Me![4KMatch])
    .Item("4kMatch2").Value = Nz(Me![4kMatch2])
    .Item("4kRoth").Value = Nz(Me![4kRoth])
    .Item("4kRoth2").Value = Nz(Me![4kRoth2])
    .Item("ReqAnnTrust").Value = Nz(Me![ReqAnnTrust])
    .Item("OtherInfo").Value = Nz(Me![OtherInfo])
    .Item("SHNo").Value = Nz(Me![SHNo])
    .Item("SHNo2").Value = Nz(Me![SHNo2])
    .Item("Spreadsheet").Value = Nz(Me![Spreadsheet])
    .Item("Spreadsheet1").Value = Nz(Me![Spreadsheet1])
    .Item("AnnNotify1").Value = Nz(Me![AnnNotify1])
    .Item("Diskette1").Value = Nz(Me![Diskette1])
    .Item("Diskette").Value = Nz(Me![Diskette])
    .Item("YE").Value = Nz(Me![YE])
    .Item("4kCatchUp").Value = Nz(Me![4kCatchUp])
    .Item("ExclOther").Value = Nz(Me![ExclOther])
    .Item("OtherSpreadsheetInfo").Value = Nz(Me![OtherSpreadsheetInfo])
    .Item("ReqAnnTrustOtherMemo").Value = Nz(Me![ReqAnnTrustOtherMemo])
    .Item("OtherSpreadsheetInfo2").Value = Nz(Me![OtherSpreadsheetInfo2])
    .Item("OtherInfo2").Value = Nz(Me![OtherInfo2])
    .Item("CensusDate").Value = Nz(Me![CensusDate])

    End With

    With appWord
    .Visible = True
    .Activate
    .Selection.WholeStory
    .Selection.Fields.Update
    .Selection.MoveDown Unit:=wdLine, Count:=1
    End With

    The code repeates itself with other IF THEN statements, but the "guts" of the code is the same as above.

    and then the code ends with:

    ErrorHandlerExit:
    Exit Sub

    ErrorHandler:
    If Err = 429 Then
    'Word is not running; open Word with CreateObject
    Set appWord = CreateObject("Word.Application")
    Resume Next
    Else
    MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
    Resume ErrorHandlerExit
    End If

    End Sub

    Thanks for looking into this.... I really appriciate it!

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    The error message complains about the Add method of the Documents object. This means that the line

    Set doc = docs.Add(strTemplateNameAndPath)

    fails. This can happen if strTemplateNameAndPath is not valid. The code also contains a line

    Debug.Print "Template and path: " & strTemplateNameAndPath

    So when the error occurs, look at the Immediate window in the Visual Basic Editor. You should see a line that begins with Template and path:, followed by the path and filename of the template. Check whether this is correct:

    • Does the path exist?
    • Does the file exist?
    • Is it a valid Word template?

  7. #7
    Star Lounger
    Join Date
    May 2003
    Location
    Kansas City, Kansas, USA
    Posts
    76
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    The error message complains about the Add method of the Documents object. This means that the line

    Set doc = docs.Add(strTemplateNameAndPath)

    fails. This can happen if strTemplateNameAndPath is not valid. The code also contains a line

    Debug.Print "Template and path: " & strTemplateNameAndPath

    So when the error occurs, look at the Immediate window in the Visual Basic Editor. You should see a line that begins with Template and path:, followed by the path and filename of the template. Check whether this is correct:

    • Does the path exist?
    • Does the file exist?
    • Is it a valid Word template?

    Hi Hans, I did look at this before hand and all is set up correctly....

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Could the template be in use when the code is run?


  9. #9
    Star Lounger
    Join Date
    May 2003
    Location
    Kansas City, Kansas, USA
    Posts
    76
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    Could the template be in use when the code is run?

    I was just looking into this and noticed that for some reason after I close Word, an instance of it stay on locking the template... If I ctrl, alt, delete, then I see WINWORD and can end process.... How do I keep this from happening??

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    An instance of Word remaining in memory could be caused by an unqualified reference to a Word object somewhere in the code, for example a line

    ActiveDocument.Close

    Because this doesn't refer directly or indirectly to the appWord object created in the code, it will spawn an independent instance of Word. This line should be

    appWord.ActiveDocument.Close

    However, I don't see such an unqualified reference in the code that you posted. Perhaps there is one in the rest of the code.


  11. #11
    Star Lounger
    Join Date
    May 2003
    Location
    Kansas City, Kansas, USA
    Posts
    76
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    An instance of Word remaining in memory could be caused by an unqualified reference to a Word object somewhere in the code, for example a line

    ActiveDocument.Close

    Because this doesn't refer directly or indirectly to the appWord object created in the code, it will spawn an independent instance of Word. This line should be

    appWord.ActiveDocument.Close

    However, I don't see such an unqualified reference in the code that you posted. Perhaps there is one in the rest of the code.
    Thank you I will look it through.

  12. #12
    Star Lounger
    Join Date
    May 2003
    Location
    Kansas City, Kansas, USA
    Posts
    76
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    An instance of Word remaining in memory could be caused by an unqualified reference to a Word object somewhere in the code, for example a line

    ActiveDocument.Close

    Because this doesn't refer directly or indirectly to the appWord object created in the code, it will spawn an independent instance of Word. This line should be

    appWord.ActiveDocument.Close

    However, I don't see such an unqualified reference in the code that you posted. Perhaps there is one in the rest of the code.

    Hey Hans I am not finding any unqualified references, however, now I am experiencing a second button on the same form throwing up errors. I have a second merge identical to the first button but for a different type of letter. I checked for the independent instance of word and it is closed, I have checked all of my references to my templates and they are fine but I am getting an error that says: Error no. 5 Description: Invalid procedure call or argument.
    I am at a loss why all of the sudden these errors are happening...

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Can you find out which line causes the error?


  14. #14
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,624
    Thanks
    3
    Thanked 60 Times in 60 Posts
    Just wanted to let you know that I changed the Title of your thread, as you aren't really doing a Mail Merge to Word, but are actually using OLE Automation to populate a Word template and create a customized document.
    Wendell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •