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?
Printable View
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?
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.
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.
Excellent question. To shorten the way of ctrl+p, page range 25-30 etc.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.Quote:
Originally Posted by fumei
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]
Try recording a macro in which you print page 5 only.
Quote:
Originally Posted by Frosty
[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]
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]
Quote:
Originally Posted by fumei
I am not certain what to put insted of lCopiesPrintFrom.
Something that would mark range, but what..?
Ummmm, is not - from the recorded macro - page 5 in the, ummmm, Pages parameter?
PrintRangeOfPages?Quote:
Originally Posted by fumei
I don't know what else.
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.
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.
Interesting concept - methgod. Sounds like something a terminal alcoholic might pray to.Quote:
Originally Posted by fumei
Do you have an example of how the string variable should look like?Quote:
Originally Posted by fumei
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.
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.
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...
What I meant is that the overall pages concept can be tricky when you are dealing with multiple sections. If the sections are defined to restart the number at page 1, then you're going to have to do trial and error to figure out how to print page 5 of section 2, when it's actually the 7th page of the document. Printing a specific page from the print dialog box is (if I recall correctly) always the absolute page of a document, not the relative page of a section.
So without sample documents, we can only give you the tools to be able to try and solve this on your own specific environment with your own specific documents. That is why this thread has so many responses. Because the answer is not always the same for all scenarios.
That's why any time someone asked questions related to pages, they get training on how to find the answer themselves, rather than just answers.
Does thy make sense, Zack?
Oh my yes, if you are dealing with multiple sections; or sections with renumbering, there are many issues.
Yes a specific page in the print dialog is absolute.
Unless stated otherwise it appears that Zack's document does not deal with multiple sections, just the one - Section 2.
http://support.microsoft.com/kb/826218
"How to print a range of pages in a multiple-section document in Word 2010, in Word 2007, and in Word 2003"
Both the online help and the MS KB say that "p2s2-p3s2" should print pages 2 and 3 of section 2, but the recorded macro and the manual version do not seem to work.
The Printer Queue flashes for a second but nothing comes out
[vba]
Sub Macro2()
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentWithMarkup, Copies:=1, Pages:="p2s2-p3s2", PageType:= _
wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
End Sub
[/vba]
Paul
And Section renumbers at 1? And there is a page 2 and 3?
Exactly, just one section..Quote:
Originally Posted by fumei
Absolute print dialog is what I need. It's strange no one ever asked for something similar..
What chance do I have of creating it if Frosty found difficulties with it..?
Quote:
Originally Posted by fumei
I'm not sure how the OP's document is numbered, but when I re-start page numbering at 1 for a section, the P2S2-P4S2 print range works as expectedQuote:
MS KB -- Printing pages in relation to their section depends on how the numbering is formatted. If the numbering in the document is set as continuous, referencing page 1 of a particular section most likely does not result in anything being printed. This occurs because, when the numbering is formatted as continuous, the page numbers in all sections except the first do not start with page 1. In this case, the page would have to be referenced according to its overall position in the documents page collection.
So formatting the string for .Pages based on an user's Input as others have suggested might be made to work if section 2 starts renumbering at 1:
P20S2-P25S2
Otherwise if not, I'd think that "P20-P25" would work even if 20-25 are in a different section (based on the MS KB)
Paul
"What chance do I have of creating it if Frosty found difficulties with it..?"
Ummm, Frosty did not find difficulties with it. And, as stated, it is not that difficult if you are dealing with one section, and no renumbering.
Have you actually tried getting a string variable (never mind the inputbox for now) used in the Pages parameter?
Do you have an example of a similar string variable?Quote:
Originally Posted by fumei
For heavens sake, I have suggested you to look at Help for Inputbox on how to get and use a string variable.
I am done here. You are not even trying. One last time...
Declare a string variable by using Dim.
Give it a value by using variable = value (in quotation marks)
Pages:= variable.
I am done. Good luck.
Zack - Just a question for clarificationQuote:
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?
In your original post, you asked about a 'selection', not 'section'
In Word, both terms have specific meanings which not be the 'everyday' ones
Are you looking to have the user enter a start and end page number and then just print that?
As you can tell, printing can be tricky, and just the Start/End could be made to work for a simply structured document, but for a multi-section document document with a variety of page (re)numbering, it's not that simple.
Paul
That's right. Just to enter a start and an end page number. No sections.Quote:
Originally Posted by Paul_Hossler
I will ask again. Have you actually tried putting in "20-25" into Pages? Does it work?
If it does, try it again using a string variable = "20-25". Does it work?
[VBA]Dim "20-25" As String[/VBA]Quote:
Originally Posted by fumei
Like this?
Zack, this almost seems like trolling.
Forget VBA. There are books you can get on the subject. You want to print a range of pages. I repeat-- forget VBA.
In the File > Print dialog (same thing you did to record a macro to print page 5)... can you simply print pages 5-6?
If you can-- then record a macro and do it again. Compare that recored macro to the macro you recorded to print *only* page 5.
That is the only task you should do, at the moment, if all you want to do is be able to print a range of pages.
If you can post that code, and see the difference between the macro which prints page 5, and the macro which prints pages 5 and 6 of your document... then we're getting somewhere.
But stop throwing things against the wall with your posts in such a random fashion. You won't be a programmer at the end of this thread. But you *should* have a better ability to solve your own Word problems.
I am trying to use Dim as suggested. I don't know if I did it right..or how should I do it..Quote:
Originally Posted by Frosty
Example:
[vba]Sub Simple()
'This is a string variable declared using the "Dim" statement.
Dim strDemo As String
'This defines the string value.
strDemo = "If you are going to post claiming what you tried, then post what you tried." & vbCr + vbCr
'This employs the variable string in a Msgbox function
If MsgBox(strDemo & "Can you do that please?", vbQuestion + vbYesNo, "Help Us Help You!") = vbYes Then
MsgBox "Thank you."
Else
MsgBox "Like Fumei, I'm done and should never have started." & vbCr + vbCr _
& "You should find another forum to troll.", vbInformation + vbOKOnly, "STOP TROLLING"
End If
End Sub[/vba]
1. In the VBA editor, under Help, searching for 'Dim' brings up an explaination and examples:
Quote:
Remarks
Variables declared with Dim at the module level are available to all procedures within the module. At the procedure level, variables are available only within the procedure.
Use the Dim statement at module or procedure level to declare the data type of a variable. For example, the following statement declares a variable as an Integer.
Dim NumberOfEmployees As Integer
2. Likewise in Help, there are examples of how to use the 'Inputbox function'
3. If you don't have a [Developer] tab where you can record macros, then go to Word Options, and check 'Show Developer tab on the Ribbon" (assuming you're using 2007/2010)
4. As others have suggested, record a small macro to just print some pages to see the code generated by Word.
5. Edit the recorded code to incorporate information from the Dim and the Inputbox help examples.
Paul
Greg has provided an excellent example.
You will need to do the work from here on, rather than simply trying whatever it is you think you should try, responding to whatever you want to respond to, rather than actually trying to solve your problem.
The problem is not how we communicate... the problem is your lack of desire to try and solve your problem the way we are trying to solve your problem.
You have everything you need to solve your original problem. It is up to you now.
I won't reply unless you do the following:
1. Attempt to print pages 5-6 manually, and then post the recorded macro of that attempt.
2. Attempt to utilize greg's macro combined with the usage of an InputBox in the earlier posts, and get that macro to show a message box of the same string you get from your post on #1.
If you authentically try to solve your own problems-- you will get an enormous amount of help from this forum.
If you are unwilling to do that, or are simply trolling to see how many regular posters you can get to try and tell you exactly the same thing... then you will get zero help from this forum. There are only 5-6 regular posters. If you alienate all of them, you might as well start with a new user name and try again.
"I am trying to use Dim as suggested. I don't know if I did it right..or how should I do it.."
This is a flat out untruth. no one suggested it like that. What I very much DID suggest was to look at Inputbox in Help to see how variables are declared and used. Or, as was also suggested look up Dim in Help. There is NO example that uses Dim like that. You are being useless in making any effort. It would take a minimum of effort to determine how to use Dim.
Either trolling or a blind inability to do anything at all.
Or...hmmmmm, could the reasons that can not be discussed be a school assignment??? In any case Zack, congratulations, you have successfully annoyed all the top posters in the forum.