Hi everyone,
I am new here and I really need your help.
I am creating a excel macro to copy some information to a word file as plain text, after that, I am trying to format the pasted text.
The macro runs fine once, the second time I try to run it, I get the run-time error '462'. If I tried to run it for the third time it works, but if I tried to do it a new time I got runtime error '462' again. And keeps like that ...
The macro code is the following:
Juan F.
I am new here and I really need your help.
I am creating a excel macro to copy some information to a word file as plain text, after that, I am trying to format the pasted text.
The macro runs fine once, the second time I try to run it, I get the run-time error '462'. If I tried to run it for the third time it works, but if I tried to do it a new time I got runtime error '462' again. And keeps like that ...
The macro code is the following:
Sub Macro5()
Dim appWord As Word.Application
Set appWord = New Word.Application
Dim Inicio As Long
Dim F As Integer
With appWord
.Visible = True
.Activate
End With
appWord.Documents.Add
Inicio = 2
For F = 1 To 10
Range("C" & Inicio).Select
Selection.Copy
appWord.Selection.TypeParagraph
appWord.Selection.PasteSpecial
appWord.Selection.MoveUp Unit:=wdParagraph, Count:=1, Extend:=wdExtend
appWord.Selection.Style = ActiveDocument.Styles("Heading 1")
appWord.Selection.MoveDown Unit:=wdLine, Count:=1
appWord.Selection.TypeParagraph
rango = "H" & Inicio & ":H" & Inicio + 3
Range(rango).Select
Selection.Copy
appWord.Selection.PasteAndFormat (wdFormatPlainText)
appWord.Selection.MoveUp Unit:=wdParagraph, Count:=4, Extend:=wdExtend
appWord.Selection.Style = ActiveDocument.Styles("Heading 2")
appWord.Selection.MoveDown Unit:=wdLine, Count:=1
appWord.Selection.TypeParagraph
Inicio = Inicio + 4
Application.CutCopyMode = False
Next F
appWord.Selection.WholeStory
appWord.Selection.Font.Name = "Arial"
appWord.Selection.Font.Size = 11
Set appWord = Nothing
Range("A1").Select
End Sub
The error is appearing in one of the following two lines:Dim appWord As Word.Application
Set appWord = New Word.Application
Dim Inicio As Long
Dim F As Integer
With appWord
.Visible = True
.Activate
End With
appWord.Documents.Add
Inicio = 2
For F = 1 To 10
Range("C" & Inicio).Select
Selection.Copy
appWord.Selection.TypeParagraph
appWord.Selection.PasteSpecial
appWord.Selection.MoveUp Unit:=wdParagraph, Count:=1, Extend:=wdExtend
appWord.Selection.Style = ActiveDocument.Styles("Heading 1")
appWord.Selection.MoveDown Unit:=wdLine, Count:=1
appWord.Selection.TypeParagraph
rango = "H" & Inicio & ":H" & Inicio + 3
Range(rango).Select
Selection.Copy
appWord.Selection.PasteAndFormat (wdFormatPlainText)
appWord.Selection.MoveUp Unit:=wdParagraph, Count:=4, Extend:=wdExtend
appWord.Selection.Style = ActiveDocument.Styles("Heading 2")
appWord.Selection.MoveDown Unit:=wdLine, Count:=1
appWord.Selection.TypeParagraph
Inicio = Inicio + 4
Application.CutCopyMode = False
Next F
appWord.Selection.WholeStory
appWord.Selection.Font.Name = "Arial"
appWord.Selection.Font.Size = 11
Set appWord = Nothing
Range("A1").Select
End Sub
appWord.Selection.Style = ActiveDocument.Styles("Heading 1")
appWord.Selection.Style = ActiveDocument.Styles("Heading 2")
Thanks a lot to the one who is able to help me.appWord.Selection.Style = ActiveDocument.Styles("Heading 2")
Juan F.