Hello again all, I'm back again with a small problem that I'm encountering when I run the macro I have been tinkering with more than once.
I run the macro and it populates a word document for me with no problem, however when I close the test word documents and run the macro again I get a Run-time error '462'
I have done a bit of reading and I believe this error is generated by the word application not closing down and running in the background (although I'm not really very sure about how or why this would be occuring). If anyone has come accross this sort of error before or can see any glaring problems in my code I'd really appreciate a few pointers, thanks in advance. K
the offending line is the
I have no idea why a formatting issue would throw an error, any ideas?
I run the macro and it populates a word document for me with no problem, however when I close the test word documents and run the macro again I get a Run-time error '462'
I have done a bit of reading and I believe this error is generated by the word application not closing down and running in the background (although I'm not really very sure about how or why this would be occuring). If anyone has come accross this sort of error before or can see any glaring problems in my code I'd really appreciate a few pointers, thanks in advance. K
Code:
Private Sub CommandButton4_Click()
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objExcel As Object
Dim theCombo As Control
Dim x As Integer
Dim y As Integer
Dim Txt1 As String
Dim Txt2 As String
Dim Txt1Rplce As String
Dim Txt2Rplce As String
x = iAddedCount
y = 1
Set objWord = CreateObject("Word.Application")
objWord.DisplayAlerts = False
objWord.Visible = True
objWord.Documents.Open ActiveWorkbook.Path & "\OSR Advisory Letter.dotm", ReadOnly:=True '"\test.dotx", ReadOnly:=True'"\Doc.dotm", ReadOnly:=True '"\test.dotx", ReadOnly:=True
objWord.Activate
objWord.Selection.Start = 1382
For Each theCombo In OSRData.MultiPage1.Pages(2).Controls
If x > 0 Then
objWord.Selection.FormFields.Add Range:=objWord.Selection.Range, Type:= _
wdFieldFormTextInput
With objWord.Dialogs(wdDialogFormFieldOptions)
.Name = ("Com" & y)
.Execute
objWord.Selection.ParagraphFormat.LeftIndent = InchesToPoints(0.63) 'not working throws an error when run a second time
objWord.Selection.Font.Bold = False
objWord.Selection.MoveRight
objWord.Selection.TypeParagraph
objWord.Selection.TypeParagraph
End With
objWord.Selection.FormFields.Add Range:=objWord.Selection.Range, Type:= _
wdFieldFormTextInput
With objWord.Dialogs(wdDialogFormFieldOptions)
.Name = ("Text1" & y)
.Execute
objWord.Selection.ParagraphFormat.LeftIndent = InchesToPoints(0.63) 'this line does not seem to generate the error when the previous line is commented out
objWord.Selection.Font.Bold = True
objWord.Selection.MoveRight
objWord.Selection.TypeParagraph
objWord.Selection.TypeParagraph
End With
objWord.Selection.FormFields.Add Range:=objWord.Selection.Range, Type:= _
wdFieldFormTextInput
With objWord.Dialogs(wdDialogFormFieldOptions)
.Name = ("Text2" & y)
.Execute
'objWord.Selection.ParagraphFormat.LeftIndent = InchesToPoints(0.63)
objWord.Selection.Font.Bold = True
objWord.Selection.MoveRight
objWord.Selection.TypeParagraph
objWord.Selection.TypeParagraph
End With
With objWord.ActiveDocument
Txt1 = Me("Text1" & y).Text
Txt2 = Me("Text2" & y).Text
Txt1Rplce = Replace(Txt1, vbLf, "")
Txt2Rplce = Replace(Txt2, vbLf, "")
objWord.ActiveDocument.FormFields("Com" & y).Result = Me("Combo" & y).Text
objWord.ActiveDocument.FormFields("Text1" & y).Result = "Observations:" & vbCr & Txt1Rplce 'Me("Text1" & y).Text
objWord.ActiveDocument.FormFields("Text2" & y).Result = "Action(s):" & vbCr & Txt2Rplce 'Me("Text2" & y).Text
End With
End If
y = y + 1
x = x - 1
Next theCombo
End Sub
the offending line is the
Code:
objWord.Selection.ParagraphFormat.LeftIndent = InchesToPoints(0.63)
I have no idea why a formatting issue would throw an error, any ideas?