• Help with Procedure (Word 2000 VBA)

    Author
    Topic
    #363050

    Sorry to put such a generic title, but I seem to be having a lot of different problems with a procedure note the “ISSUE #”s in the code as follows:

    Public Sub AnalyzeNumbers_Bullets()
    Dim DocThis As Document
    Dim ExistingListFormat As String
    ‘Dim ExistingListLevel As Integer
    Dim ExistingNumberStyle As String
    Dim ExistingNumberFormat As String
    Dim ExistingNumberStyleExample As String

    Dim ExistingParagraphIndent As String
    Dim ExistingListFormatParagraphIndentCombo As String
    Dim ExistingListFormatParagraphIndentComboCount As Integer

    Dim NewListFormat As String
    Dim NewParagraphIndent As String
    Dim iParCount As Integer
    Dim J As Integer, K As Integer
    Dim ExistingListFormatParagraphIndentComboInUse(499) As String

    Set DocThis = ActiveDocument
    ExistingListFormatParagraphIndentComboCount = 0

    iParCount = DocThis.Paragraphs.Count
    For J = 1 To iParCount
    ‘Ignore paragraphs in tables.
    If DocThis.Paragraphs(J).Range.Information(wdWithInTable) = False Then

    ‘ISSUE #1: This following line of code ignored all paragraphs in the document, not just
    ‘Ignore Headings.
    those who were using styles Heading 1 through Heading 9, which is what I intended.
    If DocThis.Paragraphs(J).Range.Information(wdRefTypeHeading) = False Then

    ‘ISSUE #2: The following line of code is intended to make the variable “ExistingNumberStyle” equal to the .NumberStyle of the current paragraph. I’m sure there is a better way.
    ExistingNumberStyle = DocThis.Paragraphs
    (J).Range.ListFormat.ListTemplate.ListLevels.Item.NumberStyle

    ‘ISSUE #3: The following line of code is intended to make the variable “ExistingNumberFormat” equal to the .NumberFormat of the current paragraph. I’m sure there is a better way.
    ExistingNumberFormat = DocThis.Paragraphs(J).Range.ListFormat.ListTemplate.ListLevels.Item.NumberFormat

    If ExistingNumberStyle = “wdListNumberStyleArabic” Then ExistingNumberStyleExample = “1”
    If ExistingNumberStyle = “wdListNumberStyleLowercaseLetter” Then ExistingNumberStyleExample = “a”
    If ExistingNumberStyle = “wdListNumberStyleUppercaseLetter” Then ExistingNumberStyleExample = “A”
    If ExistingNumberStyle = “wdListNumberStyleLowercaseRoman” Then ExistingNumberStyleExample = “i”
    If ExistingNumberStyle = “wdListNumberStyleUppercaseRoman” Then ExistingNumberStyleExample = “I”

    ‘ISSUE #4: Each of the following 9 lines of code is intended to replace the text in quotes contained in the string “ExistingNumberFormat” with the text in the string “ExistingNumberStyleExample”. However, I get the following error: “Function call on left-hand side of assignment must return Variant or Object.”
    ‘find #%within the string.
    Replace(ExistingNumberFormat, “1%”, ExistingNumberStyleExample) = ExistingListFormat
    Replace(ExistingNumberFormat, “2%”, ExistingNumberStyleExample) = ExistingListFormat
    Replace(ExistingNumberFormat, “3%”, ExistingNumberStyleExample) = ExistingListFormat
    Replace(ExistingNumberFormat, “4%”, ExistingNumberStyleExample) = ExistingListFormat
    Replace(ExistingNumberFormat, “5%”, ExistingNumberStyleExample) = ExistingListFormat
    Replace(ExistingNumberFormat, “6%”, ExistingNumberStyleExample) = ExistingListFormat
    Replace(ExistingNumberFormat, “7%”, ExistingNumberStyleExample) = ExistingListFormat
    Replace(ExistingNumberFormat, “8%”, ExistingNumberStyleExample) = ExistingListFormat
    Replace(ExistingNumberFormat, “9%”, ExistingNumberStyleExample) = ExistingListFormat

    ‘ISSUE #5: The following line of code is intended to make the variable “ExistingParagraphIndent” equal to left indent of the current paragraph. I’m getting a type mismatch error.
    DocThis.Paragraphs(J).LeftIndent = ExistingParagraphIndent

    ExistingListFormatParagraphIndentCombo = ExistingListFormat & “,” & ExistingParagraphIndent
    For K = 1 To ExistingListFormatParagraphIndentComboCount
    ExistingListFormatParagraphIndentComboCount = K
    ExistingListFormatParagraphIndentComboInUse(ExistingListFormatParagraphIndentComboCount) = _
    ExistingListFormatParagraphIndentCombo
    ‘Create a row of labels for this ExistingListFormatParagraphIndentCombo.

    I realize this is only part of my procedure, but the procedure itself is not finished. I appreciate your help with these varied issues!!

    Troy

    Viewing 1 reply thread
    Author
    Replies
    • #553116

      Hi Troy,

      I didn’t have the time to fully check your macro, but here are some ideas I had reading your macro.
      (I use Word2000)

      General:
      — I’d expect a variable ExistingListLevel to be defined as

      Dim ExistingListLevel as ListLevel

      (the same goes for many other variables; for an example of the troubles you are running into, see Issue #2 below, where you have assigned a number — wdListNumberStyle — to a string variable).
      — A loop “For Each myPara in DocThis.Paragraphs” would seem more elegant to me (and it’s a lot faster).

      Issue #1: Use instead

        ' List? (Not very elegant, either, but seems to work)
        If DocThis.Paragraphs(j).Range.ListFormat.SingleList = True Then
          ' No heading?
          If DocThis.Paragraphs(j).OutlineLevel =  wdOutlineLevelBodyText Then

      Issue #2/#3: The following seems to work (your code didn’t work for me: error for .Item)

        With DocThis.Paragraphs(j).Range.ListFormat
          ExistingListLevelNumber = .ListLevelNumber
          ExistingNumberStyle = .ListTemplate.ListLevels(ExistingListLevelNumber).NumberStyle
          ' This will give you a wdListNumberStyle-Constant, not a String:
          MsgBox ExistingNumberStyle, vbOKOnly, "ExistingNumberStyle"
          If ExistingNumberStyle = wdListNumberStyleArabic then ...
        End With

      Issue #4: Replace() will return a string, and you can’t have a string on the left-hand side of an assignment.

      Issue #5: I’d make a “Select Case ExistingParagraphIndent” for the indent and check different ranges (“Case 0 to 5″… assuming that ExistingParagraphIndent is a number).

      I’m not sure what your macro is meant to achieve; perhaps the goal is to fix the numbering in documents that use manually applied numbering instead of list styles?
      If so, I don’t think it is worthwhile trying to do that without using/assigning list styles.
      As long as your lists are manually formatted, and thus based on the list templates from the ListGallery, you don’t stand much of a chance to get your numbering fixed for good. The gurus in the Word numbering newsgroup say that these templates are stored in the registry, and your documents will be sure to mess up if the entries in the registry change (at least if you paste between documents), or if you move the documents to another computer.
      The way to go probably would be to analyse the numbering, remove the numbering, and apply the appropriate list styles (for example, apply style “List 2” if the numbering is a lowercase letter and the indent is greater than zero but less than 10pt…).

      cheersKlaus

    • #553141

      Troy,

      Quite a lot going on here and like Klaus I haven’t yet figured out the overall gist of what this is for! anyway, here are a couple of things not covered in Klaus’ response:

      Issue #4: As Klaus noted, you can’t have the function on the left side that way, more likely you want to do something like:

      ExistingListFormat = Replace(arguments)

      which will assign the result of the Replace function to the ExistingListFormat variable (assuming it’s a string variable).

      But a second thing here is that you’re running the Replace function nine times and returning the result of the function to the same variable each time- each time, the value previously stored in the ExistingListFormat is going to be replaced by the new result.
      So if you’re trying to derive and store nine values which you can use further on, you either need nine variables or else store all of the values in an array.

      Issue #5: Any time you want to store a value that has to do with paragraph or tab indentation to a variable, you need to use a Single data type – the type mismatch is being caused by ExistingParagraphFormat having been declared as a String type and then trying to assign a value of Single type to it.
      Also, here too it looks like you’ve the two sides of the statement swapped the wrong way ’round; what you want here is:

      ExistingParagraphIndent = DocThis.Paragraphs(J)leftIndent

      Hope this helps – keep hacking!

      Gary

      • #553544

        Thanks to both Klaus and Gary for your help!!

        One outstanding issue:
        ExistingListFormat = Replace(ExistingNumberFormat, “1%”, ExistingNumberStyleExample)
        ExistingListFormat = Replace(ExistingNumberFormat, “2%”, ExistingNumberStyleExample)
        ExistingListFormat = Replace(ExistingNumberFormat, “3%”, ExistingNumberStyleExample)
        ExistingListFormat = Replace(ExistingNumberFormat, “4%”, ExistingNumberStyleExample)
        ExistingListFormat = Replace(ExistingNumberFormat, “5%”, ExistingNumberStyleExample)
        ExistingListFormat = Replace(ExistingNumberFormat, “6%”, ExistingNumberStyleExample)
        ExistingListFormat = Replace(ExistingNumberFormat, “7%”, ExistingNumberStyleExample)
        ExistingListFormat = Replace(ExistingNumberFormat, “8%”, ExistingNumberStyleExample)
        ExistingListFormat = Replace(ExistingNumberFormat, “9%”, ExistingNumberStyleExample)

        I swapped this around, but it still does not seem to be doing anything. What I am trying to do is look for the %# in the .NumberFormat and replace it with the ExistingNumberStyleExample value. The result would be like “1.” or “a)”. Instead I am still seeing ExistingListFormat as “%1.” To further explain, I am trying to get an example of exactly what is on the screen for this number format.

        In addition, let me give you the big picture (I purposefully left this out before because I wanted to continue figuring this out on my own as much as possible for learning purposes. But now I think I’ve learned all I can on this without help.). I am wanting to populate a form with a row of items for each distinct number format and left indent. The row will consist of a label for the number format, label for the left indent, and two combo boxes filled with valid choices for the number format and left indent. Basically I want to standardize a nonstandard document, but with the flexibility for the user to define what existing number formats are.

        I have attached a document with the form and code. BE AWARE I know the code isn’t totally finished. THe main thing I know it is missing is the code for positioning each label and combobox. I think I can handle that part, so don’t worry about adding that in. Please let me know if there is another better way of going about what I am doing.

        P.S. In case it looked familier, I based some of the logic for this on a procedure I got from here from you, Gary, for finding unused styles.

        • #553685

          Hi Troy,

          For starters, it looks like you’ve got the numeral and the percentage symbol swapped round, that is, it should look like:

          ExistingListFormat = Replace(ExistingNumberFormat, “%1”, ExistingNumberStyleExample)

          rather than the

          ExistingListFormat = Replace(ExistingNumberFormat, “1%”, ExistingNumberStyleExample)

          which you have.

          Also I’m still puzzing over the 9 Replace functions in a row, each one assigning the return value to the ExistingListFormat variable – what’s going to end up in ExistingListFormat is the value returned by the ninth and last Replace function – are you needing to store the return value of each of the 9 Replace functions – in which case you’re best off making ExistingListFormat an array.

          HTH,
          Gary

          • #553765

            Edited by TroyWells on 24-Nov-01 05:38.

            Sorry I forgot to explain that last time. There should be “1%” or “2%”…”9%” in the string. Because I don’t know which one, I’m searching for all of them. I assumed that that if it did not find the search string, it would not do anything to the string in which it was searching. Perhaps that is a bad assumption. This was one of the rare cases I have found that the online help did not provide as much information or an example.

            Perhaps I could set a variable for this to reduce the number of search or replaces. It would great if I could use a wildcard for “any digit”.

            NEW: I wanted to include an example of what I am searching.

            The value I am searching could be “(%1)” where I would replace “%1” with a value such as “a”.
            The value I am searching could be “%1.” where I would replace “%1” with a value such as “1”.

            Thanks again for all your help!! Hope you have a great Thanksgiving!!
            Troy

            • #553934

              Hi again Troy and thanks, it was a good Thanksgiving over here…

              When I try the following test macro:

              Sub Test()
              Dim ExistingListFormat As String
              Dim ExistingNumberFormat As String
              Dim ExistingNumberStyleExample As String

              ExistingNumberFormat = “%1”
              ExistingNumberStyleExample = “1”

              ExistingListFormat = Replace(ExistingNumberFormat, “%1”, ExistingNumberStyleExample)

              MsgBox ExistingListFormat ‘ Returns “1”

              End Sub

              – it returns “1”, which I think is what you’re looking for(?).
              In this case, I’d go back and make sure that you’ve got your ‘string to replace’ as “%1” and not “1%”.

              Gary

            • #553948

              What you have there does work for me. The problem is that I don’t know what the number is going to be after the “%”. Thus have been trying to use the folloing code, assuming that if the search string is not found, it is not replaced:

              ExistingListFormat = Replace(ExistingNumberFormat, “%1”, ExistingNumberStyleExample)
              ExistingListFormat = Replace(ExistingNumberFormat, “%2”, ExistingNumberStyleExample)
              ExistingListFormat = Replace(ExistingNumberFormat, “%3”, ExistingNumberStyleExample)
              ExistingListFormat = Replace(ExistingNumberFormat, “%4”, ExistingNumberStyleExample)
              ExistingListFormat = Replace(ExistingNumberFormat, “%5”, ExistingNumberStyleExample)
              ExistingListFormat = Replace(ExistingNumberFormat, “%6”, ExistingNumberStyleExample)
              ExistingListFormat = Replace(ExistingNumberFormat, “%7”, ExistingNumberStyleExample)
              ExistingListFormat = Replace(ExistingNumberFormat, “%8”, ExistingNumberStyleExample)
              ExistingListFormat = Replace(ExistingNumberFormat, “%9”, ExistingNumberStyleExample)

              If I just pick the right line of code (and comment out the rest) it works fine, but since I don’t know which one this will be I have been stepping through all of these lines.

              Any ideas how I can handle each of the possible cases above? If there was just a way that tell when a replace has occurred, I could skip the remaining steps.

              Thanks for any help you can give!! Enjoy the leftovers!!
              Troy

            • #553955

              Hi again,

              It might be possible to use wildcards within a Replace function – but if so I don’t know how!

              You know that the ExistingNumberFormat string contains a “%” followed by some number, and what you want to find out is what that number is.

              You can use a combination of functions: InStr, Left, Right and possibly Mid to determine the identity of the number that’s after the “%” – have a look at the VBA Help on these and see if you can work it out. (You can start with InStr to determine the position of the “%” in ExistingNumberFormat – then the challenge is to return the value of the character immediately following the “%”.)

              Once you’ve determined the identity of that number, you should only have to run the Replace function once – something along the lines of:

              ExistingListFormat = Replace(ExistingNumberFormat,”%” & strNumber,ExistingNumberStyleExample)

              Give it a try and let me know if that works out.

              Gary

            • #553975

              That last bit of advice did the trick. The following is the code related that I used successfully (including a message box to see that it worked):

              ‘find %#within the string.
              Dim NumFormatPos, NumFormatCharacter, NumFormatString
              NumFormatPos = InStr(ExistingNumberFormat, “%”) + 1
              NumFormatCharacter = Mid(ExistingNumberFormat, NumFormatPos, 1)
              ExistingListFormat = Replace(ExistingNumberFormat, “%” & NumFormatCharacter, ExistingNumberStyleExample)
              MsgBox ExistingListFormat

              Thanks as always for your help and patience!!
              Troy

    Viewing 1 reply thread
    Reply To: Help with Procedure (Word 2000 VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: