Run-Time Error '462' - Please Help

JuanFZG

New Member
Joined
Apr 5, 2013
Messages
1
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:
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
:confused: 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
:confused: 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:

:confused: appWord.Selection.Style = ActiveDocument.Styles("Heading 1")

:confused: appWord.Selection.Style = ActiveDocument.Styles("Heading 2")


Thanks a lot to the one who is able to help me.

Juan F.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi welcome to the forum!

Can I suggest that you use [ CODE ] tags in the future to make your code easier to read. It's actually a forum requirement.

Anyway on both of those lines you haven't qualified the ActiveDocument so that could cause it to mess up from time to time. Try like this:

Code:
appWord.Selection.Style = appWord.ActiveDocument.Styles("Heading 1")

Also in my opinion you might be better off extending your 'With appWord' statement then you could write that line as:

Code:
With appWord
    .Selection.Style = .ActiveDocument.Styles("Heading 1")
End With

Just saves a lot less typing as you won't have to put 'appWord' all the time.

HTH!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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