Consulting

Results 1 to 8 of 8

Thread: Print Multiple Sheets as Word Docs

  1. #1
    VBAX Regular
    Joined
    Feb 2009
    Posts
    38
    Location

    Print Multiple Sheets as Word Docs

    Hi all,

    Im looking for code to deal with multiplesheets in the same workbook that I want to print as a single word document.. any thoughts on the VBA code for this?

    I have code that handles it one sheet at a time for PDF print, but need these two alterations so it prints all sheets, and in word.

    Any thoughts appreciated....Thanks

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Here's one to try. Each sheet on a page:
    [VBA]Option Explicit
    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
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Feb 2009
    Posts
    38
    Location
    Hi Lucas,
    sorry for the slow reply, caught up here in testing yesterday.

    This seems to work a treat so far, just putting it within the app Im building and will confirm.

    Thanks heaps for this so far...

  4. #4
    VBAX Regular
    Joined
    Feb 2009
    Posts
    38
    Location
    Hi Lucas,

    just catching up - this has worked well; and prints all sheets in the workbook. What is the best way to print just specific sheets in the workbook?

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Are you going to select different ones each time and need to choose from a list or will they be the same each time and can be hard coded?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    VBAX Regular
    Joined
    Feb 2009
    Posts
    38
    Location
    Hi, thanks for following up...

    It could be different each time, so a selection option would be great.

    One other - I note the pagintion code above, will this need change? I may specify a different range per sheet, not juse UsedRange?

  7. #7
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings,

    Just as to selection different sheets, maybe?
    [vba]
    'For Each ws In ActiveWorkbook.Worksheets
    For Each ws In ActiveWindow.SelectedSheets
    [/vba]

    Hope this helps,

    Mark

  8. #8
    VBAX Regular
    Joined
    Feb 2009
    Posts
    38
    Location
    Hey GTO,

    thanks will try soon. You and Lucas (Steve) have been really helpful here.
    Will let you know where this gets me.. thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •