Export Excel-range to MS Word - Automation - Part I

XL-Dennis

Well-known Member
Joined
Jul 27, 2002
Messages
1,920
Hi group,

This solution is lookin for a problem to be solved :)

However, I think this might be of general interest for the board and therefore I publish it here.

The key for the solution is to paste the range with the placement of wdInline. By that we can easily delete it and replace it with a new when necessary since it become a part of the InlineShapes-collection in the document.

To avoid any misunderstanding I explicit declare following:
- Yes, I know we can do it manually :)
- Yes, I don´t claim it´s the best solution :)

I welcome any suggestions & improvements:

<PRE>
<FONT color=blue>Sub </FONT>Excel_Range_Word()

<FONT color=#ff0000>'Here we use early binding which means that a
</FONT>
<FONT color=#ff0000>'reference must be set to MS Word Object Library x.x
</FONT>
<FONT color=#ff0000>'via Tools |Reference in the VB-editor
</FONT>
<FONT color=blue>Dim </FONT>wbBook<FONT color=blue> As</FONT><FONT color=blue> Workbook</FONT>

<FONT color=blue>Dim </FONT>wsSheet<FONT color=blue> As</FONT> Worksheet

<FONT color=blue>Dim </FONT>rnReport<FONT color=blue> As</FONT> Range

<FONT color=blue>Dim </FONT>wdApp<FONT color=blue> As</FONT> Word.Application

<FONT color=blue>Dim </FONT>wdDoc<FONT color=blue> As</FONT> Word.Document

<FONT color=blue>Dim </FONT>BMRange<FONT color=blue> As</FONT> Word.Range

<FONT color=blue>Dim </FONT>oShape<FONT color=blue> As</FONT> Word.InlineShape



<FONT color=blue>Set </FONT>wbBook = ThisWorkbook

<FONT color=blue>Set </FONT>wsSheet = wbBook.Worksheets("Sheet")



<FONT color=blue>With </FONT>wsSheet

<FONT color=blue>Set </FONT>rnReport = .Range("Rapport")

<FONT color=blue>End With</FONT>



Application.ScreenUpdating =<FONT color=blue> False</FONT>



rnReport.Copy



<FONT color=blue>Set </FONT>wdApp = CreateObject("Word.Application")



<FONT color=blue>Set </FONT>wdDoc = wdApp.Documents.Open(ThisWorkbook.Path & "Dennis.doc")



<FONT color=#ff0000>'Here we assume that it exist only one OnlineShape and it must be
</FONT>
<FONT color=#ff0000>'deleted before the new report is inserted.
</FONT>
<FONT color=blue>With </FONT>ActiveDocument.InlineShapes(1)

.Select

.Delete

<FONT color=blue>End With</FONT>



<FONT color=blue>Set </FONT>BMRange = ActiveDocument.Bookmarks("Rapport").Range



<FONT color=blue>With </FONT>BMRange

.Select

.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _

Placement:=wdInLine, DisplayAsIcon:=False

<FONT color=blue>End With</FONT>



<FONT color=blue>With </FONT>wdApp.ActiveDocument

.Save

.Close

<FONT color=blue>End With</FONT>



wdApp.Quit



<FONT color=blue>Set </FONT>BMRange =<FONT color=blue> Nothing</FONT>

<FONT color=blue>Set </FONT>wdDoc =<FONT color=blue> Nothing</FONT>

<FONT color=blue>Set </FONT>wdApp =<FONT color=blue> Nothing</FONT>



<FONT color=blue>With </FONT>Application

.CutCopyMode =<FONT color=blue> False</FONT>

.ScreenUpdating =<FONT color=blue> True</FONT>

<FONT color=blue>End With</FONT>



MsgBox "The range has successfully been copied to Dennis.doc", vbInformation



<FONT color=blue>End Sub</FONT>


</PRE>

Kind regards,
Dennis
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,221,321
Messages
6,159,218
Members
451,547
Latest member
loop98

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