ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Exporting text to a Word document (https://www.excelbanter.com/excel-programming/297030-exporting-text-word-document.html)

Ian M[_2_]

Exporting text to a Word document
 
I have borrowed this code from a Microsoft CD and I am trying to alter
it so that it will select a range from my Excel workbook, say A1 to
F16, and show it as a table in Word.

At the minute, it only take the first cell and puts it in word.

Sub ExportFiguresToReport()
Dim oDocument As Word.Document
Dim sTitle As String, sResult As String

' IS IT THESE TWO LINES WHICH I NEED TO CHANGE?

sTitle = ActiveSheet.Cells(1, 1).Text
sResult = ActiveSheet.Cells(6, 2).Text
Set oDocument = CreateObject("Word.Document")
With oDocument
With .Application
If .Visible = False Then .Visible = True
.WindowState = wdWindowStateMaximize
.Activate
End With
With .Paragraphs(1).Range
.Text = sTitle & Chr(13)
.Style = "Heading 1"
.Font.Underline = wdUnderlineSingle
End With
With .Paragraphs.Add.Range
.Text = "Total: " & sResult & Chr(13)
.Style = "Heading 2"
End With
End With
End Sub

Tom Ogilvy

Exporting text to a Word document
 
Turn on the macro recorder in Word.

go to Excel and select your range.

do a paste or pastespecial in word (the format that gives you want you
want).

Turn off the macro recorder.

this should give you the basic code for putting the table in word.

Now put in the excel code to get the range in the clipboard.

--
Regards,
Tom Ogilvy

"Ian M" wrote in message
om...
I have borrowed this code from a Microsoft CD and I am trying to alter
it so that it will select a range from my Excel workbook, say A1 to
F16, and show it as a table in Word.

At the minute, it only take the first cell and puts it in word.

Sub ExportFiguresToReport()
Dim oDocument As Word.Document
Dim sTitle As String, sResult As String

' IS IT THESE TWO LINES WHICH I NEED TO CHANGE?

sTitle = ActiveSheet.Cells(1, 1).Text
sResult = ActiveSheet.Cells(6, 2).Text
Set oDocument = CreateObject("Word.Document")
With oDocument
With .Application
If .Visible = False Then .Visible = True
.WindowState = wdWindowStateMaximize
.Activate
End With
With .Paragraphs(1).Range
.Text = sTitle & Chr(13)
.Style = "Heading 1"
.Font.Underline = wdUnderlineSingle
End With
With .Paragraphs.Add.Range
.Text = "Total: " & sResult & Chr(13)
.Style = "Heading 2"
End With
End With
End Sub





All times are GMT +1. The time now is 06:16 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com