MS Office Forum / Word / Programming / October 2006
Choices, choices
|
|
Thread rating:  |
Greg Maxey - 21 Oct 2006 04:28 GMT 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?
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Jezebel - 21 Oct 2006 04:56 GMT 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?
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Greg Maxey - 21 Oct 2006 04:59 GMT Jezebel,
Thanks. I like your construction also.
 Signature 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.
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:eht$mDM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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?
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Tony Jollans - 21 Oct 2006 09:45 GMT 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.
 Signature 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.
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:eht$mDM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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?
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Jezebel - 21 Oct 2006 10:52 GMT 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.
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
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:uyBWcTM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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.
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:eht$mDM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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?
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Greg Maxey - 21 Oct 2006 13:08 GMT 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
 Signature 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 news:Of0XJ2O9GHA.3280@TK2MSFTNGP02.phx.gbl... 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
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:uyBWcTM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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.
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:eht$mDM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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?
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Jezebel - 21 Oct 2006 13:22 GMT The issue is that if oStyle is already set to something, then the possibly error-raising assingment won't clear it (eg, if the test code is within a loop). For more rigorous code, you should have
set oStyle = nothing
in there, before trying the assignment. But if the whole shebang is within a separate procedure, called once for a given style name -- as per your example -- oStyle is known to be nothing anyway, so it doesn't matter
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.
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:%23wQovaP9GHA.3264@TK2MSFTNGP04.phx.gbl... 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 news:Of0XJ2O9GHA.3280@TK2MSFTNGP02.phx.gbl... 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
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:uyBWcTM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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.
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:eht$mDM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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?
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Greg Maxey - 21 Oct 2006 13:30 GMT Got it. Thanks.
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
The issue is that if oStyle is already set to something, then the possibly error-raising assingment won't clear it (eg, if the test code is within a loop). For more rigorous code, you should have
set oStyle = nothing
in there, before trying the assignment. But if the whole shebang is within a separate procedure, called once for a given style name -- as per your example -- oStyle is known to be nothing anyway, so it doesn't matter
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:e1EJpmQ9GHA.3384@TK2MSFTNGP05.phx.gbl... 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.
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:%23wQovaP9GHA.3264@TK2MSFTNGP04.phx.gbl... 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 news:Of0XJ2O9GHA.3280@TK2MSFTNGP02.phx.gbl... 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
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:uyBWcTM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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.
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:eht$mDM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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?
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Greg Maxey - 21 Oct 2006 13:41 GMT Jezebel,
I tried to capture your method as an example on my tips page (its at the bottom) http://gregmaxey.mvps.org/Error_Handling_Basics.htm
I cited you, so if you have an issue with that let me know and I will remove it.
Thanks.
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
The issue is that if oStyle is already set to something, then the possibly error-raising assingment won't clear it (eg, if the test code is within a loop). For more rigorous code, you should have
set oStyle = nothing
in there, before trying the assignment. But if the whole shebang is within a separate procedure, called once for a given style name -- as per your example -- oStyle is known to be nothing anyway, so it doesn't matter
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:e1EJpmQ9GHA.3384@TK2MSFTNGP05.phx.gbl... 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.
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:%23wQovaP9GHA.3264@TK2MSFTNGP04.phx.gbl... 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 news:Of0XJ2O9GHA.3280@TK2MSFTNGP02.phx.gbl... 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
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:uyBWcTM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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.
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:eht$mDM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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?
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Tony Jollans - 21 Oct 2006 14:22 GMT Where I live all the water is muddy <g>
Sorry Greg. I see Jezebel has already answered you.
 Signature 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.
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:%23wQovaP9GHA.3264@TK2MSFTNGP04.phx.gbl... 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 news:Of0XJ2O9GHA.3280@TK2MSFTNGP02.phx.gbl... 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
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:uyBWcTM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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.
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:eht$mDM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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?
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Greg Maxey - 21 Oct 2006 14:43 GMT LOL. A good friend has house in Cyprus for sale ;-)
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Where I live all the water is muddy <g>
Sorry Greg. I see Jezebel has already answered you.
-- Enjoy, Tony
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:e1EJpmQ9GHA.3384@TK2MSFTNGP05.phx.gbl... 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.
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:%23wQovaP9GHA.3264@TK2MSFTNGP04.phx.gbl... 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 news:Of0XJ2O9GHA.3280@TK2MSFTNGP02.phx.gbl... 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
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:uyBWcTM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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.
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:eht$mDM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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?
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Tony Jollans - 21 Oct 2006 15:03 GMT 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.
 Signature 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 news:es4QARR9GHA.1808@TK2MSFTNGP03.phx.gbl... Where I live all the water is muddy <g>
Sorry Greg. I see Jezebel has already answered you.
-- Enjoy, Tony
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:e1EJpmQ9GHA.3384@TK2MSFTNGP05.phx.gbl... 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.
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:%23wQovaP9GHA.3264@TK2MSFTNGP04.phx.gbl... 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 news:Of0XJ2O9GHA.3280@TK2MSFTNGP02.phx.gbl... 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
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:uyBWcTM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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.
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:eht$mDM9GHA.3264@TK2MSFTNGP04.phx.gbl... 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?
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Graham Mayor - 21 Oct 2006 16:03 GMT 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
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
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.
 Signature Enjoy, Tony
LOL. A good friend has house in Cyprus for sale ;-)
 Signature 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 news:es4QARR9GHA.1808@TK2MSFTNGP03.phx.gbl... 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
 Signature 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 news:Of0XJ2O9GHA.3280@TK2MSFTNGP02.phx.gbl... 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.
 Signature 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?
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Lüko Willms - 21 Oct 2006 10:52 GMT Am Sat, 21 Oct 2006 03:28:06 UTC, schrieb "Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> auf microsoft.public.word.vba.general :
> 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.
|
|
|