Results 1 to 11 of 11
  1. #1
    JustCallMeAl
    Guest

    Function gblnSetTrueFalseDocumentVariable

    Here is another function for your consideration. Optimizing suggestions are always welcome.

    <pre>Public Function gblnSetTrueFalseDocumentVariable( _
    ByVal vwdWordDocument As Document, _
    ByVal vstrVariableName As String, _
    ByVal vblnNewVariableValue As Boolean _
    ) As Boolean

    '---------------------------------------
    'Purpose: Set a document variable true/false value whether or
    ' not it currently exists. If exist, reset value,
    ' if not exist, add and set value.
    'Function Return: True if variable is set.
    '---------------------------------------

    Dim varExistInDocument As Variable
    Dim blnVariableSet As Boolean

    blnVariableSet = False

    For Each varExistInDocument In vwdWordDocument.Variables
    If varExistInDocument.Name = vstrVariableName Then
    varExistInDocument.Value = vblnNewVariableValue
    blnVariableSet = True
    gblnSetTrueFalseDocumentVariable = True
    Exit For
    Else
    blnVariableSet = False
    End If
    Next varExistInDocument

    If blnVariableSet = False Then
    vwdWordDocument.Variables.Add vstrVariableName, vblnNewVariableValue
    gblnSetTrueFalseDocumentVariable = True
    End If ' blnVariableSet

    End Function</pre>


  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Function gblnSetTrueFalseDocumentVariable

    Al,

    Observation: The function will always return true, so this should be a Sub, not a function.

    Minor item:

    I like
    If not blnVariableSet then

    instead of
    If blnVariableSet = False then

    personal preference.

    Otherwise looks good!
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Function gblnSetTrueFalseDocumentVariable

    Is there a reason for the sxecond "blnVariableSet = False"? It appears redundant, as it is set to false at the start.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  4. #4
    JustCallMeAl
    Guest

    Re: Function gblnSetTrueFalseDocumentVariable

    Kevin, Yes the function will always return true unless the thing blows out because of an error. Of course, there is no "other type" of error trapping in there. Shame on me. (I also did some reading of the archives where different people claim that every procedure should be a function <img src=/S/smile.gif border=0 alt=smile width=15 height=15>. I guess you may not have been one of them. <img src=/S/wink.gif border=0 alt=wink width=15 height=15>

    Interesting on the minor item. As a general rule, I find your preference difficult to read/comprehend, even though it is saying the same thing as the way I wrote it. However, in this case, with the way I "accidentally" crafted the variable name, it is not as obtuse as many others I have seen.

  5. #5
    JustCallMeAl
    Guest

    Re: Function gblnSetTrueFalseDocumentVariable

    None except to just make sure it stays the way I want it to. I guess this is paranoia-based!

  6. #6
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Function gblnSetTrueFalseDocumentVariable

    Another thought. If you used "Exit function" rather trhan "Exit for", you don't need to use blnVariableSet at all.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  7. #7
    JustCallMeAl
    Guest

    Re: Function gblnSetTrueFalseDocumentVariable

    Yes, Exit Function does fill the bill there, really what I want to happen.

    Thanks

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Function gblnSetTrueFalseDocumentVariable

    Actually, if you want to be strict about it, you don't even need the first one - since a boolean variable is set to False by default, when it's created.

    But I also tend to vote for paranoia in these things! <img src=/S/hairy.gif border=0 alt=hairy width=15 height=15>

  9. #9
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Function gblnSetTrueFalseDocumentVariable

    I'll vote for paranoia when it relies on default (not that Microsoft would ever changesuch fundamental things- not); if it's set explicitly, then it maight be a little too much.

    In light of the replies, could this be a fair summary:
    <pre>Public Sub gblnSetTrueFalseDocumentVariable( _
    ByVal vwdWordDocument As Document, _
    ByVal vstrVariableName As String, _
    ByVal vblnNewVariableValue As Boolean _
    )

    '---------------------------------------
    'Purpose: Set a document variable true/false value whether or
    ' not it currently exists. If exist, reset value,
    ' if not exist, add and set value.
    'Function Return: True if variable is set.
    '---------------------------------------

    Dim varExistInDocument As Variable
    Dim blnVariableSet As Boolean

    For Each varExistInDocument In vwdWordDocument.Variables
    If varExistInDocument.Name = vstrVariableName Then
    varExistInDocument.Value = vblnNewVariableValue
    blnVariableSet = True
    Exit Sub
    End If
    Next varExistInDocument

    vwdWordDocument.Variables.Add vstrVariableName, vblnNewVariableValue

    End Sub
    </pre>

    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  10. #10
    JustCallMeAl
    Guest

    Re: Function gblnSetTrueFalseDocumentVariable

    Yes, I now see where you were going with one of your last remarks about removing one the boolean lines.

    However, I just noticed, the line "blnVariableSet = True" isn't needed at all, is it? It's only purpose was for the last line of the procedure. In other words, if a particular variable was not in existence, then it would create/set one. However, with the Exit Sub, that makes the necessity for blnVariableSet obsolete, doesn't it? Because it certainly is not used later in the procedure.

  11. #11
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Function gblnSetTrueFalseDocumentVariable

    OK, right. And you can also remove the "Dim blnVariableSet As Boolean" line.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •