Hello,
I have found some code online which I have managed to tailor to my needs but I just need it tweaked very slightly.
The problem I have is the VBA only works properly is there isn't another Word Doc open. If there is another Doc open, it will paste the excel contents into the already opened Doc, not the desired Doc.
I need the VBA to activate the Complaint_Letter_Template_V1.docm before proceeding with the paste.
Thank you very much.
Here is the code;
Sub Generate_Letter()
'
' Generate_Letter Macro
'
Dim Wdoc As Object
Dim Cell As Range
'On Error Resume Next
Set Wdoc = GetObject("K:\COMPLAINTS\Complaint Handling Letter Templates\Complaint_Letter_Template_V1.docm")
Set WdApp = Wdoc.Parent
If Wdoc Is Nothing Then
Set Wdoc = WdApp.Documents.Add
End If
WdApp.Visible = True
'write cells to Word
With Wdoc.Content
.Delete
WdApp.Options.ReplaceSelection = False
For Each Cell In Range("Letter_Range")
With WdApp.Selection
.TypeText Text:=Cell.Text
.TypeParagraph
End With
Next Cell
End With
Set Wdoc = Nothing
End Sub
I have found some code online which I have managed to tailor to my needs but I just need it tweaked very slightly.
The problem I have is the VBA only works properly is there isn't another Word Doc open. If there is another Doc open, it will paste the excel contents into the already opened Doc, not the desired Doc.
I need the VBA to activate the Complaint_Letter_Template_V1.docm before proceeding with the paste.
Thank you very much.
Here is the code;
Sub Generate_Letter()
'
' Generate_Letter Macro
'
Dim Wdoc As Object
Dim Cell As Range
'On Error Resume Next
Set Wdoc = GetObject("K:\COMPLAINTS\Complaint Handling Letter Templates\Complaint_Letter_Template_V1.docm")
Set WdApp = Wdoc.Parent
If Wdoc Is Nothing Then
Set Wdoc = WdApp.Documents.Add
End If
WdApp.Visible = True
'write cells to Word
With Wdoc.Content
.Delete
WdApp.Options.ReplaceSelection = False
For Each Cell In Range("Letter_Range")
With WdApp.Selection
.TypeText Text:=Cell.Text
.TypeParagraph
End With
Next Cell
End With
Set Wdoc = Nothing
End Sub
Last edited: