Paste from Excel into word with correct formatting

Choch

New Member
Joined
Mar 6, 2018
Messages
7
Hey everyone,


I have the code below, but I have an issue with it. It copies correctly from My workbook into the desired Word Document, and to the desired bookmark, but it overwrites all of the contents of the Bookmarked Table. The table
has 4 lines of Headers and Info, but then I want all of it pasted below that in the Word Doc table format. below the 4 rows of Headers, there is 5 columns and the three columns I am copying from Excel should be pasted in Columns 2-4.


Any help is much appreciated!




Sub SendDatatoWord()
Dim MyRange As Excel.Range
Dim wd As Word.Application
Dim wdDoc As Word.Document
Dim wdRange As Word.Range
'Dim strdocname As String

Sheets("Inputs").Range("F3:H15").Copy

Set wd = New Word.Application
wd.Visible = True
'strdocname = ("C:\Users\1130569\Documents")
Set wdDoc = wd.Documents.Open("<path to file>\<document I want opened>.docm")


Set wdRange = wdDoc.Bookmarks("bk1").Range

On Error Resume Next
wdRange.Tables(1).delete
wdRange.Paste

'Adjust Column Widths
wdRange.Tables(1).Columns.SetWidth _
(MyRange.Width / MyRange.Columns.Count), wdAdjustSameWidth

wdDoc.Bookmarks.Add "bk1", wdRange

Set wd = Nothing
Set wdDoc = Nothing
Set wdRange = Nothing

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,224,454
Messages
6,178,766
Members
452,875
Latest member
Disastrouscoder

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