R
RB Smissaert
I have the following code to loop through an Excel array and write the
elements to Word documents:
Sub PasteToWord()
Dim i As Long
Dim arr
Dim LR As Long
Dim strPCT As String
Dim wd As Word.Application
'need to add Word here as there also is a DAO Document!
Dim Doc As Word.Document
Dim oTextFrame As Word.TextFrame
LR = Cells(65536, 1).End(xlUp).Row
arr = Range(Cells(1), Cells(LR, 1))
Set wd = New Word.Application
Set Doc = wd.Documents.Open(Left$(Application.Path, 1) &
":\PCT\PCT_Letter.doc")
Set oTextFrame = Doc.Shapes("txtBox").TextFrame
For i = 1 To 4
strPCT = arr(i, 1)
'Doc.Shapes("txtBox").TextFrame.TextRange.Text = strPCT
'Doc.Shapes("txtBox").TextFrame.AutoSize = True
'Doc.Shapes("txtBox").TextFrame.WordWrap = True
'With oTextFrame
'.TextRange.Text = strPCT
'.AutoSize = True
'.WordWrap = True
'End With
oTextFrame.TextRange.Text = strPCT
oTextFrame.AutoSize = True
oTextFrame.WordWrap = True
Doc.SaveAs Left$(Application.Path, 1) & _
":\PCT\" & _
Left$(strPCT, InStr(1, strPCT, "PCT", vbBinaryCompare) +
2) & _
".doc"
Next
Doc.Close
'this still leaves a running intance of Word
'-------------------------------------------
Set oTextFrame = Nothing
Set Doc = Nothing
Set wd = Nothing
End Sub
It works fine, but after running this there still is a running instance of
Word.
I thought I had released all the references to the Word application, so why
is this happening?
Thanks for any advice.
RBS
elements to Word documents:
Sub PasteToWord()
Dim i As Long
Dim arr
Dim LR As Long
Dim strPCT As String
Dim wd As Word.Application
'need to add Word here as there also is a DAO Document!
Dim Doc As Word.Document
Dim oTextFrame As Word.TextFrame
LR = Cells(65536, 1).End(xlUp).Row
arr = Range(Cells(1), Cells(LR, 1))
Set wd = New Word.Application
Set Doc = wd.Documents.Open(Left$(Application.Path, 1) &
":\PCT\PCT_Letter.doc")
Set oTextFrame = Doc.Shapes("txtBox").TextFrame
For i = 1 To 4
strPCT = arr(i, 1)
'Doc.Shapes("txtBox").TextFrame.TextRange.Text = strPCT
'Doc.Shapes("txtBox").TextFrame.AutoSize = True
'Doc.Shapes("txtBox").TextFrame.WordWrap = True
'With oTextFrame
'.TextRange.Text = strPCT
'.AutoSize = True
'.WordWrap = True
'End With
oTextFrame.TextRange.Text = strPCT
oTextFrame.AutoSize = True
oTextFrame.WordWrap = True
Doc.SaveAs Left$(Application.Path, 1) & _
":\PCT\" & _
Left$(strPCT, InStr(1, strPCT, "PCT", vbBinaryCompare) +
2) & _
".doc"
Next
Doc.Close
'this still leaves a running intance of Word
'-------------------------------------------
Set oTextFrame = Nothing
Set Doc = Nothing
Set wd = Nothing
End Sub
It works fine, but after running this there still is a running instance of
Word.
I thought I had released all the references to the Word application, so why
is this happening?
Thanks for any advice.
RBS