PDA

View Full Version : Copy worksheet information to Word using VBA in Microsoft Excel



thedark123
06-16-2006, 03:15 AM
How do i modify this piece of code that i got from here:
http://www.exceltip.com/exceltips.php?view=print_page&ID=481

How do I loop through every column in a worksheet and for every column of cells it copied it will be pasted to a fresh new page in a new word document?

Here are 2 screenshots: (Hope it helps ^^)

Here is a bigger picture of the first screenshot:

http://i6.photobucket.com/albums/y226/thedark123/wholetest.gif



copy this over and format into this


http://i6.photobucket.com/albums/y226/thedark123/test1234.gif




and format into this

http://i6.photobucket.com/albums/y226/thedark123/screenshot2new.gif





Sub CopyWorksheetsToWord()
' requires a reference to the Word Object library:
' in the VBE select Tools, References and check the Microsoft Word X.X object library
Dim wdApp As Word.Application, wdDoc As Word.Document, ws As Worksheet
Application.ScreenUpdating = False
Application.StatusBar = "Creating new document..."
Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Add
For Each ws In ActiveWorkbook.Worksheets
Application.StatusBar = "Copying data from " & ws.Name & "..."
ws.UsedRange.Copy ' or edit to the range you want to copy
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste
Application.CutCopyMode = False
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
' insert page break after all worksheets except the last one
If Not ws.Name = Worksheets(Worksheets.Count).Name Then
With wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range
.InsertParagraphBefore
.Collapse Direction:=wdCollapseEnd
.InsertBreak Type:=wdPageBreak
End With
End If
Next ws
Set ws = Nothing
Application.StatusBar = "Cleaning up..."
' apply normal view
With wdApp.ActiveWindow
If .View.SplitSpecial = wdPaneNone Then
.ActivePane.View.Type = wdNormalView
Else
.View.Type = wdNormalView
End If
End With
Set wdDoc = Nothing
wdApp.Visible = True
Set wdApp = Nothing
Application.StatusBar = False
End Sub

thedark123
06-16-2006, 05:05 AM
Any help will be greatly appreaciated