We help IT Professionals succeed at work.

Runtime Error 462

I just closed this issue because I thought I solved it.  I made it through two loops and then got the runtime error again.  Now I am cosistently getting it on the second loop.  Here is the original message:

I am creating a word document by extracting data from an access database.  The document is very large and as a result, I am having memory issues.  I therefore tried to break it up into sections.  It runs through the first loop fine.  In the second loop, however, I get run time error 462 on the following line  - Rows.Height = InchesToPoint(0.3) in the with block shown below:

With objword.Selection
            .MoveRight unit:=wdCharacter, Count:=23, Extend:=wdExtend
            .Rows.HeightRule = wdRowHeightAtLeast
            .Rows.Height = InchesToPoints(0.3)
with objword.Selection

Below is additional code.  I have researched a number of entries on Expert Exchange concerning this error and have tried to find what is causing it in my code.  But nothing I have tried has worked.  Your help wold be extremely appreciated!
Private Sub cmdTest_Click()
Dim dbs As Database
Dim rst As Recordset
Dim rstR As Recordset
Dim sSQL As String
Dim sTestRange As String
Dim bFndAllergen As Boolean
Dim sBookMark As String
Dim iBMark As Integer
Dim sBMark As String

Set dbs = CurrentDb
Set rstR = dbs.OpenRecordset("TestRange")
rstR.MoveFirst
iBMark = 0
While Not rstR.EOF
    sTestRange = " WHERE Name " & rstR!Range & " AND Name Not Like 'Allergen'"
    sSQL = "SELECT * FROM qryDOS" & sTestRange
    
    Set rst = dbs.OpenRecordset(sSQL)
    bFndAllergen = False
    If iBMark <> 0 Then
        OpenWord
        objword.Selection.Goto What:=wdGoToBookmark, Name:=sBMark
        objword.Selection.MoveRight unit:=wdCell
        objword.Selection.Tables(1).Rows.HeightRule = wdRowHeightAtLeast
    Else
        SetUpDoc
    End If
    iBMark = iBMark + 1
    sBMark = "BookMark" & Trim(Str(iBMark))
        
    Dim i As Integer
    While Not rst.EOF
        If rst!Name > "Allergen" And bFndAllergen = False Then
            With ActiveDocument.Bookmarks
                .Add Range:=Selection.Range, Name:="Allergen"
                .DefaultSorting = wdSortByName
                .ShowHidden = False
            End With
            bFndAllergen = True
        End If
        
        With objword.Selection
            .MoveRight unit:=wdCharacter, Count:=23, Extend:=wdExtend
            .Rows.HeightRule = wdRowHeightAtLeast
            .Rows.Height = InchesToPoints(0.3)
        End With
        With objword.Selection.Cells
            With .Shading
                .Texture = wdTextureNone
                .ForegroundPatternColor = wdColorAutomatic
                .BackgroundPatternColor = wdColorGray25
            End With
        End With
        With objword.Selection
            .Style = ActiveDocument.Styles("Heading 1")
            .Font.Bold = True
            .Font.Underline = wdUnderlineNone
            .TypeText Text:=rst.Fields("Name").Value
            .MoveRight unit:=wdCell
            .MoveRight unit:=wdCell
            .MoveRight unit:=wdCell
            .Style = ActiveDocument.Styles("Normal")
            If Not IsNull(rst!DOSCODE) Then
                .Cells.VerticalAlignment = wdCellAlignVerticalCenter
                .Font.Size = 11
                .Font.Bold = True
                .Font.Underline = wdUnderlineNone
                .TypeText Text:=rst.Fields("DOSCODE").Value
            End If
            .MoveRight unit:=wdCell
        End With
        With objword.Selection
            .MoveRight unit:=wdCharacter, Count:=23, Extend:=wdExtend
            .Rows.HeightRule = wdRowHeightAtLeast
            .Rows.Height = InchesToPoints(0.1)
        End With
        With objword.Selection.Cells
            With .Shading
                .Texture = wdTextureNone
                .ForegroundPatternColor = wdColorAutomatic
                .BackgroundPatternColor = wdColorWhite
            End With
        End With
        objword.Selection.Style = ActiveDocument.Styles("Normal")
        Select Case rst!Order_type
            Case "Single"
                objword.Selection.MoveRight unit:=wdCell
                If Not IsNull(rst!Methods) Then
                    WriteMainCatSingle "Method(s)", rst!Methods
                End If
                If Not IsNull(rst!Synonym) Then
                    WriteMainCatSingle "Synonym", rst!Synonym
                End If
                WriteMainCatSingle "Specimen Required", ""
                If Not IsNull(rst!Type) Then
                    WriteSubCatSingle "Collect", rst!Type
                End If
                If Not IsNull(rst!Temperature) Then
                    WriteSubCatSingle "Transport", rst!Temperature
                End If
                If Not IsNull(rst![Collection_Instructions]) Then
                    WriteSubCatSingle "Remarks", rst![Collection_Instructions]
                End If
                If Not IsNull(rst!Stability) Then
                    WriteSubCatSingle "Stability", rst!Stability
                End If
                If Not IsNull(rst![Unacccond]) Then
                    WriteSubCatSingle "Unacceptable Conditions", rst!Unacccond
                End If
                If Not IsNull(rst!Schedule) Then
                    WriteMainCatSingle "Schedule", rst!Schedule
                End If
                If Not IsNull(rst![Billing Code]) Then
                    WriteMainCatSingle "Billing Code", Str(rst![Billing Code])
                End If
                If Not IsNull(rst!CPTCode) Then
                    WriteMainCatSingle "CPTCode", rst!CPTCode
                End If
                If Not IsNull(rst!Notes) Then
                    WriteMainCatSingle "Notes", rst!Notes
                End If
            Case "X-Ref"
                WriteXRef (rst!Notes)
            Case "Panel"
                objword.Selection.MoveRight unit:=wdCell
                WriteMainCatSingle "Specimen Required", ""
                If Not IsNull(rst!Type) Then
                    WriteSubCatSingle "Collect", rst!Type
                End If
                If Not IsNull(rst!Temperature) Then
                    WriteSubCatSingle "Transport", rst!Temperature
                End If
                If Not IsNull(rst![Collection_Instructions]) Then
                    WriteSubCatSingle "Remarks", rst![Collection_Instructions]
                End If
                If Not IsNull(rst!Stability) Then
                    WriteSubCatSingle "Stability", rst!Stability
                End If
                If Not IsNull(rst![Unacccond]) Then
                    WriteSubCatSingle "Unacceptable Conditions", rst!Unacccond
                End If
                If Not IsNull(rst!Schedule) Then
                    WriteMainCatSingle "Schedule", rst!Schedule
                End If
                If Not IsNull(rst![Billing Code]) Then
                    WriteMainCatSingle "Billing Code", Str(rst![Billing Code])
                End If
                If Not IsNull(rst!CPTCode) Then
                    WriteMainCatSingle "CPTCode", rst!CPTCode
                End If
        End Select
        AddRow
        rst.MoveNext
    Wend
    With objword.Selection
        .MoveRight unit:=wdCharacter, Count:=23, Extend:=wdExtend
    End With

    With ActiveDocument.Bookmarks
        .Add Name:=sBMark, Range:=Selection.Range
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    objdoc.Save
    objdoc.Close
    objword.Quit
    Set objword = Nothing
    Set objdoc = Nothing
    rstR.MoveNext
    'MsgBox (rstR!Range)
Wend

objword.Selection.Goto What:=wdGoToBookmark, Name:="FirstPage"
MsgBox ("Done")
End Sub

Sub SetUpDoc()

On Error GoTo ErrorHandler

Kill "\\Ksqldb\dbapps\KathyTest\DOS Rewrite\DOS.doc"

Set objword = GetObject(, "Word.Application")
Set objdoc = objword.Documents.Add("\\Ksqldb\dbapps\KathyTest\DOS Rewrite\DOS.dot")
objdoc.SaveAs "\\Ksqldb\dbapps\KathyTest\DOS Rewrite\DOS.DOC"
objword.Visible = True

objword.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
With ActiveDocument.Bookmarks
    .Add Range:=Selection.Range, Name:="Beginning"
    .DefaultSorting = wdSortByName
    .ShowHidden = False
End With

With objword.AutoCorrect
    .CorrectSentenceCaps = False
End With
With objword.Options
    .CheckSpellingAsYouType = False
    .CheckGrammarAsYouType = False
    .SuggestSpellingCorrections = False
    .CheckGrammarWithSpelling = False
End With
ActiveDocument.ShowGrammaticalErrors = False
ActiveDocument.ShowSpellingErrors = False

objword.Selection.InsertBreak Type:=wdSectionBreakNextPage
With ActiveDocument.Bookmarks
    .Add Range:=Selection.Range, Name:="FirstPage"
    .DefaultSorting = wdSortByName
    .ShowHidden = False
End With
CancelSameAsHeaderFooter
SetTOCHeader
objword.Selection.Goto What:=wdGoToBookmark, Name:="FirstPage"
AddPageNos
objdoc.Tables.Add Range:=objword.Selection.Range, NumRows:=1, NumColumns:= _
        4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
        
With objdoc.Tables(1)
    .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    .Borders(wdBorderRight).LineStyle = wdLineStyleNone
    .Borders(wdBorderTop).LineStyle = wdLineStyleNone
    .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
    .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
    .Borders(wdBorderVertical).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
    .Borders.Shadow = False
End With

ErrorHandlerExit:
   Exit Sub

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

'Set objword = CreateObject("Word.Application")

End Sub

Sub OpenWord()
On Error GoTo OpenWordErrHandler

Set objword = Nothing
Set objword = GetObject(, "word.application")
Set objdoc = objword.Documents.Add("\\Ksqldb\dbapps\KathyTest\DOS Rewrite\DOS.DOC")
objdoc.SaveAs "\\Ksqldb\dbapps\KathyTest\DOS Rewrite\DOS.DOC"
objword.Visible = True

OpenWordErrHandlerExit:
   Exit Sub

OpenWordErrHandler:
If Err = 53 Then
        Resume Next
Else
    If Err = 429 Then
  'Word is not running; open Word with CreateObject
        Set objword = CreateObject("Word.Application")
        Resume Next
    Else
        MsgBox "Error No: " & Err.Number _
        & " in *** procedure" _
        & "; Description: " & Err.Description
        Resume OpenWordErrHandlerExit
    End If
End If
End Sub

Open in new window

Comment
Watch Question

Commented:
OK I really solved it this time.  I changed the following:

Changed   .Rows.Height = InchesToPoints(0.3)
to  .Rows.Height = objword.InchesToPoints(0.3)
AND
Changed   .Style = ActiveDocument.Styles("Heading 1")
to .Style = objword.ActiveDocument.Styles("Heading 1")

(i.e. added objword before inchestoPoints(0,3) and before ActiveDocument.