TeDeSm
asked on
How to get current page and total pages of Word document
I have a project that prints invoices using a Word .dotx template. Some invoices are multi-page and on each page I have page n of N.
However I need to print the invoice totals just on the last page where I have placed Bookmarks. I have no problem printing the totals at the Bookmarked locations.
Is there a way to compare the current page n with the total pages N and then if the last page is reached print the totals?
The code shows how I am currently printing to Bookmarks.
However I need to print the invoice totals just on the last page where I have placed Bookmarks. I have no problem printing the totals at the Bookmarked locations.
Is there a way to compare the current page n with the total pages N and then if the last page is reached print the totals?
The code shows how I am currently printing to Bookmarks.
With oWord.ActiveDocument.Bookmarks
With .Item("CustomerAccountNumber").Range
.Bold = -1
.Font.Size = 6
End With
.Item("DocumentDate").Range.Text = Me.mtxtDateOfInvoice.Text
.Item("DocumentNumber").Range.Text = strInvoiceNumber
.Item("GoodsTotal").Range.Text = MyInvoiceMainDetailList(i).GoodsTotal.ToString("N2")
.Item("VATTotal").Range.Text = MyInvoiceMainDetailList(i).VATTotal.ToString("N2")
.Item("InvoiceTotal").Range.Text = MyInvoiceMainDetailList(i).InvoiceTotal.ToString("N2")
End With
ASKER
Hi Graham, I get a late binding issue.
This is how I am setting up my references
Imports Word = Microsoft.Office.Interop.W ord
++++++
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
.........
This is how I am setting up my references
Imports Word = Microsoft.Office.Interop.W
++++++
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
.........
ASKER
Fixed the problem with the references. However because the template has the Bookmarks in the page footer the invoice totals get placed on all the pages, sort of defeats the below code.
Tried changing the font colour to white for previous pages, black for last, but then that isn't going to work either.
Is there another way to get the totals placed onto the last page without using Bookmarks.
Tried changing the font colour to white for previous pages, black for last, but then that isn't going to work either.
Is there another way to get the totals placed onto the last page without using Bookmarks.
Dim intLastPage As Integer = 0
Dim intCurrentPage As Integer = 0
With oWord.Selection
intLastPage = CInt(.Information(CType(3, Word.WdInformation)))
intCurrentPage = CInt(.Information(CType(4, Word.WdInformation)))
If intLastPage = intCurrentPage Then
With oWord.ActiveDocument.Bookmarks
.Item("GoodsTotal").Range.Text = MyInvoiceMainDetailList(i).GoodsTotal.ToString("N2")
.Item("VATTotal").Range.Text = MyInvoiceMainDetailList(i).VATTotal.ToString("N2")
.Item("InvoiceTotal").Range.Text = MyInvoiceMainDetailList(i).InvoiceTotal.ToString("N2")
End With
End If
End With
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Graham,
Tried your suggestion but still get the totals on all pages. I think this may be because the field with the included bookmark exists on all pages, inherited from the template, and therefore the IF condition is evaluated for all pages for PAGE = NUMPAGES. The condition is met when the last page is formatted and therefore the bookmark is updated for all pages.
Tried your suggestion but still get the totals on all pages. I think this may be because the field with the included bookmark exists on all pages, inherited from the template, and therefore the IF condition is evaluated for all pages for PAGE = NUMPAGES. The condition is met when the last page is formatted and therefore the bookmark is updated for all pages.
ASKER
Had another go at inserting the fields only this time I used Ctrl-F9 to produce the field braces and not try to use the Quick Parts dialogue. I did not require the default text for the bookmarks so left that blank and only inserted the bookmark into the field in the True part of the IF statement.
Your solution works thank you.
So to recap for others: Alt-F9 to toggle showing fields, Ctrl-F9 to produce field braces. In my case for currency values I used decimal point tabs to line up amounts.
Your solution works thank you.
So to recap for others: Alt-F9 to toggle showing fields, Ctrl-F9 to produce field braces. In my case for currency values I used decimal point tabs to line up amounts.
Open in new window