AutoTextEntries(“Page X of Y”) in Word 2007
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
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.