Print certain pages only - vba

I have the following code which works fine:
Application.PrintOut FileName:="", Copies:=2
This prints my 10 page document twice.
I now want to use the pages option to specify only certain pages to print out:
Application.PrintOut FileName:="", Copies:=2, Pages:="2, 6-10"
I was expecting it to print out pages 2 and 6 to 10 twice, i.e. 2,6,7,8,9,10,2,6,7,8,9,10, but instead it just printed all 10 pages twice.
I am using VBA in Word 2010.
Any idea what I'm doing wrong?
RESOURCES:
From Microsoft Developer Network:
Pages - Optional - Variant - The page numbers and page ranges to be
printed, separated by commas. For example, "2, 6-10" prints page 2 and
pages 6 through 10

Range:=wdPrintRangeOfPages needs to be added along with Pages.
For example:
Application.PrintOut FileName:="", Copies:=2, Range:=wdPrintRangeOfPages, Pages:="2,6-10"

Alternative solution from the website
expression .PrintOut(Background, Append, Range, OutputFileName, From,
To, Item, Copies, Pages, PageType, PrintToFile, Collate, FileName,
ActivePrinterMacGX, ManualDuplexPrint, PrintZoomColumn, PrintZoomRow,
PrintZoomPaperWidth, PrintZoomPaperHeight
You can use From:="2", To:="5".

Related

I'm trying to create a header that begins on the second page, inserts an em dash, the page number and another em dash, centered in vba

document.Application.ActiveDocument.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary) _
.PageNumbers.StartingNumber = 2
For Each section As Word.Section In document.Application.ActiveDocument.Sections
Dim headerRange As Word.Range = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
headerRange.Text = " — "
headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage)
headerRange.Text = headerRange.Text & " — "
Next
The issue that I am having is I cannot get the dashes on either side of the page number. It will always place both of them before or after the page number.
I have tried concatenating, I have tried various placements of the dashes. I have tried the headerRange.Collapse with no success.
'document.Application.ActiveDocument.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary) _
' .PageNumbers.StartingNumber = 2
'For Each section As Word.Section In document.Application.ActiveDocument.Sections
' Dim headerRange As Word.Range = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
' headerRange.Text = "—"
' headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
' headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
' headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage)
' headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
' headerRange.Text = "-"
'Next
To a man with a hammer, everything looks like a nail.
You are doing this the hard way, in my opinion.
I look at using a macro for this as reinventing the wheel - Instead, use Templates or Building Blocks
Perhaps I am lazy, but setting things up in vba and formatting it is hard, at least to me.
You could create a template with your header and use that as the basis for new documents. Here is my page on Templates in Microsoft Word.
You could save this as a Building Block if you want to use it in existing documents. Remember that using a page number building Block for top of page replaces any existing header, for bottom of page replaces any existing footer.
Here is a temporary link to a template containing three page number building blocks with those for top and bottom of the page being centered. All have M-dashes on either side. The font and size will be determined by the Header or Footer styles in the document. The template contains instructions for using it.
If you want to use vba to insert these building blocks, there are examples shown on my web page with specific instructions on placement of the macros and building blocks. Here is my answer here about using vba to insert a Table of Contents a Building Block.
You could also have a macro to create a new document based on your template.
No matter how you decide to do this, keep in mind that each Word section has three headers, whether or not they are seen. In multi-section documents, these may, or may not, be linked to like headers in previous sections.
Here is my recap of Header and Footer Settings.

Microsoft Word Page Numbering Part-Section-Page

I have a long document with about 9 Parts and each part contains 2 - 5 sections. I want to have my page numbers read Part-Section-Page. For example, page 5 of section 4 in part 3 would have the page number 3-4-5.
Using this field code I can output this correct page number in my footer
{STYLEREF 1 \s}-{STYLEREF 2 \s}-{PAGE}
This works because the first field code outputs the current Part, signified by Header 1, the 2nd code outputs the section number signified by Header 2, and then last output is the page which I can control with section breaks.
BUT - This is no help with the Table of Contents! When the ToC is generated, Word only recognizes {PAGE} as the page number, and therefore I need to manually write the ToC.
Is there any way to make the page number actually be in the format x-y-x?
Any ideas or help would be appreciated!

VBA: Scrape dynamically created webpage?

Trying to use VBA to scrape some product pages with 30 products per page. If you inspect the page in dev tools, the elements I need are all in span tags with straightforward class names; "part-number", "price", etc. There will be one for each product on the page, and then one empty one. But if you Ctrl + U and look at the source code, only the empty one is there. So if you look in dev tools there are 31 span tags with class "part-number", 30 populated ones and one empty. But if you look at the source code, only the empty one is there.
If I attempt to do something like...
For x = 0 to 29
Debug.Print "Part number = " & ie.document.getElementsByClassName("part-number")(x).innerText
Next x
...it returns one blank value and then errors off with "Object variable or With block variable not set."
Pretty sure what's happening here is that the source code being served up by the server only includes the one blank span tag for each class, and then the rest of the HTML is being dynamically created by Javascript or whatever, but VBA is only attempting to scrape the original source code.
Any way I can get it to scrape the DOM after being rendered instead?

Word VBA detect blank pages after odd/even section break

I'm new here so be gentle... lol
I've made a document and have separated it into chapters (sections) and am numbering the pages of each section as follows
1-1 of 9
2-1 of 6
etc
I'm using odd page breaks to put the first page of my sections in my place of preference. Of course using odd/even page breaks will under the right circumstances result in a non-printing blank page at the end of the section which of course is exactly what is supposed to happen. The problem is that in those cases my page numbers end up as for example, 2-1 of 13 but 13 is a non printing blank page and so the last printing page is 2-12 of 13. I've tried .goto to that page but apparently it won't allow you to .goto that page. I pared down my code to just outputting the number of pages to a msgbox to prevent any ancillary nonsense from getting in the way of the point. Here it is:
Dim i, x
Dim FooterPages As Integer
Dim Sect As Integer
With ActiveDocument
For i = 1 To 10
'Footer page numbers
ActiveDocument.ActiveWindow.Selection.GoTo What:=wdGoToSection, Count:=i + 2
Sect = .ActiveWindow.Selection.Information(wdActiveEndSectionNumber)
FooterPages = .Sections(Sect).Range.Information(3) - .Sections(Sect - 1).Range.Information(3)
MsgBox FooterPages
Next i
End With
I would really appreciate some help with a bit of code to detect that last page so I can subtract it from the total page count of that section.
Thanks in advance

MS Access 2007 - Print MultiTabbed Report - VBA

I am creating a form in MS Access that exceeds 22 inches in length. This form is extremely long so I am using tabs to spread out the questions.
I've now copied the form (with the tabs) on to a report in order to filter the data and to print it.
The name of the Tab control on the report is 'TabCtl219' and the report name is 'rpt_ADMIN'. I'd like to write some vba to print page 1, page 2, and page 3. Considering I can manually flip through the pages on the report, i'd like to print each page using vba. Currently, it will only print page 1.
Your thoughts are greatly appreciated.
One possible solution would be to get rid of the tab control and spread out the ui-elements and information over several pages.