AutoTextEntries(“Page X of Y”) in Word 2007

March 16, 2008 at 11:45 pm 5 comments

One of the applications I am maintaining works heavily with Microsoft Word Automation.  Over the summer I had to prepare that app to work on Office 2003 machines.  This month, I’ve “prepared” the app to work with Office 2007.  I say “prepared”, because even with all the backend and file structure changes with 2007, I had to make significantly LESS code changes for Office 2003->Office 2007 than I had to do with Office 2000->Office 2003.  In fact, the only code change I had to do for 2007 was set the Word application’s visibility to “True” in a few places.  That’s it.  Everything else worked right away.  I applaud Microsoft for keeping their object library so consistent between versions.  My boss, however, may see the less billable hours as a detriment. 🙂

Now once I tested all the related features on my Office 2007 development machine, I compiled and deployed the application to an Office 2007 machine.  Suddenly a couple of the features were producing a “Requested Member of the Collection Does Not Exist” error.  I was able to track it down to this offending line:

pubWordApp.NormalTemplate.AutoTextEntries("Page X of Y").Insert Where:=pubWordApp.Selection.Range

So I tap into Old Reliable (Google) and found someone with the same issue and a helpful response on the subject.  It got me on the right track, but I found the bolded portions to be incorrect:

You have a lot of work to do. :-( 

In Word 2007, AutoText entries are a specific group of the more general Building Blocks collection. You can’t access any template’s .AutoTextEntries collection the way you could in 2003. Start by reading the topic in the Word 2007 VBA Help titled “Working with Building Blocks”.

Actually, it wasn’t a lot of work and I could access the AutoText collection the same way (My main tip off– no issues on the development machine).  I just needed to add my “Page X of Y” to the “AutoText” section of Word 2007’s new Building Blocks on the test machine. Once you factor out my normal struggle of finding things in the ribbon, it was rather easy:

1) I created a header in a blank Word document and added my “Page X of Y” text.

2) I highlighted my new text and went to Insert->Quick Parts->Save Selection to Quick Part Gallery

3) When the Quick Part Properties prompt displayed, I made sure to designate the properties appropriately:

Name: Page X of Y
Gallery: AutoText
Category: General
Save in: Normal.dotm

I saved my changes, closed out Word and then that old 2003 code (pubWordApp.NormalTemplate.AutoTextEntries(“Page X of Y”).yada.yaya.yada) worked just fine. 

Once again– I, happy.  Boss, not so much.

Entry filed under: Software Development, Word 2007. Tags: .

Vicky the Wedding Photographer links for 2008-03-17

5 Comments Add your own

  • 1. Chriggy  |  March 17, 2008 at 12:01 am

    Of course, you could always tell your boss it took as long as he expected it would take.

    Reply
  • 2. John  |  August 12, 2008 at 10:07 am

    You are awesome. That fixed all my problems. Thank you so much.

    Reply
  • 3. Mike  |  January 24, 2009 at 1:23 pm

    Spent hours on this. You saved me from more headache. Thank you!

    Reply
  • 4. Dipo Olufemi  |  August 7, 2009 at 12:03 pm

    Thank you very much and may God bless you.

    Reply
  • 5. mitsuy  |  November 3, 2009 at 5:25 pm

    Thank you! spent a hours on this..

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Flickr Photos

Cork Kitty - White - Side

Cork Kitty - White - Front

Cork Kitty - Tiger Striped Orange.

Cork Kitty - Tiger Striped Orange - Side

Cork Kitty - Tiger Striped Grey - Front

More Photos

3D Printed Products

Tweets


%d bloggers like this: