Results 1 to 7 of 7
  1. #1
    TurnipGrnPeace is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Jun 2013
    Posts
    4

    Code to print 'section' of a word document.

    From an Access Database I need to print section 2 (as an example) of a word document rather than the whole thing but haven't found the code to do so.
    At work I'm using 2010. When I try this it prints the whole document.

    WordObj.PrintOut Range:=0, Item:= _
    0, Copies:=1, Pages:="p1s2-p1s2", PageType:= _


    0, Collate:=True, Background:=False, PrintToFile:=False, _
    PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
    PrintZoomPaperHeight:=0

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    This thread discusses printing specific pages of each section http://www.vbaexpress.com/forum/showthread.php?t=23603

    I expect could be adapted to print all pages of a specific section. How many sections does your document have? Which one do you want to print? Want to provide Word document for testing?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    TurnipGrnPeace is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Jun 2013
    Posts
    4
    Thanks June7,
    I have Mechanical Integrity Procedures which contain blocks of instruction to be done daily, monthly, annually and so on. I want to be able to allow the mechanic to print out the portion he needs that day. The .docs already have bookmarks but I have freedom to replace them with 'sections'. I note that a bookmark can define a range of lines. See 'safe' in attachment.

    I wonder if the choke point is Range:=wdPrintRangeOfPages. When I replace wdPrintRangeOfPages with 1 it prints page 1 instead of the first page of section2. If I use 0 instead of wdPrintRangeOfPages it shoots out a blank sheet of paper. -1 does nothing.Test - Copy.doc
    Private Sub Command12_Click()
    Dim WordObj As Object
    Set WordObj = CreateObject("Word.Application")
    WordObj.Documents.Open "Y:\PSM\00\Test.doc" ':Pages:"p1s2-p5s2""

    WordObj.ActiveWindow.PrintOut Range:=1, Pages:="p1s2-p1s2" ', From:="s1", To:="s2" '& oneSection.Index

    'Next oneSection
    WordObj.Quit
    Set WordObj = Nothing
    End Sub

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Is this only an example of many such documents?

    I see 3 sections - which do you want to print? What do you mean by 'safe' - SAFETY AND HEALTH CONSIDERATIONS heading?

    The section breaks hit in weird places, like the middle of tables. That doesn't make sense to me.

    Why does Initial Startup under STANDARD OPERATING PROCEDURES split over 3 pages?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    TurnipGrnPeace is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Jun 2013
    Posts
    4
    There are >20 such documents. As you've probably guessed this is a work order program.

    'safe' is actually bookmark and it is also the example range I would like to print. Spacings. I deleted most of the instructions thinking they weren't important. I really hate word. I got my boss to choke on his coffee one day when I submitted a word document to him with an attached note that the source code for word was written by satan. Apparently he agrees.

    I noticed I forgot to thank you for your assistance. My apologies.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Apparently this is dependent on how the document page numbering is set up. Review http://support.microsoft.com/kb/826218

    In line with that information, I can get page 1 (which is in section1) of the document to print but nothing from the other sections because there is no page 1 in those sections.
    For lngSectionCounter = 1 To ActiveDocument.Sections.Count
    WordObj.ActiveWindow.PrintOut Range:=wdPrintFromTo, From:="p1s" & lngSectionCounter, To:="p1s" & lngSectionCounter
    Next

    Here's example using bookmarks, maybe easier. http://www.microsoft-word-answers.co...-document.aspx

    I also had issue with rerunning the procedure multiple times. The second run errors and I have to cancel then delete process from Task Manager as well as delete the Word locking file. Then procedure runs fine. But every second run errors. I have encountered this with Access/Excel interoperability code but not sure how to fix it for Word.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    TurnipGrnPeace is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Jun 2013
    Posts
    4
    Thanks June7. I read those articles and was able to piece together this code.

    Private Sub Command12_Click()
    Dim strFile As String
    Dim Wd As Object
    Set Wd = CreateObject("Word.Application")
    strFile = "Y:\PSM\00\Test.doc"
    Set Wd = GetObject(strFile)
    Wd.Windows(1).Visible = False
    With Wd.ActiveWindow
    .Selection.GoTo Name:="safe"
    .Selection.Find.ClearFormatting
    .Application.PrintOut FileName:="", Range:=1
    End With
    Set Wd = Nothing
    End Sub

    In this example 'safe is a bookmark in the word doc. A little different than a normal bookmark I painted the entire area I wanted to print and defined that block as the bookmark. The code selects the bookmark and prints it.

    Thanks again.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 02-22-2013, 06:41 AM
  2. Create new Word document.
    By Bill H in forum Programming
    Replies: 3
    Last Post: 06-12-2012, 06:40 AM
  3. Export report to word document
    By jackyoung2012 in forum Reports
    Replies: 1
    Last Post: 03-16-2012, 11:16 AM
  4. transferring data from a Word document
    By bdaniel in forum Programming
    Replies: 5
    Last Post: 01-03-2012, 11:16 PM
  5. Import Word document using VBA
    By degras in forum Import/Export Data
    Replies: 4
    Last Post: 04-12-2011, 02:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums