Consulting

Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 54

Thread: VBA range

  1. #1

    VBA range

    How to achieve printing of the particular selection of pages (25-30 for example) with the macro combined with the msgbox prompting for the desired page range?

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Why do you want a messagebox prompting for the page range? Will they know it? I thought you wanted to print a Section?
    [vba]ActiveDocument.PrintOut Pages:="s2"[/vba]prints Section2. Regardless of the number of pages, or their page numbers.

  3. #3
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Hoo boy... have you done searches on this particular topic? The concept of "pages" in Word is a very nebulous one.

    Did you know you can manually print a page range using the File > Print dialog? P25-P30 typed manually into that dialog will do what you're trying to achieve.

    From there, you could try recording that macro to see what the code is.

    Why don't you try that (and perhaps do some searches on how pages vs. sections work in this forum and others) and that should give you some background.

    It's a big topic, and I don't really want to rehash that discussion when it already exists on this forum multiple times.

  4. #4
    Quote Originally Posted by fumei
    Why do you want a messagebox prompting for the page range?
    Excellent question. To shorten the way of ctrl+p, page range 25-30 etc.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Except if you want to just print the section there is no need.

  6. #6
    Quote Originally Posted by fumei
    Except if you want to just print the section there is no need.
    Perhaps, but I would still like to see if it is possible.

  7. #7
    I tried to create it myself but doesn't seem to work

    [VBA]lCopiesPrintFrom = InputBox( _
    Prompt:="From which page?", _
    Title:="Page Range", _
    Default:="20-25")[/VBA]

  8. #8
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Try recording a macro in which you print page 5 only.
    _______________________________________________
    Please don't cross-post without providing links to your cross-posts. We answer questions for free. Please don't waste the time of the people helping you.
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    - Frosty

  9. #9
    Quote Originally Posted by Frosty
    Try recording a macro in which you print page 5 only.

    [VBA]ActivePrinter = "Microsoft XPS Document Writer"
    Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
    wdPrintDocumentContent, Copies:=1, Pages:="5", PageType:=wdPrintAllPages, _
    ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
    False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
    PrintZoomPaperHeight:=0
    End Sub[/VBA]

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    It is absolutely possible.

    Take a good look at the difference between your code for page 5 and
    [vba]lCopiesPrintFrom = InputBox( _
    Prompt:="From which page?", _
    Title:="Page Range", _
    Default:="20-25") [/vba]

  11. #11
    Quote Originally Posted by fumei
    It is absolutely possible.

    Take a good look at the difference between your code for page 5 and
    [vba]lCopiesPrintFrom = InputBox( _
    Prompt:="From which page?", _
    Title:="Page Range", _
    Default:="20-25") [/vba]

    I am not certain what to put insted of lCopiesPrintFrom.

    Something that would mark range, but what..?

  12. #12
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Ummmm, is not - from the recorded macro - page 5 in the, ummmm, Pages parameter?

  13. #13
    Quote Originally Posted by fumei
    Ummmm, is not - from the recorded macro - page 5 in the, ummmm, Pages parameter?
    PrintRangeOfPages?

    I don't know what else.

  14. #14
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Where the number "5" is, is where you'd put you string. But you should read the print dialog to see the allowed strings you can give it

    This is the way to learn. See what you can do manually. Then record it. Then manually modify.
    _______________________________________________
    Please don't cross-post without providing links to your cross-posts. We answer questions for free. Please don't waste the time of the people helping you.
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    - Frosty

  15. #15
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Make a string variable.
    Make it equal the result of your inputbox.
    Use it with the Pages parameter.

    It is a good idea to read up on the printout methgod.

  16. #16
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by fumei
    It is a good idea to read up on the printout methgod.
    Interesting concept - methgod. Sounds like something a terminal alcoholic might pray to.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  17. #17
    Quote Originally Posted by fumei
    Make a string variable.
    Make it equal the result of your inputbox.
    Use it with the Pages parameter.

    It is a good idea to read up on the printout methgod.
    Do you have an example of how the string variable should look like?

  18. #18
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Zack, you hve to do the work on this. The concept of pages in word is really tricky. There is no perfect generic solution. So you have to figure out what is going to work for your documents, and how to adjust the code for your documents.

    We're not trying to be obtuse, but if you did a search on just this forum, you would see numerous examples.

    The best way for you to figure this out, is to go from that recorded macro that prints out just page 5, to a recorded macro that prints out pages 5 & 6. Try to do it manually, until you get it to work, and then record the macro.

    Compare that from the one that just printed out page 5... And you'll be pretty close to *your* solution for this tricky problem. But it's only your solution. Printing specific pages is a hard topic.
    _______________________________________________
    Please don't cross-post without providing links to your cross-posts. We answer questions for free. Please don't waste the time of the people helping you.
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    - Frosty

  19. #19
    I am not a VBA expert, hopefully there is a solution to this issue.

    If it can't be wrote in VBA or if it would take too much time and effort to create it let it go.

    I was curious, but I understand you people have lives. You can't spend 10 hours writing VBA codes, especially since Frosty says it's hard.

  20. #20
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I am not a VBA expert, hopefully there is a solution to this issue.
    You do not need to be an expert as this is quite basic. There is no hope involved. There IS a solution.


    If it can't be wrote in VBA or if it would take too much time and effort to create it let it go.
    It most certainly can be written in VBA, and no it does not take a lot of effort. But it does take SOME effort on your part.

    I was curious, but I understand you people have lives. You can't spend 10 hours writing VBA codes, especially since Frosty says it's hard.

    Curiosity is good...follow that. While dealing with pages themselves is a bit convoluted (which is what I think Frosty is mentioning), what you are trying to do is not hard at all.

    You want to - if I understand correctly - have a variable (taken from an inputbox) used in printing.

    Frosty - "Printing specific pages is a hard topic." Ummmmm, not in this case. If the specific pages are numeric i.e. pages 5-7. Identifying specific pages can be difficult yes. But if it is simply I want to print pages 5-7, or 20-25, I would have to disagree - that is not hard.

    Zack, look up Inputbox in Help. This shows you how to declare a variable (MyValue), and how to make that equal the result of the Inputbox. Now use it
    [vba]ActiveDocument.PrintOut Pages:=MyValue[/vba]

    macropod - not sure an alcoholic moving to meth is a good idea...

Posting Permissions

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