0

I am having an error when compiling VBScript that will delete a specific line in RTF file. The error points to Line 6, Char 25 which is '=' sign in "Selection.GoTo What:=wdGoToBookmark" statement but I think the syntax is correct.


Error Message

Line: 6 Char: 25

Error: Expected statement

Code: 800A0400

Source: Microsoft VBScript compilation error

Set Word = CreateObject("Word.Application")
Set wordfile = Word.Documents.Open("filename.RTF")
Word.Visible = TRUE

Sub Macro1()
    Selection.GoTo What:=wdGoToBookmark, Name:="IDX12"
    With ActiveDocument.Bookmarks
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    Selection.MoveUp Unit:=wdLine, Count:=2
    Selection.Delete Unit:=wdCharacter, Count:=1
    ActiveDocument.Save
End Sub


Macro1
Word.Quit
  • Possible duplicate of [Does VBScript allow named arguments in function calls?](https://stackoverflow.com/questions/42194300/does-vbscript-allow-named-arguments-in-function-calls) – Geert Bellekens Sep 02 '19 at 12:57
  • 1
    Vbscript doesn't know named arguments, and it also doesn't know Selection and ActiveDocument. You'll have a bit more work to convert this VBA macro to VBScript. – Geert Bellekens Sep 02 '19 at 12:57

1 Answers1

0

I see 3 main issues in your script.

  • Unfortunately, VBScript does not support named arguments (whereas VBA does). Thus, you must list all the null arguments of the methods
  • You use some objects (Selection, ActiveDocument ...) as if they were available as it is. They aren't : they are properties of the Word.Application object.
  • Last issue : the constants you use are also unknown of vbs. So either you declare them or you use their Word internal value.

    Set Word = CreateObject("Word.Application")
    ' !! You also might meet a problem here because you don't use the file full path. The file may not be found !!
    Set wordfile = Word.Documents.Open("filename.RTF")
    Word.Visible = TRUE

    Sub Macro1()
        'Word.Selection.GoTo What:=wdGoToBookmark, Name:="IDX12"
        Word.Selection.GoTo -1, , , "IDX12"
        With Word.ActiveDocument.Bookmarks
            '.DefaultSorting = wdSortByName
            .DefaultSorting = 0
            .ShowHidden = False
        End With
        'Word.Selection.MoveUp Unit:=wdLine, Count:=2
        Word.Selection.MoveUp 5, 2
        'Word.Selection.Delete Unit:=wdCharacter, Count:=1
        Word.Selection.Delete 1, 1
        Word.ActiveDocument.Save
    End Sub

    Macro1
    Word.Quit
Olivier Depriester
  • 1,615
  • 1
  • 7
  • 20
  • Hi Oliver. This works pretty well. I had no idea about the difference in VBA and VBS syntax. Thanks for listing all the issues not just providing the solution code. Very much appreciated :) – Leo Julongbayan Sep 02 '19 at 13:21