Tenho uma rotina que abre e preenche um arquivo do word com informações de uma tabela no excel.
Tudo roda perfeitamente bem da primeira vez, mas quando rodo a segunda ele apresenta o erro 462. Alguma ideia?
Private Sub btn_montar_contrato_Click()
Dim WdApp As Word.Application
Dim wdDOC As Word.Document
Dim rng As Word.Range
Set wdDOC = Nothing
Dim wdtemp As Word.Document
On Error Resume Next
Set WdApp = GetObject(, "Word.Application")
If Err.Number > 0 Then Set WdApp = CreateObject("Word.Application")
On Error GoTo 0
WdApp.DisplayAlerts = False
WdApp.Visible = True
For i = 10 To 11
Set wdDOC = Word.Documents.Open("C:UserspauloDesktopteste2modelo_recisao.docx", readyonly = True)
With wdDOC
'*Dados locador
.Application.Selection.HomeKey Unit:=wdStory
.Application.Selection.Find.ClearFormatting
.Application.Selection.Find.Forward = True
.Application.Selection.Find.Text = "#nome"
.Application.Selection.Find.Execute , , , , , , , , , Plan3.Range("d" & i), wdReplaceAll
.Application.Selection.HomeKey Unit:=wdStory
.Application.Selection.Find.ClearFormatting
.Application.Selection.Find.Forward = True
.Application.Selection.Find.Text = "#datainic"
.Application.Selection.Find.Execute , , , , , , , , , Plan3.Range("h" & i), wdReplaceAll
.Application.Selection.HomeKey Unit:=wdStory
.Application.Selection.Find.ClearFormatting
.Application.Selection.Find.Forward = True
.Application.Selection.Find.Text = "#datarec"
.Application.Selection.Find.Execute , , , , , , , , , Plan3.Range("k" & i), wdReplaceAll
.Application.Selection.HomeKey Unit:=wdStory
.Application.Selection.Find.ClearFormatting
.Application.Selection.Find.Forward = True
.Application.Selection.Find.Text = "#horario"
.Application.Selection.Find.Execute , , , , , , , , , Plan3.Range("l" & i), wdReplaceAll
.Application.Selection.HomeKey Unit:=wdStory
.Application.Selection.Find.ClearFormatting
.Application.Selection.Find.Forward = True
.Application.Selection.Find.Text = "#nivel"
.Application.Selection.Find.Execute , , , , , , , , , Plan3.Range("m" & i), wdReplaceAll
.Application.Selection.HomeKey Unit:=wdStory
.Application.Selection.Find.ClearFormatting
.Application.Selection.Find.Forward = True
.Application.Selection.Find.Text = "#faculdade"
.Application.Selection.Find.Execute , , , , , , , , , Plan3.Range("n" & i), wdReplaceAll
.Application.Selection.HomeKey Unit:=wdStory
.Application.Selection.Find.ClearFormatting
.Application.Selection.Find.Forward = True
.Application.Selection.Find.Text = "#datafim"
.Application.Selection.Find.Execute , , , , , , , , , Plan3.Range("k" & i), wdReplaceAll
.Application.Selection.HomeKey Unit:=wdStory
.Application.Selection.Find.ClearFormatting
.Application.Selection.Find.Forward = True
.Application.Selection.Find.Text = "#unidade"
.Application.Selection.Find.Execute , , , , , , , , , Plan3.Range("c" & i), wdReplaceAll
.Application.Selection.HomeKey Unit:=wdStory
.Application.Selection.Find.ClearFormatting
.Application.Selection.Find.Forward = True
.Application.Selection.Find.Text = "#curso"
.Application.Selection.Find.Execute , , , , , , , , , Plan3.Range("q" & i), wdReplaceAll
.Application.Selection.HomeKey Unit:=wdStory
.Application.Selection.Find.ClearFormatting
.Application.Selection.Find.Forward = True
.Application.Selection.Find.Text = "#horas"
.Application.Selection.Find.Execute , , , , , , , , , Plan3.Range("t" & i), wdReplaceAll
.Application.Selection.HomeKey Unit:=wdStory
.Application.Selection.Find.ClearFormatting
.Application.Selection.Find.Forward = True
.Application.Selection.Find.Text = "#hext"
.Application.Selection.Find.Execute , , , , , , , , , Plan3.Range("z" & i), wdReplaceAll
.Application.Selection.HomeKey Unit:=wdStory
.Application.Selection.Find.ClearFormatting
.Application.Selection.Find.Forward = True
.Application.Selection.Find.Text = "#datarec"
.Application.Selection.Find.Execute , , , , , , , , , Plan3.Range("w" & i), wdReplaceAll
.Application.Selection.HomeKey Unit:=wdStory
.Application.Selection.Find.ClearFormatting
.Application.Selection.Find.Forward = True
.Application.Selection.Find.Text = "#supervisor"
.Application.Selection.Find.Execute , , , , , , , , , Plan3.Range("v" & i), wdReplaceAll
.SaveAs ("C:UserspauloDesktopteste2" & Range("d" & i).Value & "_RECISAO.docx")
.Close
Set wdDOC = Nothing
End With
Next
WdApp.DisplayAlerts = True
WdApp.Quit acQuitSaveNone
Set wdDOC = Nothing
Set WdApp = Nothing
End Sub