Help With Writing A Word Macro

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fahadqureshi
    New Member
    • Oct 2007
    • 28

    Help With Writing A Word Macro

    I am trying to write a word macro that scans the documents and highlights all the numbers that are less then .95
    is this possible to do using a word macro?

    i guess i really only need the command that selects a range of numbers.
    like if number < .95 or something like that. i am not really a vba person and am used to writing only c++ and python scripts.
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by fahadqureshi
    I am trying to write a word macro that scans the documents and highlights all the numbers that are less then .95
    is this possible to do using a word macro?

    i guess i really only need the command that selects a range of numbers.
    like if number < .95 or something like that. i am not really a vba person and am used to writing only c++ and python scripts.
    i dont know if there's an easy method, but with a DO and a couple of IF it can be done. Something like:

    [CODE=vb]Sub prueba()
    Dim i As Double
    Dim j As Integer
    Dim Dou1 As Double
    Selection.Start = 0
    Selection.End = 0
    For i = 1 To ActiveDocument. Characters.Coun t
    j = Asc(ActiveDocum ent.Characters( i))
    If (j >= 48 And j <= 57) Or j = 46 Then
    If Selection.End < i - 1 Then
    Selection.Start = i - 1
    Selection.End = i
    Else
    Selection.End = i
    End If
    Else
    If Selection.End = i - 1 Then
    Dou1 = Selection.Text
    If Dou1 < 0.95 Then
    Selection.Font. Bold = True
    End If
    End If
    End If
    Next
    End Sub[/CODE]

    HTH

    Comment

    • fahadqureshi
      New Member
      • Oct 2007
      • 28

      #3
      i am getting a type mismatch at the
      Code:
      Dou1 = Selection.Text
      The file does contain letters and numbers and different characters in it. Could there be a mismatch because i, j, Dou1 are all defined as numbers. Also, i am sure there is a good reason for it, but why is this line in the code and what does it do:

      Code:
      If (j >= 48 And j <= 57) Or j = 46 Then

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Originally posted by fahadqureshi
        i am getting a type mismatch at the
        Code:
        Dou1 = Selection.Text
        The file does contain letters and numbers and different characters in it. Could there be a mismatch because i, j, Dou1 are all defined as numbers. Also, i am sure there is a good reason for it, but why is this line in the code and what does it do:

        Code:
        If (j >= 48 And j <= 57) Or j = 46 Then
        actually the error was because there are periods without numbers. because it searches for numbers an dots, and if it finds a dot without a number and try it as a number it will show you the error. so all you have to do is in the line above the error, change the IF for this one

        If Selection.End = i - 1 And (Selection.End - Selection.Start > 1) Then

        That will check if the selected ranges are bigger than one... (this will fix this problem, but if you use suspensive, it will give you an error again because you'll have "..." that is larger than one but still no number).

        what im doing is using an integer "j" to store the ascii code of each character

        the ascii code for 0 is 48 , the code for 9 is 57 and all the numbers are between them. 46 is for a dot.

        HTH

        Comment

        • fahadqureshi
          New Member
          • Oct 2007
          • 28

          #5
          ohhhh ok. well in that case i am still getting an error because i have many ....... in a row in various places.

          this is what my document looks like and i want to highlight the number in the middle that are less then .95000 aditionally i want to highlight the number to the right of it and the numbers and text to the left of it as well but that might be too complicated.

          42062 ALTA_L98 69.000 0.95212 0.98269
          42820 HS_TAP69 69.000 0.93047 0.97650
          44400 KARSTN 9 69.000 0.92176 0.97748
          42080 ALVIN 9 69.000 0.92959 0.97614
          44380 JULIFF 69.000 0.92045 0.98942
          44440 MANVEL 9 69.000 0.92236 0.97481

          my document also contains all kinds of different characters and symbols like:
          -, <, >, $, :, &, etc.

          Comment

          • kadghar
            Recognized Expert Top Contributor
            • Apr 2007
            • 1302

            #6
            Originally posted by fahadqureshi
            ohhhh ok. well in that case i am still getting an error because i have many ....... in a row in various places.

            ...

            my document also contains all kinds of different characters and symbols like:
            -, <, >, $, :, &, etc.
            Then i think the easiest way to make it work will be making it ignore this errors, since "....." cant be compared, it will never highlight so just ask vb not to tell you that it isnt a number. Add this line before the FOR starts:

            on error resume next

            Now for the other numbers you wanted to hightlight, give it a try by yourself using selection.start and selection.end

            HTH

            Comment

            Working...