Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through COM collections with FOR EACH

Status
Not open for further replies.

Mike Lewis

Programmer
Jan 10, 2003
17,490
10
38
Scotland
www.ml-consult.co.uk
Several of my applications have "suddenly" developed a bug which I can't explain.

It always occurs when I try to loop through a collection in a COM server using FOR EACH ... ENDFOR.

To give a simple example:

Code:
* oDoc is an object ref to a Word document
FOR EACH ox IN oDoc.Sentences
  ? ox.text
ENDFOR

I've got this sort of code in many places in my apps, and it has always worked. But now, I am getting an error that says that the property (Sentences in this example) cannot be found.

The funny thing is that, if I replace the code with the following:

Code:
FOR lnI = 1 TO oDoc.Sentences.Count
  ox = oDoc.Sentences(lnI)
  ? ox.Text
ENDFOR

that works perfectly.

I'm not aware of any change I've made to my system that could have caused this to happen.

Some further details:

- It happens consistently with all Office apps (Word, Outlook, Excel, etc).

- It happens on two different computers; one has Office 2000, the other has Office 2007.

- The error message is a native VFP message, not an OLE message.

- On both machines, I am running VFP 9.0 SP1.

- I don't see the problem when using native VFP collections.

What am I missing? Am I going ga-ga? I hope someone can help.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Hi Mike,
Code:
FOR EACH ox IN oDoc.Sentences
  ? ox.text
ENDFOR

I just tried it and it works for me. I also checked some automation code and I have a couple of FOR EACH loops in production that have been working for years. (But then we have been on Word 2002 for years. I seem to remember having the same difficulty occassionally myself, however. I think it has something to do with being able to resolve the COM object references.

pamela
 
Hi Pamela,

Thanks for that. Although it doesn't solve the problem, it does help to confirm my sanity.

The funny thing is that I have this type of code throughout my applications, many of which have been running live for years, and I've never heard of any of them failing at users' sites. It's just in the last few days that I've noticed it, and only on my two development systems.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Are we all suffering some kind of mass madness?

Regards

Griff
Keep [Smile]ing
 
Interesting. I've seen the error when I'm using FOR EACH on a native collection and fail to include the FOXOBJECT keyword, but I haven't run into it with a COM collection.

Tamar
 
Well, I've got that madness with the app on a clients pc not sending emails to the mail server, I also STILL have an issue with *some* machines not really enjoying doing OLE automation of M$ Word... you have an issue with code that has worked (and continues to work in most cases) perfectly well for a while...

Are we all going bonkers..

B-)

A conspiracy theorist might connect it with M$ having a 'new' OS out, that no one seems to be buying yet.

Regards

Griff
Keep [Smile]ing
 
Some new information:

- The problem I've described is not specific to VFP 9. I am seeing it with earlier versions as well.

- But it might be specific to Office. I have just tried similar code with the Crystal Reports COM server, and it works as expected.

(I'd like to try it with other COM servers, but I can't off-hand think of any others that I've got and which have collections.)

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top