Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to get current page and total pages of Word document

Posted on 2014-02-06
6
Medium Priority
?
873 Views
Last Modified: 2014-02-14
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.

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

Open in new window

0
Comment
Question by:TeDeSm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39839288
Not quite sure that I understand your situation, but this code show how to see if the current page is the last one.
If Selection.Information(wdActiveEndPageNumber) = ActiveDocument.Range.Information(wdNumberOfPagesInDocument) Then

Open in new window

0
 

Author Comment

by:TeDeSm
ID: 39841366
Hi Graham, I get a late binding issue.

This is how I am setting up my references

Imports Word = Microsoft.Office.Interop.Word
++++++

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
.........
0
 

Author Comment

by:TeDeSm
ID: 39841768
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.

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

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 39842882
Sorry, your code looked like VBA, not .Net.

The template design is somewhat idiosyncratic, however it could be done with fields

You could have a bookmark in an IF field that looks like this:

{ IF { PAGE } = { NUMPAGES } [0] }

The 0 is the default text in the bookmark, named to suit
0
 

Author Comment

by:TeDeSm
ID: 39859200
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.
0
 

Author Closing Comment

by:TeDeSm
ID: 39859353
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question