none
Runtime error 462 RRS feed

  • Question

  • Hi all,

    I have been battling with runtime error 462 when automating Word. I tried several solutions as per the forums, but end up with the error that the server is unavailable.

    I tried the solution at http://support.microsoft.com/kb/189618 in a test environment. After running the code for the second time, I still receive this runtime error from Access, although I changed the code as suggested by the author.

    The only difference is that I now get the error message AFTER the second execution of the code. Normally I would receive the message BEFORE the second execution of the code.

    Any suggestions?

    Below my actual code in my application:

        oWord.ActiveDocument.SaveAs "c:\temp\Q & A Sheet.docx"
        oWord.Quit
        MsgBox "The Question and Answer sheet document has been saved in the following location: " & vbCrLf & _
            "C:\Temp\Q & A Sheet.docx", vbInformation, "Document saved"

    Set oWord = Nothing
    Set oDoc = Nothing

    Wednesday, November 29, 2017 1:35 PM

Answers

  • Hello,

    I would get runtime error at  With ActiveDocument.Sections(1) when it runs second time. 

    Please change it into 

     With oWord.ActiveDocument.Sections(1)

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Deon SA Thursday, November 30, 2017 9:58 AM
    Thursday, November 30, 2017 8:49 AM
    Moderator

All replies

  • Hi all,

    Just an update:

    I copied my code from the original application into a new database and ran the code.

    I do not get any error messages!!

    Now I am confused....

    Wednesday, November 29, 2017 2:16 PM
  • Hello,

    As the code works in a new database, i think the issue causes from your original database instead of code itself. You may export objects to the new one. You could also try to remove objects one by one and comment code in your original db to check what object or which code causes the issue. 

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 30, 2017 6:22 AM
    Moderator
  • Hi Celeste,

    I ran the code this morning in the new database and received the same error message. The only difference was that an instance of Word was open when I ran the code yesterday and received no errors, so I tested it again this morning with the following results:

    If no instance of Word is running prior to the execution of my code, the error message pops up as soon as I run the code for the second time.

    If I open any Word document prior to running the code, no error is displayed and I can run the code multiple times with no error, provided I keep an instance of Word open.

    NOTE: When I run my code and receive the error, I also check the task manager and no instance of Word is running. I read somewhere the problem is that Word is not really closed, that is why the error occurs, but according to the task manager, no instance of Word is open.

    Thanks

    Deon

    Thursday, November 30, 2017 6:41 AM
  • Hi Deon

    Do you create a new Word instance in your code like Set oWord = CreateObject("Word.Application") ? If you set its visible into true and run step by step, could you see the instance?

    Could you please share complete code or a sample database here?

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 30, 2017 6:55 AM
    Moderator
  • Hi Celeste,

    I attach the code (which I have modified a bit for the sample DB) as well as the text files from where the data comes from:

    I am not sure how to share the sample database here.

    Private Sub Command1_Click()
        Dim oWord As Word.Application
        Dim oDoc As Word.Document
        Dim oTable As Word.Table
        Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
        Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
        Dim oRng As Word.Range
        Dim oLn As Word.Line
        Dim oShape As Word.InlineShape
        Dim oChart As Object
        Dim Pos As Double
        Dim QNumber, QNumberInDB, Q, QAnswer, QChoice1, QChoice2, QChoice3, QChoice4, QChoice5
    
         'open word file / template and make Word visible
        Set oWord = New Word.Application
        Set oDoc = oWord.Documents.Open("c:\ets africa\q&a.docx", , False, True)
        oWord.Visible = True
        oWord.Activate
        With ActiveDocument.Sections(1)
            .Footers(wdHeaderFooterPrimary).PageNumbers.Add _
            PageNumberAlignment:=wdAlignPageNumberCenter, _
            FirstPage:=True
        End With
        
        'Start Word and open the document template.
        ''Set oWord = CreateObject("Word.Application")
        ''oWord.Visible = True
        ''oWord.Activate
        ''Set oDoc = oWord.Documents.Add
        ''With ActiveDocument.Sections(1)
        ''    .Footers(wdHeaderFooterPrimary).PageNumbers.Add _
        ''    PageNumberAlignment:=wdAlignPageNumberCenter, _
        ''    FirstPage:=True
        ''End With
    
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        ''oRng.ParagraphFormat.SpaceAfter = 6
        
        oRng.InsertAfter "Question Sheet for Course: " '''& Forms!Frm_GenerateTest.Text2
        oRng.Font.Bold = True: oRng.Font.Underline = True
        oRng.InsertParagraphAfter
        oRng.InsertAfter "Section A: True or False"
        oRng.Font.Bold = True: oRng.Font.Underline = True
        oRng.InsertParagraphAfter
        'Get the true false questions
        Open "c:\temp\true_false.txt" For Input As #1
        Input #1, QNumber, QNumberInDB, Q, QAnswer
        While Not EOF(1)
            Input #1, QNumber, QNumberInDB, Q, QAnswer
            oRng.InsertAfter QNumber & ". " & Q
            oRng.InsertParagraphAfter
            oRng.InsertAfter Chr$(91) & Space(3) & Chr$(93) & " True " & Chr$(91) & Space(3) & Chr$(93) & " False"
            oRng.InsertParagraphAfter
        Wend
        Close #1
        oRng.Collapse (wdCollapseEnd)
        oRng.InsertBreak wdPageBreak
        oRng.Collapse wdCollapseEnd
        '-----------------------------------------------------------------
        'Get the multiple choice questions
        ''oRng.InsertBreak wdPageBreak
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        oRng.InsertParagraphAfter: oRng.InsertParagraphAfter
        oRng.InsertAfter "Section B: Multiple Choice"
        oRng.Font.Bold = True: oRng.Font.Underline = True
        oRng.InsertParagraphAfter
        Open "c:\temp\mult_choice.txt" For Input As #1
        Input #1, QNumber, QNumberInDB, Q, QChoice1, QChoice2, QChoice3, QChoice4, QChoice5, QAnswer
        While Not EOF(1)
            Input #1, QNumber, QNumberInDB, Q, QChoice1, QChoice2, QChoice3, QChoice4, QChoice5, QAnswer
            oRng.InsertAfter QNumber & ". " & Q
            oRng.InsertParagraphAfter
            oRng.InsertAfter QChoice1
            oRng.InsertParagraphAfter
            oRng.InsertAfter QChoice2
            oRng.InsertParagraphAfter
            oRng.InsertAfter QChoice3
            oRng.InsertParagraphAfter
            oRng.InsertAfter QChoice4
            oRng.InsertParagraphAfter
            oRng.InsertAfter QChoice5
            oRng.InsertParagraphAfter: oRng.InsertParagraphAfter
        Wend
        Close #1
        oRng.Collapse (wdCollapseEnd)
        oRng.InsertBreak wdPageBreak
        oRng.Collapse wdCollapseEnd
        '-----------------------------------------------------------------
        'Get the Missing word questions
        ''oRng.InsertBreak wdPageBreak
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        oRng.InsertParagraphAfter: oRng.InsertParagraphAfter
        oRng.InsertAfter "Section C: Missing Word"
        oRng.Font.Bold = True: oRng.Font.Underline = True
        oRng.InsertParagraphAfter
        Open "c:\temp\miss_word.txt" For Input As #1
        Input #1, QNumber, QNumberInDB, Q, QAnswer
        While Not EOF(1)
            Input #1, QNumber, QNumberInDB, Q, QAnswer
            oRng.InsertAfter QNumber & ". " & Q
            oRng.InsertParagraphAfter
        Wend
        Close #1
        '-----------------------------------------------------------------
                    
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        oRng.InsertParagraphAfter
        oRng.InsertAfter "***END OF TEST***"
        oRng.Font.Bold = True
        '=================================================================================
    'Create the answer sheet if requested
    ''If Forms!Frm_GenerateTest.Check2.Value = True Then
        ''Set oWord = CreateObject("Word.Application")
        ''oWord.Visible = True
        ''oWord.Activate
        oRng.Collapse (wdCollapseEnd)
        oRng.InsertBreak wdPageBreak
        oRng.Collapse wdCollapseEnd
        ''Set oDoc = oWord.Documents.Add
        ''With ActiveDocument.Sections(1)
        ''    .Footers(wdHeaderFooterPrimary).PageNumbers.Add _
        ''    PageNumberAlignment:=wdAlignPageNumberCenter, _
        ''    FirstPage:=True
        ''End With
    
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        ''oRng.ParagraphFormat.SpaceAfter = 6
        oRng.InsertAfter "Answer Sheet for Course: " '& Forms!Frm_GenerateTest.Text2
        oRng.Font.Bold = True: oRng.Font.Underline = True
        oRng.InsertParagraphAfter
        oRng.InsertAfter "Section A: True or False"
        oRng.InsertParagraphAfter
        oRng.InsertAfter "Question Number" & Space(40) & "Answer"
        oRng.Font.Bold = True: oRng.Font.Underline = True
        oRng.InsertParagraphAfter
        'Get the true false questions
        Open "c:\temp\true_false.txt" For Input As #1
        Input #1, QNumber, QNumberInDB, Q, QAnswer
        While Not EOF(1)
            Input #1, QNumber, QNumberInDB, Q, QAnswer
            oRng.InsertAfter QNumber & ". " & Space(70) & QAnswer
            oRng.InsertParagraphAfter
        Wend
        Close #1
        oRng.Collapse (wdCollapseEnd)
        oRng.InsertBreak wdPageBreak
        oRng.Collapse wdCollapseEnd
        '-----------------------------------------------------------------
        'Get the multiple choice questions
        ''oRng.InsertBreak wdPageBreak
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        oRng.InsertParagraphAfter: oRng.InsertParagraphAfter
        oRng.InsertAfter "Section B: Multiple Choice"
        oRng.InsertParagraphAfter
        oRng.InsertAfter "Question Number" & Space(40) & "Answer"
        oRng.Font.Bold = True: oRng.Font.Underline = True
        oRng.InsertParagraphAfter
        oRng.Font.Bold = True: oRng.Font.Underline = True
        oRng.InsertParagraphAfter
        Open "c:\temp\mult_choice.txt" For Input As #1
        Input #1, QNumber, QNumberInDB, Q, QChoice1, QChoice2, QChoice3, QChoice4, QChoice5, QAnswer
        While Not EOF(1)
            Input #1, QNumber, QNumberInDB, Q, QChoice1, QChoice2, QChoice3, QChoice4, QChoice5, QAnswer
            oRng.InsertAfter QNumber & ". " & Space(70) & QAnswer
            oRng.InsertParagraphAfter: oRng.InsertParagraphAfter
        Wend
        Close #1
        oRng.Collapse (wdCollapseEnd)
        oRng.InsertBreak wdPageBreak
        oRng.Collapse wdCollapseEnd
        '-----------------------------------------------------------------
        'Get the Missing word questions
        ''oRng.InsertBreak wdPageBreak
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        oRng.InsertParagraphAfter: oRng.InsertParagraphAfter
        oRng.InsertAfter "Section C: Missing Word"
        oRng.InsertParagraphAfter
        oRng.InsertAfter "Question Number" & Space(40) & "Answer"
        oRng.Font.Bold = True: oRng.Font.Underline = True
        oRng.InsertParagraphAfter
        oRng.Font.Bold = True: oRng.Font.Underline = True
        oRng.InsertParagraphAfter
        Open "c:\temp\miss_word.txt" For Input As #1
        Input #1, QNumber, QNumberInDB, Q, QAnswer
        While Not EOF(1)
            Input #1, QNumber, QNumberInDB, Q, QAnswer
            oRng.InsertAfter QNumber & ". " & Space(70) & QAnswer
            oRng.InsertParagraphAfter
        Wend
        Close #1
        '-----------------------------------------------------------------
                    
        Set oRng = oDoc.Bookmarks("\endofdoc").Range
        oRng.InsertParagraphAfter
        oRng.InsertAfter "***END OF ANSWER SHEET***"
        oRng.Font.Bold = True
    ''End If
    ''Kill "c:\temp\true_false.txt"
    ''Kill "c:\temp\mult_choice.txt"
    ''Kill "c:\temp\miss_word.txt"
    
    ''If Forms!Frm_GenerateTest.Check2.Value = True Then
        oWord.ActiveDocument.SaveAs "c:\temp\Q & A Sheet.docx"
        oWord.Quit
        MsgBox "The Question and Answer sheet document has been saved in the following location: " & vbCrLf & _
            "C:\Temp\Q & A Sheet.docx", vbInformation, "Document saved"
    ''Else
        ''oWord.ActiveDocument.SaveAs "c:\temp\Question Sheet.docx"
        ''oWord.Quit
        ''MsgBox "The Question sheet document has been saved in the following location: " & vbCrLf & _
            "C:\Temp\Question Sheet.docx", vbInformation, "Document saved"
    ''End If
    Set oWord = Nothing
    Set oDoc = Nothing
    
    
    End Sub
    

    Here is the text files data (there are 3 text files with data). The first file name is "Miss_Word.Txt":

    "Question Number","Question Number in Database","Question","Answer"
    1,6,"The Maximum weight of a MPSF Jack is ____________ kg","1000"
    2,9,"What does the abreviation PPE stands for? ______________________","Personal Protective Equipment"

    The second file name is "Mult_Choice.Txt":

    "Question Number","Question Number in Database","Question","Choice 1","Choice 2","Choice 3","Choice 4","Choice 5","Answer"
    1,8,"What Daily checks must be done on the forklift?","Handbrake","Lights","Indicators","Oil Level","All the above","All the above"
    2,5,"What checks must be performed when receiving new items?","Quality","Quantity","Correct labbeling","Neatness","All of the above","All of the above"
    

    The third text file name is "True_False.Txt":

    "Question Number","Question Number in Database","Question","Answer"
    1,4,"A bullet proof glass screen is a 15mm thick glass","False"
    2,7,"The Seatbelt on a forklift must always be worn","True"
    

    Thanks

    Deon

    Thursday, November 30, 2017 8:02 AM
  • Hello,

    I would get runtime error at  With ActiveDocument.Sections(1) when it runs second time. 

    Please change it into 

     With oWord.ActiveDocument.Sections(1)

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Deon SA Thursday, November 30, 2017 9:58 AM
    Thursday, November 30, 2017 8:49 AM
    Moderator
  • Thanks Celeste - that did the trick (I thought it was just something small that I am overlooking!!)

    Deon

    Thursday, November 30, 2017 9:58 AM