sharky12345
Well-known Member
- Joined
- Aug 5, 2010
- Messages
- 3,431
- Office Version
- 2016
- Platform
- Windows
I've managed to get hold of the following code that will copy the values from a range and paste them to a Word document, what I need to do if possible is only copy the rows that have data in column A.
Additionally, the code should only copy across to column R - so to summarise, copy ALL columns from A to R and only if A has data in it, (starting at A2).
Is it possible?
Additionally, the code should only copy across to column R - so to summarise, copy ALL columns from A to R and only if A has data in it, (starting at A2).
Is it possible?
Code:
Option Explicit
Sub Data2Word()
'Remember: this code requires a referece to the Word object model
'dimension some local variables
Dim rng As Range 'our source range
Dim wdApp As New Word.Application 'a new instance of Word
Dim wdDoc As Word.Document 'our new Word document
Dim t As Word.Range 'the new table in Word as a range
Dim myWordFile As String 'path to Word template
'initialize the Word template path
'here, it's set to be in the same directory as our source workbook
myWordFile = ThisWorkbook.Path & "\DocWithTableStyle.dot"
'get the range of the contiguous data from Cell A1
Set rng = Range("A2").CurrentRegion
'you can do some pre-formatting with the range here
rng.HorizontalAlignment = xlCenter 'center align the data
rng.Copy 'copy the range
'open a new word document from the template
Set wdDoc = wdApp.Documents.Add(myWordFile)
Set t = wdDoc.Content 'set the range in Word
t.Paste 'paste in the table
With t 'working with the table range
.Style = "GreenBar" 'set the style created for the table
'we can use the range object to do some more formatting
'here, I'm matching the table with using the Excel range's properties
.Tables(1).Columns.SetWidth (rng.Width / rng.Columns.Count), wdAdjustSameWidth
End With
'until now the Word app has been a background process
wdApp.Visible = True
'we could use the Word app object to finish off
'you may also want to things like generate a filename and save the file
wdApp.Activate
End Sub