Choices, choices

G

Greg Maxey

AFAIK, and unlike with bookmarks and tasks, there is no "Exists" procedure for determining if a Style is present in a document.

One way I can do this is:

Sub Test1()
Dim oStyle As Style
Dim styName As String
styName = "Normal"
For Each oStyle In ActiveDocument.Styles
If oStyle.NameLocal = styName Then
MsgBox styName & " style exists in this document."
Exit Sub
End If
Next oStyle
MsgBox "Style not found in this document."
End Sub

I realize that for the purpose of determining if a style exists that the code above works fine and answers the question with most documents in the bat of the eye. Yet it seems inefficient and if there were say a few thousand or so something or another then rather than looking at each one it makes more sense to just call out the item in question and see if responds. I was cobbled together the following code using error handling. I figure if I attempt so action with a style that doesn't exists then it will immediately throw an error and Bob's your uncle.

Sub Test2()
On Error GoTo Handler
Dim styName As String
styName = "Normal"
Debug.Print ActiveDocument.Styles(styName).NameLocal
MsgBox styName & " style exists in this document."
Exit Sub
Handler:
If Err.Number = 5941 Then
MsgBox "Style not found in this document."
Err.Clear
End If
End Sub

As many of you know I am not a purist and have no formal training in VBA. I sheepishly admit that I am still as dumb as a box of rocks wrt most of the technical aspects of the object model (whatever that means) ;-).

My questions. Is there anything unsound or fundamentally wrong with the approach used in Sub Test2?
 
J

Jezebel

There's nothing wrong with using error trapping like this. For humans, the word 'error' has connotations of 'bad' and 'mistake'; the computer has no such preconceptions: an error is an just a condition, same as 'no error'.

My preferred construction for this kind of testing is along these lines --

on error resume next
set oStyle = ActiveDocument.Styles(styleName)
on error goto 0 [or goto ErrorHandler]

If oStyle is nothing then
.... style is not defined
end if


This is only a stylistic preference: I prefer to handle errors as a form of testing within the body of the code and reserve the error-handler for exceptions that have to be handled in special ways.





AFAIK, and unlike with bookmarks and tasks, there is no "Exists" procedure for determining if a Style is present in a document.

One way I can do this is:

Sub Test1()
Dim oStyle As Style
Dim styName As String
styName = "Normal"
For Each oStyle In ActiveDocument.Styles
If oStyle.NameLocal = styName Then
MsgBox styName & " style exists in this document."
Exit Sub
End If
Next oStyle
MsgBox "Style not found in this document."
End Sub

I realize that for the purpose of determining if a style exists that the code above works fine and answers the question with most documents in the bat of the eye. Yet it seems inefficient and if there were say a few thousand or so something or another then rather than looking at each one it makes more sense to just call out the item in question and see if responds. I was cobbled together the following code using error handling. I figure if I attempt so action with a style that doesn't exists then it will immediately throw an error and Bob's your uncle.

Sub Test2()
On Error GoTo Handler
Dim styName As String
styName = "Normal"
Debug.Print ActiveDocument.Styles(styName).NameLocal
MsgBox styName & " style exists in this document."
Exit Sub
Handler:
If Err.Number = 5941 Then
MsgBox "Style not found in this document."
Err.Clear
End If
End Sub

As many of you know I am not a purist and have no formal training in VBA. I sheepishly admit that I am still as dumb as a box of rocks wrt most of the technical aspects of the object model (whatever that means) ;-).

My questions. Is there anything unsound or fundamentally wrong with the approach used in Sub Test2?
 
G

Greg Maxey

Jezebel,

Thanks. I like your construction also.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

There's nothing wrong with using error trapping like this. For humans, the word 'error' has connotations of 'bad' and 'mistake'; the computer has no such preconceptions: an error is an just a condition, same as 'no error'.

My preferred construction for this kind of testing is along these lines --

on error resume next
set oStyle = ActiveDocument.Styles(styleName)
on error goto 0 [or goto ErrorHandler]

If oStyle is nothing then
.... style is not defined
end if


This is only a stylistic preference: I prefer to handle errors as a form of testing within the body of the code and reserve the error-handler for exceptions that have to be handled in special ways.





AFAIK, and unlike with bookmarks and tasks, there is no "Exists" procedure for determining if a Style is present in a document.

One way I can do this is:

Sub Test1()
Dim oStyle As Style
Dim styName As String
styName = "Normal"
For Each oStyle In ActiveDocument.Styles
If oStyle.NameLocal = styName Then
MsgBox styName & " style exists in this document."
Exit Sub
End If
Next oStyle
MsgBox "Style not found in this document."
End Sub

I realize that for the purpose of determining if a style exists that the code above works fine and answers the question with most documents in the bat of the eye. Yet it seems inefficient and if there were say a few thousand or so something or another then rather than looking at each one it makes more sense to just call out the item in question and see if responds. I was cobbled together the following code using error handling. I figure if I attempt so action with a style that doesn't exists then it will immediately throw an error and Bob's your uncle.

Sub Test2()
On Error GoTo Handler
Dim styName As String
styName = "Normal"
Debug.Print ActiveDocument.Styles(styName).NameLocal
MsgBox styName & " style exists in this document."
Exit Sub
Handler:
If Err.Number = 5941 Then
MsgBox "Style not found in this document."
Err.Clear
End If
End Sub

As many of you know I am not a purist and have no formal training in VBA. I sheepishly admit that I am still as dumb as a box of rocks wrt most of the technical aspects of the object model (whatever that means) ;-).

My questions. Is there anything unsound or fundamentally wrong with the approach used in Sub Test2?
 
T

Tony Jollans

A slight addition to this - and I use similar constructs - is that you must initialise oStyle first.

If the style doesn't exist the statement will fail and ostyle will remain as it was before so the test for Nothing will only be good if oStyle was Nothing before.

--
Enjoy,
Tony

There's nothing wrong with using error trapping like this. For humans, the word 'error' has connotations of 'bad' and 'mistake'; the computer has no such preconceptions: an error is an just a condition, same as 'no error'.

My preferred construction for this kind of testing is along these lines --

on error resume next
set oStyle = ActiveDocument.Styles(styleName)
on error goto 0 [or goto ErrorHandler]

If oStyle is nothing then
.... style is not defined
end if


This is only a stylistic preference: I prefer to handle errors as a form of testing within the body of the code and reserve the error-handler for exceptions that have to be handled in special ways.





AFAIK, and unlike with bookmarks and tasks, there is no "Exists" procedure for determining if a Style is present in a document.

One way I can do this is:

Sub Test1()
Dim oStyle As Style
Dim styName As String
styName = "Normal"
For Each oStyle In ActiveDocument.Styles
If oStyle.NameLocal = styName Then
MsgBox styName & " style exists in this document."
Exit Sub
End If
Next oStyle
MsgBox "Style not found in this document."
End Sub

I realize that for the purpose of determining if a style exists that the code above works fine and answers the question with most documents in the bat of the eye. Yet it seems inefficient and if there were say a few thousand or so something or another then rather than looking at each one it makes more sense to just call out the item in question and see if responds. I was cobbled together the following code using error handling. I figure if I attempt so action with a style that doesn't exists then it will immediately throw an error and Bob's your uncle.

Sub Test2()
On Error GoTo Handler
Dim styName As String
styName = "Normal"
Debug.Print ActiveDocument.Styles(styName).NameLocal
MsgBox styName & " style exists in this document."
Exit Sub
Handler:
If Err.Number = 5941 Then
MsgBox "Style not found in this document."
Err.Clear
End If
End Sub

As many of you know I am not a purist and have no formal training in VBA. I sheepishly admit that I am still as dumb as a box of rocks wrt most of the technical aspects of the object model (whatever that means) ;-).

My questions. Is there anything unsound or fundamentally wrong with the approach used in Sub Test2?
 
L

Lüko Willms

Am Sat, 21 Oct 2006 03:28:06 UTC, schrieb "Greg Maxey"
Sub Test2()

I would implement this as a function:

Function StyleExists (styName As String) As Boolean
Dim testName As String
On Error GoTo Handler
testName = ActiveDocument.Styles(styName).NameLocal
StyleExists = True
Exit function
Handler:
If Err.Number = 5941 Then StyleExists = False
Err.Clear
End If
End Function


Same applies to the existence of DocVariables and such stuff.

I just try to generalise it to any kind of collection, passing the
collection as a parameter of type Object to the function, but run into
problems. I get an error number 91.

Where do I find all those error numbers?

Yours,
L.W.
 
J

Jezebel

Yes, I actually put that into the original post, then deleted it for the sake of simplicity. Then again, if it's part of a separate procedure (as in Greg's example), the initialisation happens by default anyway.



"Tony Jollans" <my forename at my surname dot com> wrote in message A slight addition to this - and I use similar constructs - is that you must initialise oStyle first.

If the style doesn't exist the statement will fail and ostyle will remain as it was before so the test for Nothing will only be good if oStyle was Nothing before.

--
Enjoy,
Tony

There's nothing wrong with using error trapping like this. For humans, the word 'error' has connotations of 'bad' and 'mistake'; the computer has no such preconceptions: an error is an just a condition, same as 'no error'.

My preferred construction for this kind of testing is along these lines --

on error resume next
set oStyle = ActiveDocument.Styles(styleName)
on error goto 0 [or goto ErrorHandler]

If oStyle is nothing then
.... style is not defined
end if


This is only a stylistic preference: I prefer to handle errors as a form of testing within the body of the code and reserve the error-handler for exceptions that have to be handled in special ways.





AFAIK, and unlike with bookmarks and tasks, there is no "Exists" procedure for determining if a Style is present in a document.

One way I can do this is:

Sub Test1()
Dim oStyle As Style
Dim styName As String
styName = "Normal"
For Each oStyle In ActiveDocument.Styles
If oStyle.NameLocal = styName Then
MsgBox styName & " style exists in this document."
Exit Sub
End If
Next oStyle
MsgBox "Style not found in this document."
End Sub

I realize that for the purpose of determining if a style exists that the code above works fine and answers the question with most documents in the bat of the eye. Yet it seems inefficient and if there were say a few thousand or so something or another then rather than looking at each one it makes more sense to just call out the item in question and see if responds. I was cobbled together the following code using error handling. I figure if I attempt so action with a style that doesn't exists then it will immediately throw an error and Bob's your uncle.

Sub Test2()
On Error GoTo Handler
Dim styName As String
styName = "Normal"
Debug.Print ActiveDocument.Styles(styName).NameLocal
MsgBox styName & " style exists in this document."
Exit Sub
Handler:
If Err.Number = 5941 Then
MsgBox "Style not found in this document."
Err.Clear
End If
End Sub

As many of you know I am not a purist and have no formal training in VBA. I sheepishly admit that I am still as dumb as a box of rocks wrt most of the technical aspects of the object model (whatever that means) ;-).

My questions. Is there anything unsound or fundamentally wrong with the approach used in Sub Test2?
 
G

Graham Mayor

We have had so much rain here in Cyprus in the last week that the water in
my pool is muddy .... OK cloudy .... but it is still 26 degrees :)

The holiday home is still for sale if you need a bolt hole ;)
http://www.gmayor.com/house_for_sale.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


"Tony Jollans" <my forename at my surname dot com> wrote in message
Hi Greg,

My original comment referred, rather obliquely, to the fact that I live on a
farm but your mention of Cyprus (where I'd love to live!) reminded me of the
sea. Several years ago I went to Norway on holiday and I remember clearly
the contrast between the clarity of the water in Bergen harbour and the
opacity of ostensibly the same liquid at the other end of the journey in
Newcastle.

--
Enjoy,
Tony

LOL. A good friend has house in Cyprus for sale ;-)

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

"Tony Jollans" <my forename at my surname dot com> wrote in message
Where I live all the water is muddy <g>

Sorry Greg. I see Jezebel has already answered you.

--
Enjoy,
Tony

There you two go again using phrases that muddy the water (...you must
initialise oStyle first). Here is a revision of my code and it is a
separate procedure. Not assuming to undertand anything, I take Jezebel's
word that as it is a separate procedure that it is "initialised" by default.
Would one of you be so kind as to explain what initialise means and how it
would be accomplished if the code wasn't a separate procedure. Thanks.

Sub UsingErrorHandling()
Dim oStyle As Style
Dim styleName As String
styleName = "Goobledygook"
On Error Resume Next
Set oStyle = ActiveDocument.Styles(styleName)
On Error GoTo 0
If Not oStyle Is Nothing Then
MsgBox StyleName & " style exists in this docuement"
Else
MsgBox StyleName & " style is not found in this docuement"
End If
End Sub

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Yes, I actually put that into the original post, then deleted it for the
sake of simplicity. Then again, if it's part of a separate procedure (as in
Greg's example), the initialisation happens by default anyway.



"Tony Jollans" <my forename at my surname dot com> wrote in message
A slight addition to this - and I use similar constructs - is that you must
initialise oStyle first.

If the style doesn't exist the statement will fail and ostyle will remain as
it was before so the test for Nothing will only be good if oStyle was
Nothing before.

--
Enjoy,
Tony

There's nothing wrong with using error trapping like this. For humans, the
word 'error' has connotations of 'bad' and 'mistake'; the computer has no
such preconceptions: an error is an just a condition, same as 'no error'.

My preferred construction for this kind of testing is along these lines --

on error resume next
set oStyle = ActiveDocument.Styles(styleName)
on error goto 0 [or goto ErrorHandler]

If oStyle is nothing then
.... style is not defined
end if


This is only a stylistic preference: I prefer to handle errors as a form of
testing within the body of the code and reserve the error-handler for
exceptions that have to be handled in special ways.





AFAIK, and unlike with bookmarks and tasks, there is no "Exists" procedure
for determining if a Style is present in a document.

One way I can do this is:

Sub Test1()
Dim oStyle As Style
Dim styName As String
styName = "Normal"
For Each oStyle In ActiveDocument.Styles
If oStyle.NameLocal = styName Then
MsgBox styName & " style exists in this document."
Exit Sub
End If
Next oStyle
MsgBox "Style not found in this document."
End Sub

I realize that for the purpose of determining if a style exists that the
code above works fine and answers the question with most documents in the
bat of the eye. Yet it seems inefficient and if there were say a few
thousand or so something or another then rather than looking at each one it
makes more sense to just call out the item in question and see if responds.
I was cobbled together the following code using error handling. I figure if
I attempt so action with a style that doesn't exists then it will
immediately throw an error and Bob's your uncle.

Sub Test2()
On Error GoTo Handler
Dim styName As String
styName = "Normal"
Debug.Print ActiveDocument.Styles(styName).NameLocal
MsgBox styName & " style exists in this document."
Exit Sub
Handler:
If Err.Number = 5941 Then
MsgBox "Style not found in this document."
Err.Clear
End If
End Sub

As many of you know I am not a purist and have no formal training in VBA. I
sheepishly admit that I am still as dumb as a box of rocks wrt most of the
technical aspects of the object model (whatever that means) ;-).

My questions. Is there anything unsound or fundamentally wrong with the
approach used in Sub Test2?
 

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