Link to home
Start Free TrialLog in
Avatar of WaterStreet
WaterStreetFlag for United States of America

asked on

Two MS Word macro syntax errors in Office 2013 under Win 8

I'm using MS Office Home and Student 2013 under a new computer running Win 8. I need help regarding two Word macro syntex errors - see the attached picture and the exact code below:


Can someone show how to correct the syntax, give some workaround or tell me what I need to do to obtain the same result in the macro?

=================================================================
            If WordNum > maxwords - 1 Then
                j = MsgBox("The maximum array size has been exceeded. _
                  Increase maxwords.", vbOKOnly)
                Exit For
            End If
        End If

        For l = j + 1 To WordNum
            If (Not ByFreq And Words(l) < Words(k)) Or
               (ByFreq And Freq(l) > Freq(k)) Then k = l
        Next l
  ==================================================================  
 
two-syntax-errors-in-red-3.jpg

  Thanks
SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WaterStreet

ASKER

I hope to try that later today.

Thanks.
GrahamSkan

Didn't work.

I believe  made all the suggested changes, but I still get the Syntax error in the same two lines of code.  They show as red text in the edit window, so I made the in bold text below:

Here is the entire macro that I'm trying to use in MS Word under Office Home and Student 2013:

Sub WxxdFrequency()
    Dim SingleWxxd As String           'Raw Wxxd pulled from doc
    Const maxWxxds = 200000            'Maximum unique Wxxds allowed
    Dim Wxxds(maxWxxds) As String      'Array to hold unique Wxxds
    Dim Freq(maxWxxds) As Integer      'Frequency counter for unique Wxxds
    Dim WxxdNum As Integer             'Number of unique Wxxds
    Dim ByFreq As Boolean              'Flag for sorting order
    Dim ttlwxxds As Long                 'Total Wxxds in the document
    Dim Excludes As String             'Wxxds to be excluded
    Dim Found As Boolean               'Temporary flag
    Dim j As Integer                   'Temporary variables
    Dim k As Integer                   '
    Dim l As Integer                   '
    Dim Temp As Integer                '
    Dim tWxxd As String                '

    ' Set up excluded Wxxds
    Excludes = "[the][a][of][is][to][for][this][that][by][be][and][are]"

    ' Find out how to sort
    ByFreq = True
    ans = InputBox$("Sort by Wxxd or by FREQ?", "Sort order", "Wxxd")
    If ans = "" Then End
    If UCase(ans) = "Wxxd" Then
        ByFreq = False
    End If
   
    Selection.HomeKey Unit:=wxxdstory
    System.Cursor = wdCursorWait
    WxxdNum = 0
    ttlwxxds = ActiveDocument.Wxxds.Count

    ' Control the repeat
    For Each aWxxd In ActiveDocument.Wxxds

Open in new window

       SingleWxxd = Trim(LCase(aWxxd))
        If SingleWxxd < "a" Or SingleWxxd > "z" Then SingleWxxd = ""    'Out of range?
        If InStr(Excludes, "[" & SingleWxxd & "]") Then SingleWxxd = "" 'On exclude list?
        If Len(SingleWxxd) > 0 Then
            Found = False
            For j = 1 To WxxdNum
                If Wxxds(j) = SingleWxxd Then
                    Freq(j) = Freq(j) + 1
                    Found = True
                    Exit For
                End If
            Next j
            If Not Found Then
                WxxdNum = WxxdNum + 1
                Wxxds(WxxdNum) = SingleWxxd
                Freq(WxxdNum) = 1
            End If
            If WxxdNum > maxWxxds - 1 Then
                j = MsgBox("The maximum array size has been exceeded. _
                  Increase maxWxxds.", vbOKOnly)

                Exit For
            End If
        End If
        ttlwxxds = ttlwxxds - 1
        StatusBar = "Remaining: " & ttlwxxds & "     Unique: " & WxxdNum
    Next aWxxd

    ' Now sort it into word order
    For j = 1 To WxxdNum - 1
        k = j
        For l = j + 1 To WxxdNum
            If (Not ByFreq And Wxxds(l) < Wxxds(k)) Or
               (ByFreq And Freq(l) > Freq(k)) Then k = l

        Next l
        If k <> j Then
            wxxd = Wxxds(j)
            Wxxds(j) = Wxxds(k)
            Wxxds(k) = wxxd
            Temp = Freq(j)
            Freq(j) = Freq(k)
            Freq(k) = Temp
        End If
        StatusBar = "Sorting: " & WxxdNum - j
    Next j

    ' Now write out the results
    tmpName = ActiveDocument.AttachedTemplate.FullName
    Documents.Add Template:=tmpName, NewTemplate:=False
    Selection.ParagraphFormat.TabStops.ClearAll
    With Selection
        For j = 1 To WxxdNum
            .TypeText Text:=Trim(Str(Freq(j))) & vbTab & Wxxds(j) & vbCrLf
        Next j
    End With
    System.Cursor = wdCursorNormal
    j = MsgBox("There were " & Trim(Str(WxxdNum)) & _
        " different Wxxds ", vbOKOnly, "Finished")
End Sub
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Graham.
I hope to work with that later today.
I got side tracked. I hope to try this later today.
I didn't catch the syntax difference in the code you gave me in your first posting, until you explained it in your final posting; so, I awarded both equally, with the best answer being the explanation.

Thanks
However, I then got a Run time error '4120'

Bad parameter in the following code line

 Selection.HomeKey Unit:=wxxdstory
 
 
But never mind. I'm awarding this question because you answered the one I originally asked, which cured the syntax errors that in the two pieces of code.

I abandoned this macro and have successfully used the one at
http://www.authorsden.com/categories/article_top.asp?catid=20&id=39307

It has an "excludes" feature which I have extended for my own use.


Thanks a lot.