Results 1 to 15 of 15
  1. #1
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Word Constants (Word 2K/XP)

    Dear Woodytes,

    I am trying to use the constant wdListNumberStyleBullet, as I have a subroutine which I pass parameters to depending on the conditions so I want to pass the NumberStyle each time. I haven't quite got the hang of using these constants, I know that the numeric value of this constant is 23 but I prefer to have the symbolic constant as it makes the code more undertsandable. However I can't work out how to define the variable that will hold this and i get an error no matter what I try. The relevant code looks like this...

    Private xxNumberStyle As Variant ' I have tried other types but with no success)

    ....sub1 xxNumberStyle:=wdListNumberStyleBullet ' call the subroutine with named parameters
    _________
    Sub sub1(wdListNumberStyleBullet) ' run the subroutine
    .NumberStyle = xxNumberStyle ' here it fails with "out of range"
    end sub

    I know it's a simple problem of my lack of VBA experience.... ideas please

    liz

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word Constants (Word 2K/XP)

    When you define a procedure, you must declare the arguments as variables.

    Sub sub1(wdListNumberStyleBullet) is very confusing since it declares wdListNumberStyleBullet to be the [b[variable[/b] argument (of type Variant since you don't specify the type explicitly), but wdListNumberStyleBullet is also a built-in constant. Within sub1 this constant will not be available, VBA just can't "see" it because it has been redefined.

    Also .NumberStyle = ... has no meaning outside a With ... End With block.

    You could do something like this (whether it's useful is something else, and sub1 is a horribly non-descriptive name for a procedure):

    Sub sub1(xxNumberStyle As WdListNumberStyle)
    With ActiveDocument.ListTemplates(1).ListLevels(1)
    .NumberStyle = xxNumberStyle
    ' set other properties here if necessary
    End With
    End Sub

    Call it like this:

    sub1 xxNumberStyle:=wdListNumberStyleBullet

    Here the variable argument xxNumberStyle is assigned the constant value wdListNumberStyleBullet.

  3. #3
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Word Constants (Word 2K/XP)

    Hans,

    First let me say that the bit of code I gave was an example of what I am doing as the code itself is extensive and complex; my procedures and variables are NEVER named ambiguously and all my code used OPtion explicit, so thanks for the lesson in good practice but I'm doing it anyway!

    Sorry - the .numberstyle is within a With...End block I omitted it from the example and I typed the parameter in the performed subroutine incorrectly, I don't use the wd constant I use my variable. so more acurately & with more context the code is:

    ' In Declaratives I have all the variables including:
    ........Private xxNumberStyle As Variant ' I have tried other types but with no success


    'In a procedure I call a subroutine with several parameters, this example only shows the problem line, like this:
    ........usefulnamesub xxNumberStyle:=wdListNumberStyleBullet

    _________
    ........Sub usefulnamesub(xxNumberStyle, xxStyleNumberStart, xxStyleNumberEnd, xxNumberFormatArray, xxListTemplateName) ' run the subroutine
    ............ Set xxListTemplate = ActiveDocument.ListTemplates.Add(OutlineNumbered:= False)
    ............ xxListTemplate.Name = xxListTemplateName ' sets the list template name
    ............ For xxStyleNumber = xxStyleNumberStart To xxStyleNumberEnd ' loops for the required number of times
    ................ xxNumberFormat = xxNumberFormatArray(xxStyleNumber) ' selects the right number format from an array
    .................... With xxListTemplate.ListLevels(xxStyleNumber + 1) ' because starts at 1
    ........................ .NumberStyle = xxNumberStyle ' here it fails with "out of range"
    ..................... End with
    ................. next xxStyleNumber
    end sub

    Locals show that xxNumberStyle holds 23 as a value, I think the probelms is to do with my definition of xxNumberStyle. I need to do something like this as this is a called subroutine that loops for a prescibed number of times creating similar styles using different bullets so an array holds the buttet values and the .NumberStyle parameter must define that it is bullets, other calls to the routine create other list sytles using alpha and number NumberStyles.

    Dots represent spaces as I haven't discovered how to paste in fromatted text without it compressinh n spaces to one.

    hope this is now clearer, thank you .................... liz

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word Constants (Word 2K/XP)

    Liz,

    I can't really help you with the code, I'm out of my depth here, but you might try xxNumberStyle As WdListNumberStyle both in the declaration of the variable:

    Private xxNumberStyle As WdListNumberStyle

    and in the procedure header:

    Sub usefulnamesub(xxNumberStyle As WdListNumberStyle, ...)

  5. #5
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Word Constants (Word 2K/XP)

    I have tried this to no avail

    anyone have any other ideas????

    liz

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Constants (Word 2K/XP)

    Probably not a solution to your problem, but one thing I noted:

    You omitted the Byval or Byref keyword in the argument declaration of the sub. Default is byref, making the called sub change the argument variable in the calling routine too, should the called sub change that variable. Unexpected things may happen!
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Constants (Word 2K/XP)

    BTW:

    This works fine for me:

    Sub test()
    Dim a As Long
    a = wdListNumberStyleBullet
    MsgBox a
    End Sub
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  8. #8
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Word Constants (Word 2K/XP)

    More information...

    I changed the code to use .NumberStyle = wdListNumberStyleArabic
    and it works perfectly
    changing only that line to .NumberStyle = wdListNumberStyleBullet
    gives an out of range error

    I am mystified??? is there any other parameter which has a bearing on how the .NumberStyle is used?

  9. #9
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Word Constants (Word 2K/XP)

    Jan Karel,

    I thought that byref was more efficient and in some cases the values could be changed. I don't think it's a problem in this case. However I haven't yet found a very satisfactory explanation of these two keywords and their recommended usage. If yuo know of any good text please let me know.

    liz

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Constants (Word 2K/XP)

    Maybe this page gives some ideas.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  11. #11
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Word Constants (Word 2K/XP)

    and after much fiddling it seems that this probelm is not .NumberStyle although that is where it manifests the Out of Range probelm. It's related to a previous line setting the number format (using .NumberFormat )
    depending on when the subroutine is called the format will change so I have tried to set up an array to hold the bullets defined by e.g. ChrW(61602)

    the array is xxNumberFormatArray is defined as a Long whilst the valuesNumberFormatArray is defined as a variant, in summary the code does this:

    set up the values: valuesNumberFormatArray = Array(61602, 61553, 61631, 61558, 61618, "", "", "", "")

    call the subroutine using : CreateListTemplateForLists xxNumberFormatArray:=valuesNumberFormatArray
    (note, I can't remember why I had to so this two step process, but it works so I've left it)
    then set the NumberFormat using the relevant array entry: .NumberStyle = xxNumberFormatArray(xxStyleNumber)
    (note, xxstylenumber controls the loop)

    I know the code is bascally sound as when I substitute one specific value in the .NumberFormat & .NumberStyle lines everthing is fine, this must be to do with how I am defining the variable????

    any more ideas

    liz

  12. #12
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Word Constants (Word 2K/XP)

    <P ID="edit" class=small>(Edited by jscher2000 on 11-Jul-03 14:44. Added parentheses on the ParamArray variable.)</P>Hi, Liz. I have been avoiding this topic because I seldom use automatic list numbering and I like it less the more I try. Be that as it may, the following might help:

    1. <LI>To display code on the Lounge with its original indentation, surround it with <!t>[pre]<!/t> and <!t>[/pre]<!/t> tags.

      <LI>Although you said that you tried variable types other than variant, I think it is the use of variant that is causing the problem, 'though I could be wrong because I have only been able to review snippets. Try this:

      <pre>Sub MakeList(xxNumberStyle As Long, xxStyleNumberStart As Long, _
      xxStyleNumberEnd As Long, ParamArray xxNumberFormatArray() As Variant, _
      xxListTemplateName As String)
      'Same code you have now
      End Sub</pre>
    Does this make any difference?

  13. #13
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Word Constants (Word 2K/XP)

    jscher,

    Thanks for the tip (1).
    I have solved the immediate problem, se below, a good night's sleep does wonders!

    The code I am developing is a clean-up of some long, and largely recorded, code that I have used well for years, it create templates & styles as I want them and clean (particularly very few list templates). As I had some time on my hands I decided to, finally, convert the code to something more efficient and simpler to maintain. I haven't coded for 20 years and had avioded learning a new language out of lazyiness and since macro recording with a few tweaks has been good enough. So I have good general programming practice but a lack of VBA experience so please excuse the odd mixture of dumb and not-so questions.

    Am I right in assuming that if I have delared and typed the variables (and I'm using option explicit) i don't need to specify the variable type in the subrroutine definition as you have? I'm not and it's OK is there a reason to do it as you showed (or was this for clarity)

    What is ParamArray? I can't find reference to it in Help or the Object Browser - I assume it defines a parameter array but how does it work and what are the benefits over what I was doing?

    I was going to ask this: if I use .NumberFormat = ChrW(xxNumberFormat) where xxNumberFormat is defined as long and it holding the right value, in this case 61602, when I step through the code .NumberFormat shows a "?" so it has not recognised the line above as .NumberFormat=ChrW(61602) how do I acheive this? But I found that it didn't recognise the number format even if I did .NumberFormat=ChrW(61602), even though this is what a recorded macro generates and will work, I have solved this now by explicitly stating the Font for the bullet using the .Name property. This is a bit odd as a recorded macro doesn't complete this it is set as "" but the code works and also the number is a combinaion representing typeface and member fo it should resolve successfully. Anyway since it's OK now I shall just have to remember in future.

    You said you don't use auto list numbering... so what do you use? I have used them successfully for years providing I follow some simple rules especially: build on a clean tempate, define the styles and then the list templates, don't use any other way and certainly not the bullets/numbering buttons.

    thanks for all your time.............. liz

    p.s. no hurry for the answers above but I'm interested when you have time

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word Constants (Word 2K/XP)

    Liz, you ask
    <hr>Am I right in assuming that if I have delared and typed the variables (and I'm using option explicit) i don't need to specify the variable type in the subrroutine definition as you have?<hr>
    This is based on a misunderstanding. You have given your variables the same name as the arguments to the procedure; this is allowed but not necessary. I'll give a very simple (and useless) example:

    Sub Test(lngArg As Long)
    MsgBox lngArg + 32000
    End Sub

    Sub RunTest1()
    Dim lngNumber As Long
    lngNumber = 15000
    Test lngNumber
    End Sub

    Sub RunTest2()
    Dim strText As String
    strText = "Lounge"
    Test strText
    End Sub

    As you see, the argument to the Test procedure is named lngArg, but it is called from RunTest1 with lngNumber as argument. This is completely legitimate.

    If you try to compile the code, you'll get a compilation error in RunTest2 because you try to call Test with a string argument, while Test expects a Long. If you don't state the type of lngArg explicitly, you wouldn't get a compile error, but you would get a run time error if you tried to execute RunTest2. So declaring the argument type explicitly is safer, you'll catch errors earlier.

    You can read about ParamArray if you look up 'Sub, instruction' in the online help. I don't think Jefferson's suggestion will work, for a ParamArray argument must be the last argument in a procedure or function.

    Here is an example of using ParamArray:

    Function SumVariable(ParamArray lngArgs())
    Dim i As Long
    For i = LBound(lngArgs) To UBound(lngArgs)
    SumVariable = SumVariable + lngArgs(i)
    Next i
    End Function

    You can call this as MsgBox SumVariable(23, 37) or MsgBox SumVariable(1, 2, 3, 4) or MsgBox SumVariable(12, 31, 43, 98, 12, 3, 62, 11, 9, 28).

  15. #15
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Word Constants (Word 2K/XP)

    Regarding where to declare, either way might work, but principles of encapsulation suggest using local names inside procedures unless there's a good reason not to.

    Regarding parameter arrays, see Microsoft Office 2000/Visual Basic Programmer's Guide: Using Parameter Arrays. Hans is correct that I put it in the wrong place; it must be last. There are other ways to pass arrays, but if you want to do it through a variant, I think this is the most logical way.

    Regarding list numbering: I type the numbers myself!! (Unless it's an outline, then I use a custom-built outline template.)

Posting Permissions

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