Run Time Error 462 (1 Viewer)

Samantha

still learning...
Local time
Yesterday, 22:55
Joined
Jul 12, 2012
Messages
180
Hello! I have the following code that sends info to a word document to create a label I would like the code to find and highlight text on the label. The first time I run the code it works fine. Any additional occurrences produces runtime error 462 breaking at Selection.HomeKey wdStory. I have been trying to research the line and the error and have come up empty.

Code:
Private Sub cmdMakeFileLabel_Click()
On Error GoTo Error_Handler

Dim ProjectDescriptionLine As String, strCoName As String, sFindText As String
        
    'Declare an instance of MS Word.
    Dim Wrd As New Word.Application
    Set Wrd = CreateObject("Word.Application")
    
    'Specify the path and name to the word document.
    Dim MergeDoc As String
    MergeDoc = Application.CurrentProject.Path
    MergeDoc = MergeDoc & "\FileFolderLabels.dotx"
    
    'Open the word document template, make it visible.
    Wrd.Documents.Add MergeDoc
    Wrd.Visible = True
        
    'Build Project Description Line
    If ([sfrmProjectType].[Form]![ProjectType]) = "TM" Then
    ProjectDescriptionLine = ProjectDescription & " " & "TM"
    Else
    ProjectDescriptionLine = ProjectDescription
    End If
    
    If IsNull([tblLookupCompany.Nickname]) Then
    strCoName = [Company]
Else: strCoName = [tblLookupCompany.Nickname]
End If
    
    'Replace each bookmark with current data.
    With Wrd.ActiveDocument.Bookmarks
        .Item("ServiceAddress").Range.Text = Me!ServiceAddress.Value
        .Item("Company").Range.Text = strCoName
        .Item("JobNumber").Range.Text = JobNumber
        .Item("ProjectDescription").Range.Text = ProjectDescriptionLine

'Start from the top of the document
Selection.HomeKey wdStory
sFindText = "TM"
Selection.Find.Execute sFindText
Do Until Selection.Find.Found = False
 Selection.Range.HighlightColorIndex = wdYellow
 Selection.MoveRight
 Selection.Find.Execute
Loop

  End With
    MsgBox "Your Document is Ready." & vbCrLf & "Please edit accordingly and disregard changes ", vbOKOnly, "Successful"

Exit_Procedure:
Exit Sub

Error_Handler:
    MsgBox "An error has occurred in this application." & " Please contact your technical support with the following information:" & vbCrLf & vbCrLf & "Error Number" & Err.Number & ", " & Err.Description, Buttons:=vbCritical
    
    Resume Exit_Procedure

End Sub

Thanks in advance!
Samantha
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:55
Joined
May 7, 2009
Messages
19,247
I think you need to qualify it:

wrd.Selection.HomeKey wdStory
 

Samantha

still learning...
Local time
Yesterday, 22:55
Joined
Jul 12, 2012
Messages
180
I think you need to qualify it:

wrd.Selection.HomeKey wdStory

Your right! thanks so much :banghead:

Code:
Wrd.Selection.HomeKey wdStory
sFindText = "TM"
Wrd.Selection.Find.Execute sFindText
Do Until Wrd.Selection.Find.Found = False
 Wrd.Selection.Range.HighlightColorIndex = wdYellow
 Wrd.Selection.MoveRight
 Wrd.Selection.Find.Execute
Loop
 

Users who are viewing this thread

Top Bottom