PDA

View Full Version : Solved: Word Automation help



let98007
05-28-2009, 12:13 PM
I am a Visual FoxPro programmer converting several dozen VFP reports to Microsoft Word templates. The templates will be used to produce documents entirely under automation control (the user won't see the documents until after they've been printed.) The automation code must work with all versions of Word from 2000 through 2007. Automation appears to be different enough from macros that my questions may not be appropriate here. Do you know of a forum that covers Word automation issues?

fumei
05-28-2009, 01:04 PM
Well this forum does. And Tek-Tips does.

"Automation appears to be different enough from macros that my questions may not be appropriate here."

I am not sure what you mean by that. There is no real distinction. Automation uses VBA. Macros use VBA.

You may have some difficulties make things work for all versions. There are significant differences in processes and objects in 2007, compared to 2000 to 2003.

I am also trying understand circumstances whereby the user "won't see the documents until after they've been printed". If they are so fully automated...why is the user involved at all?

fumei
05-28-2009, 01:12 PM
Perhaps if you state specific things you want assistance on we may be able to help/suggest things.

let98007
05-28-2009, 03:23 PM
The syntax for VFP OLE automation is similar to macro code until you reach a left paren, then you are on your own. Anything inside the parentheses must be converted and it's not always clear how to do that. Here are some tasks I've strugled with:

1) How do I apply bold to only the header (first row) of the table? The command that seems to apply bold in VFP is oTable.Range.Font.Bold = .t. but I can't figure out how to restrict the range to one row.
Update: I did figure out how to select the row and apply bold font (in just 4 hours of trying!) but now can't figure out how to "unselect" the text...

2) How do I cause the header to be repeated if the table is forced to a second or subsequent page?

3) How do I set the width of columns in the table, preferrably in inches?

Then there is a general issue. I want all templates to have a header on page 1 but the same Footer on all pages. I seem to remember this was easy in Word 2003 but I haven't found any menu options that allow this in 2007. I'm still struggling with the menus in 2007 even though I've had it for 18 months and use it every day.

Also, is there any way to know in advance which processes will fail or throw errors in 2000/2003 BEFORE I install at the client's site?

I appreciate any help or direction you can offer!

fumei
06-01-2009, 09:57 AM
Let's deal with this first:

"Also, is there any way to know in advance which processes will fail or throw errors in 2000/2003 BEFORE I install at the client's site?"

The simplest answer is...no, there is no way without YOU testing it first.

"The syntax for VFP OLE automation is similar to macro code until you reach a left paren, then you are on your own. "

I do not know what you mean by that. I am not following what you are doing exactly. If you have the correct libraries referenced, and you have IntelliSense functional, and you are doing early binding, then you should get IntelliSense popup syntax help.

1) How do I apply bold to only the header (first row) of the table? The command that seems to apply bold in VFP is oTable.Range.Font.Bold = .t. but I can't figure out how to restrict the range to one row. First off, it is good that you are using a table object (oTable) as this makes things easier. However...
oTable.Range.Font.Bold = .t
seems odd. This is not correct syntax. The correct syntax is:
oTable.Range.Font.Bold = True
no . (period). PLUS, that instruction applies to the WHOLE table.

"I can't figure out how to restrict the range to one row"

By making the instruction apply to just the first row.
oTable.Rows(1).Range.Font.Bold = True


"Update: I did figure out how to select the row and apply bold font (in just 4 hours of trying!) but now can't figure out how to "unselect" the text..."

Better to not use Selection at all. There is, in fact, no reason to use Selection. The code above does not, so the issue of "unselecting" never comes up.

"2) How do I cause the header to be repeated if the table is forced to a second or subsequent page?"

Dim oTable As Table
Set oTable = ActiveDocument.Tables(1)
With oTable.Rows(1)
.Range.Font.Bold = True
.HeadingFormat = True
End With
The above bolds the first row, AND makes it the repeating header. No Selection required.

3. Your width question. Try recording a macro and looking at the code.

let98007
06-01-2009, 04:29 PM
Ah, welcome to the many syntactical differences in Visual FoxPro’s implementation of VBA.

oTable.font.bold = .t. && is proper syntax, while
oTable.font.bold = True && generates an error

To my utter amazement,

oTable.rows[1].range.font.bold = .t.

does indeed apply bold to the row only—in spite of the fact that VFP’s Intellisense (based upon the type library) indicates that the rows object does not have a range property. I too would like to avoid using Selection but it seemed to be the only way. Thanks! And

oTable.Rows(1).HeadingFormat = .t.

is exactly what I spent several hours looking for.

I decided that AutoFit will work fine for this particular table. If I have to revisit the topic later I may have to try the macro technique. Often the macro code isn’t that much help since the syntax can be so different.

On the topic of testing for Word version differences, I may have to wait until I’ve installed the app to find the problems. I don’t own all of the versions of Word my clients run and most are no longer available for purchase--even if I could justify the cost and confusion of running multiple versions here. I was hoping there might be a cross reference of commands that cause problems. It seems like all versions from 2000 until the release of 2007 behave similarly. The first problem I ran into when I installed Office 2007 was that existing automation code broke on the line

oWord.Options.BackgroundPrinting=.f.

Now if I can find a way to modify the templates so that any subsequent pages use the same footer as Page 1 I’ll feel really successful. The option for “Different Page 1” allows for removal of the Header on Page 1 but there’s no option to leave it on Page 1 and remove it from subsequent pages!

Thanks for your help. I really appreciate it!

fumei
06-02-2009, 12:04 PM
oTable.font.bold = .t

is correct syntax within VFP use of VBA????? Good heavens!

That is atrocious and no bloody wonder anyone would get syntax errors creeping in.

Microsoft. You gotta wonder.

"in spite of the fact that VFP’s Intellisense (based upon the type library) indicates that the rows object does not have a range property."

Well.....even within Word itself and VBA there are a number of things that do NOT come up with IntelliSense. In particular:

object.OLEFormat.Object.

after that last dot (.) IntelliSense gives nothing. Nada. Zip.

However, there are number of properties and methods that legal and valid. But unless you know that they are there, you will never find them....unless you have intensely, persistently, wandered through Object Browser.

So...I gotta ask, what is Object Browser like within VFP and its (somewhat odd) implementation of VBA? Does it show Help with that weird syntax ( = .t)????

Mind you, try doing VBA with Corel Draw or Corel Paint. Yes they are "VBA compliant" but....whoa.....

let98007
06-02-2009, 09:50 PM
I spend a lot of time pouring over the object model and type library, but even when I find an interesting property or method, it can be difficult to figure out what it really does. Microsoft documentation is the subject of a good many jokes—and deservedly so: it states the obvious and offers few meaningful examples. The Object Browser is the same in all of the development tools I think, and when you open the Word type library, you see only standard VBA syntax. I do have some VFP sample code, much of it from Word 97, which offers some help with syntax. What it often comes down to though is simply experimentation. That can get really frustrating when you have a tight deadline or are not paid by the hour--which is most of the time!

fumei
06-04-2009, 08:37 AM
LOL

I hear you. Sorry I can not help more.

let98007
06-04-2009, 05:55 PM
You have been a great help and I do appreciate it! I'm pretty good at the VFP syntax stuff by now--I've been using FoxPro for almost 20 years. But the VBA commands aren't all that obvious sometimes. Then there's the Word 2007 stuff--won't go there!

fumei
06-05-2009, 11:04 AM
"Then there's the Word 2007 stuff--won't go there!"

LOL. Indeed. In fact...I will NOT go there. I worked with the beta and disliked it immensely, and I am hoping intensely that I will be retired before we bring 2007 (or later) in. I dread having to deal with it in our organization...although I have already been asked about training for it.

let98007
06-06-2009, 01:33 PM
Unfortunately, I needed a new Office license when I bought another laptop and succumbed to the price--less than 1/2 of what I would have paid to buy an earlier edition. Now I know why! I'm glad to find I'm not the only one with an intense dislike of it though. Every thing I do in 2007 takes me 4 times as long as it would have in 2000/2003, and I've been using 2007 for 18 months so it's not just a learning curve. Then there is the backwards compatibility issue, especially where automation is concerned.

fumei
06-08-2009, 09:41 AM
The learning curve is bad enough, but for me, I simply do not like the interface, and I dislike intensely that Microsoft made Word harder to use for those of us who actually know how to use Word. I believe the consensus among real "power users" of Word is that 2007 made being a serious power user more difficult.

For the average or simple user, it is - perhaps - better, but frankly, I for one would question that.

Good luck.

let98007
06-08-2009, 10:52 AM
I couldn't agree more! I don't think the 2007 interface benefits any category of user. The organization is illogical and seems arbirary to me. There are a couple of features I like, but they aren't worth the pain and frustration! I have recommended that my clients NOT upgrade.

fumei
06-08-2009, 12:06 PM
I wrote a formal whitepaper for our organization that strongly recommended against it, not least for the license issue. It will cost us literally millions of dollars, and for what really? Some (supposedly) "better" eye-candy.

Bah humbug. Heck, the vast majority of our users do not use 10% of the features/functionality of our current Office version (2002/XP). And they actually understand less than that 10%.

let98007
06-08-2009, 10:18 PM
Many years ago I produced a 24 page newsletter using Word 5.0 and can still remember how frustrated I was when I could no longer use the keyboard shortcuts I knew so well. The mouse just slowed me down! I feel very much that way about ribbon menus.

Ironically, I have learned to like Outlook 2007 (after I got over being angry when it deleted 3/4 of the messages moved from 2000 during the first archive process.)

fumei
06-09-2009, 11:56 AM
Hey! A keyboard person! Yup, I go way back with with both WordPerfect and Word...and....WordStar. I still fire the majority of my macros with keyboard shortcuts.

What cracks me up is people even after I have shown them still insist with messageboxes, or userforms to:

Lift their hand off the keyboard
Move it over to the mouse
Move the mouse to position over "OK"
Click the button

This is when the OK button has the focus...and all they have to do it press Enter.

Drives me nuts.

let98007
06-09-2009, 09:33 PM
Not many of us keyboard folks left I'm afraid. A dying breed. Ahhh, the good old days :-)

fumei
06-10-2009, 10:09 AM
It is just so much faster and more efficient, and less prone to carpal tunnel...which is no laughing matter.

Just as an interesting (possibly) aside - and we are WAY off into the weeds as this has nothing to do with the thread - my boss is blind. Therefore I am acutely aware of making sure documentation of software covers using keyboard functions/shortcuts. He has a screen reader, but he never uses a mouse...he can't see the cursor. So mouse movements are nothing to him. He has to use the keyboard.

He thanked me profusely when I told him about Shift-F10. It had been driving him nuts as there are a few places where apps seemingly require a right click. That is the "standard" way to get, for example, context-sensitive menus. He does not even have a mouse attached to his computer. This, apparently, locked him out of doing some things. I told him Shift-F10 is a standard keyboard equivalent to a right click. It should work with all Windows compliant applications, not just Microsoft apps.

He was so happy.

BTW: that is also how you can get to some things apparently blocked when you record macros. There are places where you can NOT use the mouse when recording macros....but Shift-F10 always works.

BTW2: our visually impaired users (and we have a few) will, I think, find 2007 to be bloody awful. While Microsquish makes decent noises about accessibility, IMO their apps are very much dedicated to the GUI.