Re-Posted - Please help tweak my VBA (Copy/Paste from Excel to Word)

Status
Not open for further replies.

Shales

Board Regular
Joined
Aug 8, 2006
Messages
171
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
This is a duplicate of: https://www.mrexcel.com/forum/excel-questions/1028605-help-tweak-my-vba-excel-pasting-word.html
Please read Mr Excel's policy against Duplicate Posting in rule 12: http://www.mrexcel.com/forum/board-announcements/99490-forum-rules.html
Thread closed. You may continue the discussion in the original thread - which might have attracted more extensive responses had you posted in the correct forum (https://www.mrexcel.com/forum/general-excel-discussion-other-questions/ - as it's an Office Automation question).
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,215,222
Messages
6,123,717
Members
449,116
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top