Word 2007 B2TR Automation Woes

S

spm

I have an app which currently automates the creation of user documents
via any version of Word up to Word 2003, and am attempting to update it
to support Word 2007. The app works fine with Word 2003 (and below).

I worked on upgrading the app to support automation via Word 2007,
using the Beta 2 release of Word 2007, and all worked fine except for
an issue with setting AutoFormatXXX properties: I reported this issue
previously, and then installed the Word 2007 Beta 2 Technical Refresh
(B2TR) and re-imported the Word 2007 Object Library for use with my app.

Now, the AutoFormatXXX properties all work as expected, but now all
sorts of other problems occur:

1. Calls to a document's Styles.Add function fail with an access
violation. Styles.Add2003, however, works fine. I have tried Styles.Add
with various values for the last parameter, using each of True, False,
and empty in turn, and all fail.

2. Calls to a document's InlineShapes.AddPicture work OK and seem to
return a valid object, but subsequent efforts to set the Shape object's
properties often cause an E_FAIL (0x80004005) error. This is not
consistent, as sometimes they work and at others they fail, using the
exact same data.

3. Apparently random "Type mismatch" errors. Sometimes a call succeeds,
but then fails on the next test run with the same data. This happens on
different functions, nit always the same.

Note: None of these issues occurred (with the same codebase) with Word
2007 B2, nor with earlier versions of Word, so they are new to the
B2TR. This seems like a significant backward step. Assistance is kindly
requested.
 
C

Cindy M.

Hi Spm,

Can you post relevant code snippets for each of the scenarios? If I can
duplicate the problems, I'll use the official bug reporting mechanism to
get them in ASAP.
I have an app which currently automates the creation of user documents
via any version of Word up to Word 2003, and am attempting to update it
to support Word 2007. The app works fine with Word 2003 (and below).

I worked on upgrading the app to support automation via Word 2007,
using the Beta 2 release of Word 2007, and all worked fine except for
an issue with setting AutoFormatXXX properties: I reported this issue
previously, and then installed the Word 2007 Beta 2 Technical Refresh
(B2TR) and re-imported the Word 2007 Object Library for use with my app.

Now, the AutoFormatXXX properties all work as expected, but now all
sorts of other problems occur:

1. Calls to a document's Styles.Add function fail with an access
violation. Styles.Add2003, however, works fine. I have tried Styles.Add
with various values for the last parameter, using each of True, False,
and empty in turn, and all fail.

2. Calls to a document's InlineShapes.AddPicture work OK and seem to
return a valid object, but subsequent efforts to set the Shape object's
properties often cause an E_FAIL (0x80004005) error. This is not
consistent, as sometimes they work and at others they fail, using the
exact same data.

3. Apparently random "Type mismatch" errors. Sometimes a call succeeds,
but then fails on the next test run with the same data. This happens on
different functions, nit always the same.

Note: None of these issues occurred (with the same codebase) with Word
2007 B2, nor with earlier versions of Word, so they are new to the
B2TR. This seems like a significant backward step. Assistance is kindly
requested.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
C

Cindy M.

Hi Spm,
1. Calls to a document's Styles.Add function fail with an access
violation. Styles.Add2003, however, works fine. I have tried Styles.Add
with various values for the last parameter, using each of True, False,
and empty in turn, and all fail.
I just tried this within Word, and get no error or crash. What interface
are you using to automate Word?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
S

spm

Hi Cindy,

I can produce code snippets from my app, sure, but they are all in
Delphi. Would they still be of use to you (the Delphi snippets should
be easy enough to read)?

That said, I have installed the B2TR on top of a full B2 installation,
and did so by a simple run of the B2TR distribution file (which
appeared to complete successfully). I note from MS's announcements that
B2TR is not, apparently, a "single-click" update, and that the
installation materials should be read carefully. All I can find on MS's
web site, however, is a mention that the TEMP folder needs clearing out
before the update. I am open to the suggestion that my B2TR
installation might, perhaps, be broken in some way (though I doubt it),
and that might have resulted in a bad type library import. Are you
aware of any B2TR installation gotchas that I should be aware of?
 
C

Cindy M.

Hi Spm,
Are you
aware of any B2TR installation gotchas that I should be aware of?
There's a listing at http://pschmid.net but I'm not sure any are
relevant to this problem - you'd have to look. FWIW I had absolutely no
problems.

It's possible we're looking at an issue with external accessing of the
automation interfaces, but...
I can produce code snippets from my app, sure, but they are all in
Delphi. Would they still be of use to you (the Delphi snippets should
be easy enough to read)?
I certainly don't see any Styles.Add2003 in either Word's object browser
with "hidden memebers" activated OR the list generated in a .NET
application via the PIAs. It seems Delphi is finding something that's
not exposed elsewhere. And I just tested Styles.Add through a COM Add-in
generated by the .NET Framework - no problems there, either.

FWIW
I have tried Styles.Add
with various values for the last parameter, using each of True, False,
and empty in turn, and all fail.
If I'm working through .NET with strict typing, then I have to pass a
wdStyleType as the last parameter. None of what you list here are
acceptable. The default would be wdStyleTypeParagraph

For (2) and (3), code snippets would be fine for testing. But (3) may
not be reproducible for me at all. Do these various functions all
manipulate the same kind of object? Do they have anything in common?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
S

spm

I certainly don't see any Styles.Add2003 in either Word's object
browser with "hidden memebers" activated OR the list generated in a
.NET application via the PIAs. It seems Delphi is finding something
that's not exposed elsewhere. And I just tested Styles.Add through a
COM Add-in generated by the .NET Framework - no problems there,
either.

...

If I'm working through .NET with strict typing, then I have to pass a
wdStyleType as the last parameter. None of what you list here are
acceptable. The default would be wdStyleTypeParagraph

Ah, that was enlightening. Delphi is not in the habit of adding
unexpected functions to a type library, so something had to be
different between our two setups. What I found was that the type
library I was using was that from Word 2007 Beta2, and that had
Styles.Add2003, and a new Styles.Add which had an extra parameter. In
moving to the B2TR, however, and reimporting its' (new) type library
properly (I obviously hadn't done this as I thouhgt I had ;-)), it
appears that MS removed the new Add method, and reverted to the old
(pre Office 2007) one, now simply named Add again. That all now works
fine...
For (2) and (3), code snippets would be fine for testing. But (3) may
not be reproducible for me at all. Do these various functions all
manipulate the same kind of object? Do they have anything in common?
.... and similarly the apparently random 'type mismatch' errors I was
experiencing are all eliminated. The E_FAIL errors, however, persist
when attempting to set the properties of pictures I add to the
InlineShapes collection. That issue, as I reported earlier, is new to
the B2TR, and it is a pretty major issue at that.

I will post code snippets here later, but in the meantime, I can
reproduce the issue simply by adding a picture from an existing image
file using InlineShapes.AddPicture(), and then set (any) properties of
the returned object to cause the error.
 
C

Cindy M.

Hi Spm,
I was using was that from Word 2007 Beta2, and that had
Styles.Add2003, and a new Styles.Add which had an extra parameter. In
moving to the B2TR, however, and reimporting its' (new) type library
properly (I obviously hadn't done this as I thouhgt I had ;-)
I will post code snippets here later, but in the meantime, I can
reproduce the issue simply by adding a picture from an existing image
file using InlineShapes.AddPicture(), and then set (any) properties of
the returned object to cause the error.
Checking... Yep, I see it now. Only when inserting an InlineShape;
doesn't happen if it's in the document. Good catch! Reporting it now.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
S

spm

<whew>Glad that one's straightened out :)

Indeed, I was surprised that MS 'had made' such a backward step ;-)
Checking... Yep, I see it now. Only when inserting an InlineShape;
doesn't happen if it's in the document. Good catch! Reporting it now.

Ah, good to see you can confirm things. Thanks for reporting it. In the
meantime, I'll see if I can achieve the same end result from adding
pics to the document instead - it will be useful for me to know, anyway.
 
C

Cindy M.

Hi Spm,
Ah, good to see you can confirm things. Thanks for reporting it. In the
meantime, I'll see if I can achieve the same end result from adding
pics to the document instead - it will be useful for me to know, anyway.
Apparently it's a timing issue. Starting a couple of versions back, Word
handles graphics stuff asynchronously. And in 2007 it's inserted using XML
(the new file format). Apparently, Word needs to allow more time (or
perform some kind of check) before letting automation code continue.
That's why we aren't seeing problems with InlineShapes already in the
document. If you put a loop in between creating and manipulating the
object and keep incrementing the looping counter, at some point you should
see the error go away.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
S

spm

Cindy said:
Apparently it's a timing issue. Starting a couple of versions back,
Word handles graphics stuff asynchronously. And in 2007 it's inserted
using XML (the new file format). Apparently, Word needs to allow more
time (or perform some kind of check) before letting automation code
continue. That's why we aren't seeing problems with InlineShapes
already in the document. If you put a loop in between creating and
manipulating the object and keep incrementing the looping counter, at
some point you should see the error go away.

That seems to fit with my experiences here - if I step through the same
code in a debugger, all works fine, hence the timing issue. OK, I could
repeat the (first) property setting after creation in a loop, but that
is inherently dangerous (as well as very messy), since if there is a
genuine issue the set-property/trap-exception/wait-and-loop will
continue indefinitely unless I put some upper time limit on it. Yuk.

Does the information you have suggest that this issue will persist into
the public release of Word 2007, or will it be fixed prior to then?
 
C

Cindy M.

Hi Spm,
Does the information you have suggest that this issue will persist into
the public release of Word 2007, or will it be fixed prior to then?
Too soon to say. But it has been taken notice of :), so I very much hope
it gets fixed!

Cindy Meister
 
S

spm

Cindy said:
Apparently it's a timing issue. Starting a couple of versions back,
Word handles graphics stuff asynchronously. And in 2007 it's inserted
using XML (the new file format). Apparently, Word needs to allow more
time (or perform some kind of check) before letting automation code
continue. That's why we aren't seeing problems with InlineShapes
already in the document. If you put a loop in between creating and
manipulating the object and keep incrementing the looping counter, at
some point you should see the error go away.

On further investigation, trying repeatedly in a loop doesn't actually
resolve matters. What I tried doing was to create the InlineShape
object, then immediately set a property value and trap any error. If an
error occurs, I wait s short time (1 second) and then try again, until
either (a) the error goes away or (b) a retry lmit is reached. Sadly,
it seems that even attempting to set a property while Word is still
asynchronously finishing the object creation often causes a nasty
situation in which Word seems to freeze, and the machine crawl to a
halt. It seems the only way is wait an (indeterminable) amout of time
before trying to set a relevant property. Clearly this is not feasible.

It becomes crucial, therefore, that MS fix this issue before release,
otherwise all sorts of apps using automation are going to irrecoverably
break...
 
C

Cindy M.

Hi Spm,

I admit I haven't had time to test further on this, but...

I wonder if the InlineShapes.Count property is updated immediately? Or if
you could test in a loop for that to change?
On further investigation, trying repeatedly in a loop doesn't actually
resolve matters. What I tried doing was to create the InlineShape
object, then immediately set a property value and trap any error. If an
error occurs, I wait s short time (1 second) and then try again, until
either (a) the error goes away or (b) a retry lmit is reached. Sadly,
it seems that even attempting to set a property while Word is still
asynchronously finishing the object creation often causes a nasty
situation in which Word seems to freeze, and the machine crawl to a
halt. It seems the only way is wait an (indeterminable) amout of time
before trying to set a relevant property. Clearly this is not feasible.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
S

spm

Cindy said:
I admit I haven't had time to test further on this, but...

I wonder if the InlineShapes.Count property is updated immediately?
Or if you could test in a loop for that to change?

Yes, it does update immediately. That said, I have found a workaround
to the issue. Given that I don't use any Word 2007-specific features, I
now always initially create the document in Word 97-2003 format. When I
have finished constructing all the content, I do a Document.SaveAs() in
Word 2007 format (using wdFormatDocumentDefault) to the same (open)
file, and it all works fine. I guess the object model works as it used
to when creating a Word 97-2003 format doc, and only converting it to
Word 2007 XML format at the end avoids all the new issues. The
automation certainly runs *significantly* faster in this mode.
 
C

Cindy M.

Hi Steve,
Given that I don't use any Word 2007-specific features, I
now always initially create the document in Word 97-2003 format. When I
have finished constructing all the content, I do a Document.SaveAs() in
Word 2007 format (using wdFormatDocumentDefault) to the same (open)
file, and it all works fine. I guess the object model works as it used
to when creating a Word 97-2003 format doc, and only converting it to
Word 2007 XML format at the end avoids all the new issues. The
automation certainly runs *significantly* faster in this mode.
VERY interesting. Yes, I've noticed the XML interface, in general, is
awfully (painfully) slow.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top