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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Ghost Object Riddle

Status
Not open for further replies.

fumei

Technical User
Oct 23, 2002
9,349
CA
Hi all,

I am thinking of writing a FAQ on a fairly common Word issue. However, I want to tease the water here.

This common Word object can have the following code run - without any error.
Code:
If [i]object[/i].Exists = [b]False[/b] Then
   Msgbox [i]object[/i] & " does not exist (.Exists = False). " & _
       "However, the text is: " _
       [i]object[/i].Range.Text
End If

The fact that this object can have .Exists = False, yet still hold text content, causes fairly consistent confusion. It is like a ghost object...sort of. There are parent/child issues, as SOME of the child objects can have this "ghosting", but ONE of the child objects NEVER does.

What is it? Is it worth writing a FAQ on? I know I am being a bit of a twit putting this up, but hey... I am curious if anyone gets it.

Gerry
 
Oh crap..... I wrote the code directly, rather than cutting and pasting from the actual procedure. I am sure someone will notice, and comment, that in fact my code will have an error.

I forgot the ampersand in the messagebox string. It should be:
Code:
If [i]object[/i].Exists = False Then
   Msgbox [i]object[/i] & " does not exist (.Exists = False). " & _
       "However, the text is: " [COLOR=red]&[/color red] _
       [i]object[/i].Range.Text
End If



Gerry
 
Just to be clear...

1. You create the condition (and I am choosing my words carefully) that .Exists = True

2. You give .Range.Text a value, say, object.Range.Text = "This is some text."

3. You test - Msgbox object.Range.Text - and a messagebox displays "This is some text."

4. You create the condition (and I am choosing my words carefully) that .Exists = False

5. You test - Msgbox object.Range.Text - and a messagebox will STILL display "This is some text."

In other words, once this object HAS text, that text value persists regardless of whether .Exists is TRUE or FALSE.

Gerry
 
Gerry,

HeaderFooter object?

I'd say it's worthwhile writing the FAQ


Regards,
Mike
 
Hi gerry,

I'll second Mike on that. You know more about how they work than most people and I have seen some detailed posts from you on the subject.

If you are talking about Header and Footer objects then I quite agree that .Exists is a very poorly chosen name for something which always does exist. I will just say, however, that you don't need to set .Exists to True in order to be able to set the .Range.Text property. The objects exist, period, and can be manipulated any way one wishes.

And, yes, a FAQ would be good.

I'm off to check now, just in case there is something else with an .Exists property - I have a nagging feeling at the back of my mind that there is.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Exists is a method of the Bookmarks and Tasks collection, and a property of the HeaderFooter object.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
HEY! I am SOOOOO happy at the speed of replies, and the ones who I suspected would get it, in fact did.

Yes headers and footers. The reason I tossed the .Exists aspect in was that I have seen posts where someone was using the fact of .Exists = False to make the conclusion the object was not there. And even commented it as such. Are there other objects that .Exists is, hmmmm, rather irrelevant?

OK, a FAQ it is. Thanks to you guys for once again impressing me with the high quality of helpful people who are in this forum.

Just to point though. Not ALL headerfooter objects have this somewhat odd aspect. It is only DifferentFirstPage and DifferentOddEven that can have .Exists = False. Primary can not be set = False. You can try, and it does not cause an error, but it simply is ignored.

ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Exists = False
MsgBox ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Exists

will return TRUE. Primary is always true.

However, you CAN set the others (DifferentFirstPage, and DifferentOddEven to False.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top