Thank you Sir!
The button I was referring to was the button on the form (continue button) which doesn't work, it does not automatically get the template and open it with the data entered.
I amended the code but it still does not work. I cannot use the button and when I activate the macro from Outlook it gives me a VB error “Run-time error ‘13”: Type mismatch.
When I press “Debug: it outlines the following code line in yellow: If .Tag = 0 Then
I’m guessing the new code line needs a minor adjustment?
Cheers
I have amended the code to include the include what you mentioned, below is what I now have:
Option Explicit
Sub ReplaceFromUserForm()
Dim olItem As MailItem
Dim olEmail As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim i As Long, j As Long
Const sFindText As String = "#Name#|#Desc#|#Acres#|#Value#"
Dim sReplaceText As String
Dim vFind As Variant
With UserForm1
.Caption = "Complete the fields"
.CommandButton1.Caption = "Continue"
.CommandButton2.Caption = "Cancel"
.Show
If .Tag = 0 Then GoTo lbl_Exit
vFind = Split(sFindText, "|")
Set olItem = CreateItemFromTemplate("U:\Operations Centre\Email Templates\form_test.oft")
With olItem
.BodyFormat = olFormatHTML
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
.Display
For j = 0 To UBound(vFind)
Set oRng = wdDoc.Range
Select Case j
Case 0
sReplaceText = UserForm1.TextBox1.Text
Case 1
sReplaceText = UserForm1.TextBox2.Text
Case 2
sReplaceText = UserForm1.TextBox3.Text
Case 3
sReplaceText = UserForm1.TextBox4.Text
Case Else
End Select
With oRng.Find
Do While .Execute(findText:=vFind(j))
oRng.Text = sReplaceText
oRng.collapse 0
DoEvents
Loop
End With
Next j
.Display
End With
End With
Unload UserForm1
lbl_Exit:
Exit Sub
End Sub
|