-1

I'm trying to get Word to fill in cells in a table. The script works when run as a macro from within Word, but fails when saved as a .vbs file and double-clicked, or run with wscript. This is a part of it.

set obj = GetObject(,"Word.Application)
With obj
    With .Selection
        MsgBox .text
        If (.Information(wdWithInTable) = True) Then

            .Collapse Direction:=wdCollapseStart
            tCols = .Tables(1).Columns.Count
            tRow = .Information(wdStartOfRangeRowNumber)
            tCol = .Information(wdStartOfRangeColumnNumber)

            For I = 2 To 5
                .Tables(1).Cell(tRow, I).Range.Text = "fred" & Str(I)
            Next

            ` now make new row
            For I = 1 To tCols - tCol + 1
                .MoveRight unit:=wdCell
            Next

        End If
    End With
End With

I have three problems. First, it won't compile unless I comment out the .Collapse and .MoveRight lines. Second, although the MsgBox .text displays the selected text, I get "out of range" errors if I try to access any .Information property.

I'm sure I'm missing something very simple: I usually write software for Macs, and I'd do this using AppleScript. This is my first attempt at getting anything done under Windows.

user692942
  • 16,398
  • 7
  • 76
  • 175
  • 2
    You [cannot use constants](https://stackoverflow.com/a/14122422/11683) such as `wdCollapseStart` in VBScript. Put Option Explicit on top and it will correctly stop compiling. Replace them with their literal value (e.g. `wdWithInTable` => `12`), or declare them as `Const`s with correct values in the beginning of your script. – GSerg Oct 21 '18 at 12:27
  • 1
    Possible duplicate of [Convert VBA code to Vbscript](https://stackoverflow.com/questions/25927864/convert-vba-code-to-vbscript) – user692942 Oct 21 '18 at 16:22
  • You'd think with the age of VBScript now that this wouldn't be the first time this has come up on [so] and you'd be right. – user692942 Oct 21 '18 at 16:23
  • [Yet another example](https://stackoverflow.com/a/29557401/692942) – user692942 Oct 21 '18 at 16:26
  • [Still another](https://stackoverflow.com/q/14064069/692942) guess it should be expected. – user692942 Oct 21 '18 at 16:28
  • Also, there is a quite clear typo in the first-line of the code, should be `Set obj = GetObject(,"Word.Application")`. – user692942 Oct 21 '18 at 16:31

2 Answers2

0

VBScript and VBA are different languages.

They are a bit similar, but not very. Moreover, VBScript is not like AppleScript; it doesn't let you easily interface with running programs.

The interfaces you'll get from VBScript can behave subtly differently in VBA and VBScript. However, I think you've got two problems here:

  • := is invalid syntax in VBScript; you'll need to find an alternative way of calling the function. Try just using positional arguments.
  • You've no guarantee that this will open the expected file; there could be another instance of Word that it's interacting with instead.
wizzwizz4
  • 6,140
  • 2
  • 26
  • 62
0

Since your code is not running within the Word environment it would require a reference to the Word object library in order to use enumeration constants (those things that start with wd).

VBScript, however, cannot work with references, which means the only possibility is to use the long value equivalents of the enumerations. You'll find these in the Word Language References. Simplest to use is probably the Object Browser in Word's VBA Editor. (In Word: Alt+F11 to open the VBA Editor; F2 to start the Object Browser; type in the term in the "Search" box, click on the term, then look in the bottom bar.)

The code in the question uses, for example:

wdWithInTable
wdCollapseStart
wdStartOfRangeRowNumber
wdStartOfRangeColumnNumber
wdCell

The reason you get various kinds of errors depends on where these are used.

Also, VBScript can't used named parameters such as Unit:=. Any parameters must be passed in comma-delimited format, if there's more than one, in the order specified by the method or property. If there are optional parameters you don't want to use these should be left "blank":

MethodName parameter, parameter, , , parameter
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43