Ask a questionAsk a question
 

QuestionMicrsoft Word Object 11.0 Library Errors

  • Friday, November 06, 2009 7:47 PMHealthCareAnalyst Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello,

    I have written a program in VB6.0 that creates a new word document, pulls records from a database and writes information from the record to the Word ducument - essentially this is creating a directorty listing of people.  The problem I often run into is I get word 'Method' errors, i.e. Error - '.Tab' method of object falied, or '.paragraphformat.spacebefore' Object method failed.  It seems to happen when the document has a large number paragraphs (1,000+) and they happen with increasing frequency once one is generated.  It alomst seems like the VB program looses connection with Word.

    Any ideas?

    To create the word document, I am using the following code:

    Dim wApp as Word.Application
    Dim wDoc as Word.Document
    Dim oSec as Word.Section
    Dim Rng as Word.Range

    Set wApp = new Word.Application
    set wDoc = wApp.document.add

    As I cycle throught the recordset, I am using the follwing code to write their name on one line, and then their address on the next.  Prior to writing their address inforomation, I am setting the Tab properties for the range to right aligned with leaderdots so that telephone number is displayed to the right side of the page with leader dots extending from the address.  I am also marking the Name for an index at the end.

                Set oSec = wDoc.Sections(wDoc.Sections.Count)
                
    While not Rs.EOF

                'Insert Name
                oSec.Range.InsertParagraphAfter
                Set Rng = wDoc.Paragraphs(wDoc.Paragraphs.Count).Range
                oSec.Range.InsertAfter sName  'sName is the variable holding the persons name - assigned from db Record
                Rng.End = wDoc.Sentences(wDoc.Sentences.Count).End
                'Mark the range for the index
                wDoc.Indexes.MarkEntry Range:=Rng, Entry:=sName, Italic:=True
                
                'Insert Address Line                            
                oSec.Range.InsertParagraphAfter
                Set Rng = wDoc.Paragraphs(wDoc.Paragraphs.Count).Range
                Rng.MoveEnd Unit:=wdParagraph, Count:=wDoc.Paragraphs.Count

                Call FormatText(Rng, sFontName, 9, False, False, 1#, 0, 0, "Normal")
                                     
                Rng.ParagraphFormat.TabStops.Add Position:=InchesToPoints(6#), _
                                        Alignment:=wdAlignTabRight, Leader:=wdTabLeaderDots '5.49
                                        
               oSec.Range.InsertAfter sPrintStr              'sPrintStr is a variable that contains the Address & vbtqab & telephone string fron the Db Record
               Rng.End = wDoc.Sentences(wDoc.Sentences.Count).End
               
               Rs.MoveNext

    Wend


    This is the FormatText Function that sets for the range format

    Sub FormatText(oRng As Range, sFont As String, iSize As String, bBold As Boolean, bItalic As Boolean, sgIndent As Single, iSpaceBefore As Integer, iSpaceAfter As Integer, sStyle As String)
        On Error GoTo Err_Resume
        With oRng
            If sStyle <> "" Then
                .Style = wDoc.Styles(sStyle)
           
            End If
           
            If Left(sStyle, 7) <> "Heading" Then
                .Font.Bold = bBold
                .Font.Italic = bItalic
               
                wDoc.Save
                .ParagraphFormat.SpaceBefore = iSpaceBefore
                .ParagraphFormat.SpaceAfter = iSpaceAfter
           
            End If
           
            wDoc.Save
            .ParagraphFormat.LeftIndent = InchesToPoints(sgIndent)
            .Font.Size = iSize
            .Font.Name = sFont
           
        End With
       
        Exit Sub
       
    Err_Resume:
        Resume Next
       
    End Sub