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.
Thanks in advance!
Samantha
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