Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2007

Tip: Looking for answers? Try searching our database.

Checking Existence of Variable / Bypass the compiler check

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michiel - 21 Dec 2007 14:41 GMT
Hi,

I should like to write a sub that MIGHT write data to a public variable.
Meaning:
-If the Public variable exists the sub assigns a value.
-If the public variable does not exist. It just ignores it.

For example:
------------------------------------------------------------------------------
----------------
Option Explicit
Public strOutsideVar as String  'This line might be present

Sub TheSub()
 Dim strInsideVar as String

 strInsideVar = "Hallo" & strInsideVar  
 strOutsideVar  = strInsideVar 'Only be executed if the public variable
(strOutsideVar is available)
End Sub

I have the "Option Explicit" switched on.
If the Public Variable is absent , upon running I get a compile error message.

How can I prevent that or how could I get the idea implemented?

Thanks

Michiel.
Jay Freedman - 21 Dec 2007 15:16 GMT
What you request is not possible. But I don't understand how, if you're
writing the code, you don't know in advance whether or not the variable
exists. If you expect to need it, just declare it. It won't hurt anything if
it's never used.

If the variable could be declared in a different module that may or may not
be present, that's a different story... but I still don't think there's any
solution.

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Hi,
>
[quoted text clipped - 26 lines]
>
> Michiel.
Michiel - 21 Dec 2007 15:28 GMT
Thanks Jay!

The reason why I want it is the fact that I want to use the sub in many
different applications.
If the Public Variable is necessary I needs to be declared of course but if
it is not used I do not want to pollute my codings with Publics wich are not
used.

M.

>What you request is not possible. But I don't understand how, if you're
>writing the code, you don't know in advance whether or not the variable
[quoted text clipped - 10 lines]
>>
>> Michiel.
Jay Freedman - 21 Dec 2007 18:16 GMT
As attractive as the idea of keeping your projects "clean" may be, it
doesn't do any good if it doesn't work.

Maybe you should think about passing the variable as an argument instead of
declaring it as Public. Then only the projects that call this procedure need
to know about the argument. It also reduces the chance that the Public
variable might be corrupted by other procedures that mistakenly use its
name.

> Thanks Jay!
>
[quoted text clipped - 20 lines]
>>>
>>> Michiel.
Michiel - 21 Dec 2007 19:10 GMT
Jay,

I follow your wise advise!
Thanks, and,...

HAPPY HOLIDAYS!

M.

>As attractive as the idea of keeping your projects "clean" may be, it
>doesn't do any good if it doesn't work.
[quoted text clipped - 10 lines]
>>>>
>>>> Michiel.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.