Run-time error 462

Pucci1150

New Member
Joined
Feb 19, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm trying to automate the filling of a word template contract and I keep getting the 462 run-time error. The debug gives me the error colored in red below. I'm hoping for some directions on how to correct my code. I've tried couple of things already but nothing seems to be working. Could the problem be linked to authorisation problems ? The template is on a OneDrive but the authorizations should be fine.

Thanks in advance,:)

Rich (BB code):
Sub RemplirContrat()
    Dim appWord As Object
    Dim doc As Object
    Dim contratTemplate As String

    contratTemplate = "C:\Users\...."

    Set appWord = CreateObject("Word.Application")
    appWord.Visible = True
   
    Set doc = appWord.Documents.Open(contratTemplate)

    If Not doc.ReadOnly Then
        doc.Content.Select
        appWord.Selection.Copy

        Set doc = appWord.Documents.Add
        doc.Range.Paste
    End If

    With doc.Content.Find
        .Execute FindText:="<<Date entrée en vigueur>>", ReplaceWith:=Cells(2, 1).Value, Replace:=wdReplaceAll
        .Execute FindText:="<<Nom Société>>", ReplaceWith:=Cells(2, 2).Value, Replace:=wdReplaceAll
        .Execute FindText:="<<Adresse>>", ReplaceWith:=Cells(2, 3).Value, Replace:=wdReplaceAll
        .Execute FindText:="<<Numéro Entreprise>>", ReplaceWith:=Cells(2, 4).Value, Replace:=wdReplaceAll
        .Execute FindText:="<<Représentant>>", ReplaceWith:=Cells(2, 5).Value, Replace:=wdReplaceAll
        .Execute FindText:="<<Fonction>>", ReplaceWith:=Cells(2, 6).Value, Replace:=wdReplaceAll
        .Execute FindText:="<<<<% dispense>>", ReplaceWith:=Cells(2, 7).Value, Replace:=wdReplaceAll
        .Execute FindText:="<<Titre Client>>", ReplaceWith:=Cells(2, 8).Value, Replace:=wdReplaceAll
        .Execute FindText:="<<Forme Sociale>>", ReplaceWith:=Cells(2, 9).Value, Replace:=wdReplaceAll
        .Execute FindText:="<<Place de signature>>", ReplaceWith:=Cells(2, 10).Value, Replace:=wdReplaceAll
        .Execute FindText:="<<Date du jour>>", ReplaceWith:=Cells(2, 11).Value, Replace:=wdReplaceAll
        .Execute FindText:="<<DPO ou Personne de contact>>", ReplaceWith:=Cells(2, 12).Value, Replace:=wdReplaceAll
    End With

    doc.SaveAs "C:\..."

    appWord.Documents(contratTemplate).Close False

    appWord.Quit
    Set doc = Nothing
    Set appWord = Nothing
End Sub
 
Last edited by a moderator:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Do you have a reference set to the Word object library? I assume not since your code is late bound, which means that you cannot use constants from that library (e.g. wdReplaceAll) without declaring them yourself.

It's also odd to reuse an object variable for another object in the same code.
 
Upvote 0
To be totally honest i'm very unexperienced with VBA and i've mainly been reusing codes i found online so far, and I have not set any references in the word object library.
 
Upvote 0
Try adding this to the top of your code:

Code:
   Const wdReplaceAll As Long = 2

then save the workbook and restart Excel before trying it again (which should allow any hidden Word instances to close)
 
Upvote 0
I did it and i'm still receiving the same error message (run-time error 462). I've tried to look into the references as you mentionned just before but i cannot select it in the Tools section of VBA.
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top