none
New to Programming: Syntax Error-unable to solve (Macro to format Word tables) Please help! RRS feed

  • Question

  • Sub ColorResults()



       ' Make PASS Blue

       ActiveDocument.Select

       Continue = True

       

       Do While Continue = True

       

           Selection.Find.ClearFormatting

           With Selection.Find

               .Text = "Pass"

               .Replacement.Text = ""

               .Forward = True

           End With

           Selection.Find.Execute

           

           Continue = Selection.Find.Found

           If Continue = True Then

              Selection.Expand wdParagraph

              Selection.Range.Font.Color = wdColorBlue

           End If

           

           Selection.Move Unit:=wdLine, Count:=1

       

       Loop



       ' Make PARTIAL PASS Green

       ActiveDocument.Select

       Continue = True

       

       Do While Continue = True

       

           Selection.Find.ClearFormatting

           With Selection.Find

               .Text = "Partial Pass"

               .Replacement.Text = ""

               .Forward = True

           End With

           Selection.Find.Execute

           

           Continue = Selection.Find.Found

           If Continue = True Then

              Selection.Expand wdParagraph

              Selection.Range.Font.Color = wdColorGreen

           End If

           

           Selection.Move Unit:=wdLine, Count:=1

       

       Loop



       ' Make FAIL Red

       ActiveDocument.Select

       Continue = True

       

       Do While Continue = True

       

           Selection.Find.ClearFormatting

           With Selection.Find

               .Text = "Fail"

               .Replacement.Text = ""

               .Forward = True

           End With

           Selection.Find.Execute

           

           Continue = Selection.Find.Found

           If Continue = True Then

              Selection.Expand wdParagraph

              Selection.Range.Font.Color = wdColorRed

           End If

           

           Selection.Move Unit:=wdLine, Count:=1

       

       Loop



       ' Remove @@@

       ActiveDocument.Select

       With Selection.Find

           .Text = "@@@"

           .Replacement.Text = ""

       End With

       Selection.Find.Execute Replace:=wdReplaceAll



    End Sub



    Sub BoldLabels()



       ' Make Test Description: bold

       ActiveDocument.Select

       Continue = True

       

       Do While Continue = True

       

           Selection.Find.ClearFormatting

           With Selection.Find

               .Text = "Test Description:"

               .Replacement.Text = ""

               .Forward = True

           End With

           Selection.Find.Execute

           

           Continue = Selection.Find.Found

           If Continue = True Then

              Selection.Expand wdParagraph

              Selection.Range.Bold = True

           End If

           

           Selection.Move Unit:=wdLine, Count:=1

       

       Loop



       ' Make Expected Test Results: bold

       ActiveDocument.Select

       Continue = True

       

       Do While Continue = True

       

           Selection.Find.ClearFormatting

           With Selection.Find

               .Text = "Expected Test Results:"

               .Replacement.Text = ""

               .Forward = True

           End With

           Selection.Find.Execute

           

           Continue = Selection.Find.Found

           If Continue = True Then
              Selection.Expand wdParagraph

              Selection.Range.Bold = True

           End If

           

           Selection.Move Unit:=wdLine, Count:=1

       

       Loop



       ' Make Test Results: bold

       ActiveDocument.Select

       Continue = True

       

       Do While Continue = True

       

           Selection.Find.ClearFormatting

           With Selection.Find

               .Text = "Test Results:"

               .Replacement.Text = ""

               .Forward = True

           End With

           Selection.Find.Execute

           

           Continue = Selection.Find.Found

           If Continue = True Then

              Selection.Expand wdParagraph

              Selection.Range.Bold = True

           End If

           

           Selection.Move Unit:=wdLine, Count:=1

       

       Loop



    End Sub



    Sub FormatTables()



        BoldLabels

        

    ' Format the tables

        ActiveDocument.Select

        Index = 1

        Continue = True

        

        Do While Continue = True

           Selection.Find.ClearFormatting

           With Selection.Find

               .Text = "REQID"

               .Replacement.Text = ""

               .Forward = True

           End With

           Selection.Find.Execute

           Do While Selection.Paragraphs(Index).Range.Characters.Count > 1

              unitsMoved = Selection.MoveDown(Unit:=wdLine, Count:=1, Extend:=wdExtend)

              If unitsMoved = 1 Then

                 Index = Index + 1

              End If

              Selection.Expand wdParagraph

              'MsgBox "Index = " + Str$(Index), , "Warning"

           Loop

           Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend

           Selection.Expand wdLine

           

           Index = 1

           Continue = Selection.Find.Found

           'DefListSep = getListSeparator()

           'setListSeparator ("=")

           
           If Continue = True Then

            
              Selection.ConvertToTable Separator:=wdSeparateByDefaultListSeparator_,

                 NumColumns:=2, NumRows:=1, Format:=wdTableFormatGrid8, ApplyBorders:=True _

                  , ApplyShading:=True, ApplyFont:=True, ApplyColor:=True, ApplyHeadingRows _

                  :=True, ApplyLastRow:=False, ApplyFirstColumn:=True, ApplyLastColumn:= _

                  False, AutoFit:=True, AutoFitBehavior:=wdAutoFitFixed

              Selection.Columns.PreferredWidthType = wdPreferredWidthPoints

              Selection.Columns.PreferredWidth = InchesToPoints(2)

              Selection.Collapse Direction:=wdCollapseEnd

           End If

       Loop

       

       setListSeparator (DefListSep)

       

    End Sub

    Sub ColorResults()
    
    
    
       ' Make PASS Blue
    
       ActiveDocument.Select
    
       Continue = True
    
       
    
       Do While Continue = True
    
       
    
           Selection.Find.ClearFormatting
    
           With Selection.Find
    
               .Text = "Pass"
    
               .Replacement.Text = ""
    
               .Forward = True
    
           End With
    
           Selection.Find.Execute
    
           
    
           Continue = Selection.Find.Found
    
           If Continue = True Then
    
              Selection.Expand wdParagraph
    
              Selection.Range.Font.Color = wdColorBlue
    
           End If
    
           
    
           Selection.Move Unit:=wdLine, Count:=1
    
       
    
       Loop
    
    
    
       ' Make PARTIAL PASS Green
    
       ActiveDocument.Select
    
       Continue = True
    
       
    
       Do While Continue = True
    
       
    
           Selection.Find.ClearFormatting
    
           With Selection.Find
    
               .Text = "Partial Pass"
    
               .Replacement.Text = ""
    
               .Forward = True
    
           End With
    
           Selection.Find.Execute
    
           
    
           Continue = Selection.Find.Found
    
           If Continue = True Then
    
              Selection.Expand wdParagraph
    
              Selection.Range.Font.Color = wdColorGreen
    
           End If
    
           
    
           Selection.Move Unit:=wdLine, Count:=1
    
       
    
       Loop
    
    
    
       ' Make FAIL Red
    
       ActiveDocument.Select
    
       Continue = True
    
       
    
       Do While Continue = True
    
       
    
           Selection.Find.ClearFormatting
    
           With Selection.Find
    
               .Text = "Fail"
    
               .Replacement.Text = ""
    
               .Forward = True
    
           End With
    
           Selection.Find.Execute
    
           
    
           Continue = Selection.Find.Found
    
           If Continue = True Then
    
              Selection.Expand wdParagraph
    
              Selection.Range.Font.Color = wdColorRed
    
           End If
    
           
    
           Selection.Move Unit:=wdLine, Count:=1
    
       
    
       Loop
    
    
    
       ' Remove @@@
    
       ActiveDocument.Select
    
       With Selection.Find
    
           .Text = "@@@"
    
           .Replacement.Text = ""
    
       End With
    
       Selection.Find.Execute Replace:=wdReplaceAll
    
    
    
    End Sub
    
    
    
    Sub BoldLabels()
    
    
    
       ' Make Test Description: bold
    
       ActiveDocument.Select
    
       Continue = True
    
       
    
       Do While Continue = True
    
       
    
           Selection.Find.ClearFormatting
    
           With Selection.Find
    
               .Text = "Test Description:"
    
               .Replacement.Text = ""
    
               .Forward = True
    
           End With
    
           Selection.Find.Execute
    
           
    
           Continue = Selection.Find.Found
    
           If Continue = True Then
    
              Selection.Expand wdParagraph
    
              Selection.Range.Bold = True
    
           End If
    
           
    
           Selection.Move Unit:=wdLine, Count:=1
    
       
    
       Loop
    
    
    
       ' Make Expected Test Results: bold
    
       ActiveDocument.Select
    
       Continue = True
    
       
    
       Do While Continue = True
    
       
    
           Selection.Find.ClearFormatting
    
           With Selection.Find
    
               .Text = "Expected Test Results:"
    
               .Replacement.Text = ""
    
               .Forward = True
    
           End With
    
           Selection.Find.Execute
    
           
    
           Continue = Selection.Find.Found
    
           If Continue = True Then
              Selection.Expand wdParagraph
    
              Selection.Range.Bold = True
    
           End If
    
           
    
           Selection.Move Unit:=wdLine, Count:=1
    
       
    
       Loop
    
    
    
       ' Make Test Results: bold
    
       ActiveDocument.Select
    
       Continue = True
    
       
    
       Do While Continue = True
    
       
    
           Selection.Find.ClearFormatting
    
           With Selection.Find
    
               .Text = "Test Results:"
    
               .Replacement.Text = ""
    
               .Forward = True
    
           End With
    
           Selection.Find.Execute
    
           
    
           Continue = Selection.Find.Found
    
           If Continue = True Then
    
              Selection.Expand wdParagraph
    
              Selection.Range.Bold = True
    
           End If
    
           
    
           Selection.Move Unit:=wdLine, Count:=1
    
       
    
       Loop
    
    
    
    End Sub
    
    
    
    Sub FormatTables()
    
    
    
        BoldLabels
    
        
    
    ' Format the tables
    
        ActiveDocument.Select
    
        Index = 1
    
        Continue = True
    
        
    
        Do While Continue = True
    
           Selection.Find.ClearFormatting
    
           With Selection.Find
    
               .Text = "REQID"
    
               .Replacement.Text = ""
    
               .Forward = True
    
           End With
    
           Selection.Find.Execute
    
           Do While Selection.Paragraphs(Index).Range.Characters.Count > 1
    
              unitsMoved = Selection.MoveDown(Unit:=wdLine, Count:=1, Extend:=wdExtend)
    
              If unitsMoved = 1 Then
    
                 Index = Index + 1
    
              End If
    
              Selection.Expand wdParagraph
    
              'MsgBox "Index = " + Str$(Index), , "Warning"
    
           Loop
    
           Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
    
           Selection.Expand wdLine
    
           
    
           Index = 1
    
           Continue = Selection.Find.Found
    
           'DefListSep = getListSeparator()
    
           'setListSeparator ("=")
    
           
           If Continue = True Then
    
            
              Selection.ConvertToTable Separator:=wdSeparateByDefaultListSeparator_,
    
                 NumColumns:=2, NumRows:=1, Format:=wdTableFormatGrid8, ApplyBorders:=True _
    
                  , ApplyShading:=True, ApplyFont:=True, ApplyColor:=True, ApplyHeadingRows _
    
                  :=True, ApplyLastRow:=False, ApplyFirstColumn:=True, ApplyLastColumn:= _
    
                  False, AutoFit:=True, AutoFitBehavior:=wdAutoFitFixed
    
              Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
    
              Selection.Columns.PreferredWidth = InchesToPoints(2)
    
              Selection.Collapse Direction:=wdCollapseEnd
    
           End If
    
       Loop
    
       
    
       setListSeparator (DefListSep)
    
       
    
    End Sub
    
    

    Thursday, August 17, 2017 8:33 PM

All replies

  • Hi Believe2017,

    Can you provide the entire code sample that can reproduce your issue now, or you can point out where you get this problem here. It is beneficial to us to analyze you issue. Please note the format of the code

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 18, 2017 8:50 AM
  • Hello, 

    This forum is for VB in Visual Studio. Your VB in the Macro is done in the VBA forum.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=isvvba


    Success
    Cor

    Friday, August 18, 2017 10:17 AM
  • Hi Believe2017 -

    Holy Moly, That's a Lot of code lines... but this may help move one step forward:

    Towards the end, your line has the continuation character Underscore and then a comma. This is turned around. it should be comma and then underscore. This is in line

       If Continue = True Then        
              Selection.ConvertToTable Separator:=wdSeparateByDefaultListSeparator_,


    The correct syntax

       If Continue = True Then
              Selection.ConvertToTable Separator:=wdSeparateByDefaultListSeparator, _

    If you post the actual error message with some statement of the problem, it really helps get to an answer for you!

    EDIT:

    I don't kow if the invervening lines are an artifact of pasting your code into the question, but those don't work, either, Here's the full line that will pass muster:

              Selection.ConvertToTable Separator:=wdSeparateByDefaultListSeparator, _
                 NumColumns:=2, NumRows:=1, Format:=wdTableFormatGrid8, ApplyBorders:=True, _
                  ApplyShading:=True, ApplyFont:=True, ApplyColor:=True, ApplyHeadingRows:=True, _
                  ApplyLastRow:=False, ApplyFirstColumn:=True, ApplyLastColumn:= _
                  False, AutoFit:=True, AutoFitBehavior:=wdAutoFitFixed
    EDIT 2: Finally, your line setListSeparator (DefListSep) isn't a method or function native to Word. Not sure what you are trying to do. I've never had to be explicit with declaring List Separators.

    -J


    -MainSleuth You've Got It, Use It! Engineering, Science, Statistics Solutions http://ToolSleuth.com. For any reply that either helps to answer your question or is the answer, please mark it as helpful or as the answer so others with the same question will have an answer quickly.



    • Edited by MainSleuth Friday, August 18, 2017 1:12 PM
    Friday, August 18, 2017 1:01 PM
  • Thank you,

    Unfortunately dealt with sudden death of loved one -apology in delayed response

    error = compile error: sub or function not defined

    it points to setListSeparator(DefListSep)

    it should separate what's on right side of '=' sign and place in adjacent column

    REQID=Testxxx3589478

     

    Testtype=1

     

    ThisTest=Ready

     

    This1=Set

     

    AndThen=Go

     

    Desired table output:  

    REQID

    Testxxx3589478

    Testtype

    1

    ThisTest

    Ready

    This1

    Set

    AndThen

    Go


    Tuesday, August 22, 2017 5:02 PM
  • Thank you,

    Unfortunately dealt with sudden death of loved one -apology in delayed response

    error = compile error: sub or function not defined

    it points to setListSeparator(DefListSep)

    it should separate what's on right side of '=' sign and place in adjacent column

    REQID=Testxxx3589478

     

    Testtype=1

     

    ThisTest=Ready

     

    This1=Set

     

    AndThen=Go

     

    Desired table output below:  

    REQID

    Testxxx3589478

    Testtype

    1

    ThisTest

    Ready

    This1

    Set

    AndThen

    Go

    Tuesday, August 22, 2017 5:05 PM
  • Thank you!
    Tuesday, August 22, 2017 5:05 PM
  • Delay understood - sorry to hear of that.

    If you see Edit 2, on the previous response, I was wondering what you were trying to accomplish w/ setListSeparator. You should comment out the offending line and define the "=" as your separator. Then the end of your routine looks like:

          If Continue = True Then
            
            Selection.ConvertToTable Separator:="=", _
                 NumColumns:=2, NumRows:=1, Format:=wdTableFormatGrid8, ApplyBorders:=True, _
                  ApplyShading:=True, ApplyFont:=True, ApplyColor:=True, ApplyHeadingRows:=True, _
                  ApplyLastRow:=False, ApplyFirstColumn:=True, ApplyLastColumn:= _
                  False, AutoFit:=True, AutoFitBehavior:=wdAutoFitFixed
              Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
              Selection.Columns.PreferredWidth = InchesToPoints(2)
              Selection.Collapse Direction:=wdCollapseEnd
    
           End If
       Loop
       
       'setListSeparator (DefListSep)
    
    End Sub
    


    -MainSleuth You've Got It, Use It! Engineering, Science, Statistics Solutions http://ToolSleuth.com. For any reply that either helps to answer your question or is the answer, please mark it as helpful or as the answer so others with the same question will have an answer quickly.

    Thursday, August 24, 2017 4:52 PM