View Full Version : Setting A Word Range With VBScript


dethomas143@charter.net
02-17-2006, 03:53 AM
I'm trying to create a vbscript that among other things adds some text
to an existing Word document in a specific place within the document.
I calculate the place to enter the text by the numer of characters from
the start of the document.

After working with the word documentation, it seems that what I want to
do is set the appropriate range within the document based on the number
of characters and then to an InsertAfter.

The problem is I am unable to set either the start or end of the range.
The documentation uses syntax like this: ActiveDocument.Range (Start
:= 0, End := intNumChars)

VBscript will not use this syntax, however. Aside from the
parentheses, it does not understand the := In VBScript = is used for
assignment and I can use something like
this: ActiveDocument.Range.End = intNumChars

I get no error when I run this, but the value does not get set. If I
check the value of Range.End immediately after executing this statement
it is the total length of the document. I've verified that intNumChars
has the value I expect it to have.

I'm hoping someone can tell me how I can set the start and end values
of the range using vbscript.

Thanks In Advance,

David E. Thomas

Jonathan West
02-17-2006, 12:34 PM
Hi David,

The key to this is understanding the difference between VBScript and VBA. In
this context the key point is that VBScript does not support named
arguments.

Named arguments are ones where you assign the value to the argument using
the := operator. However, even in VBA, you don't need to use named
arguments. In VBA, if you want to insert some text after the 30th character,
you can use either of these lines of code

ActiveDocument.Range(Start:=30, End:=30).InsertAfter Text:="My text to
add"
ActiveDocument.Range(30, 30).InsertAfter "My text to add"

VBScript can only use the second approach. Since you will also probably be
setting an object variable to the Word application, you will also need to
qualify the ActiveDocument object something like this

objWord.ActiveDocument.Range(30, 30).InsertAfter "My text to add"

Where objWord is the object variable you have assigned to the Word
application.

In VBA, where a method has optional arguments, you can simply name the ones
you want, and omit the rest, like this.

ActiveDocument.Printout Range:=wdPrintCurrentPage

The line of code above has omitted the first two arguments (Background and
Append). To do the same thing without naming the argument, you have to find
some way of indicating that the argument you are specifying is the third
argument in the list. You can achieve that by using commas, like this.

ActiveDocument.Printout , , wdPrintCurrentPage

and in VbScript like this

objWord.ActiveDocument.Printout , , 2

You put in a comma for each parameter you are missing that is in front of
the one you are using. In the example above, two parameters have been
missed, hence two commas. You don't need to bother adding commas for further
missing parameters after the last one you use.

In the VBScript, I have used the value (2) instead of the constant
wdPrintCurrentPage, as VBScript doesn't recognise VBA constant names.

The order of arguments for any Word VBA method is given in the Help file
entry of the method in the Word VBA Help file.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org




<dethomas143@charter.net> wrote in message
news:1140148409.075548.9490@z14g2000cwz.googlegroups.com...
> I'm trying to create a vbscript that among other things adds some text
> to an existing Word document in a specific place within the document.
> I calculate the place to enter the text by the numer of characters from
> the start of the document.
>
> After working with the word documentation, it seems that what I want to
> do is set the appropriate range within the document based on the number
> of characters and then to an InsertAfter.
>
> The problem is I am unable to set either the start or end of the range.
> The documentation uses syntax like this: ActiveDocument.Range (Start
> := 0, End := intNumChars)
>
> VBscript will not use this syntax, however. Aside from the
> parentheses, it does not understand the := In VBScript = is used for
> assignment and I can use something like
> this: ActiveDocument.Range.End = intNumChars
>
> I get no error when I run this, but the value does not get set. If I
> check the value of Range.End immediately after executing this statement
> it is the total length of the document. I've verified that intNumChars
> has the value I expect it to have.
>
> I'm hoping someone can tell me how I can set the start and end values
> of the range using vbscript.
>
> Thanks In Advance,
>
> David E. Thomas
>

dethomas143@charter.net
02-17-2006, 03:02 PM
Jonathan West wrote:

Thank you for the quick and informative response....

I was trying the ActiveDocument.Range (30, 30) syntax which ran without
error but then afterward I checked the range and it applied to the
entired document.

What I was not doing was the .instertafter on the same line. Based on
your response I'm inferring that setting the range worked but only for
the current statement. I'll give your suggestion a try ASAP.

I did finally find another way to solve the problem by creating a range
object and setting it to the activedocument.range and then doing a
..start = and a .end = on the new range and then calling .insertafter on
that range. That worked but I like your way better.

Thanks again.

David E. Thomas

> Hi David,
>
> The key to this is understanding the difference between VBScript and VBA. In
> this context the key point is that VBScript does not support named
> arguments.
>
> Named arguments are ones where you assign the value to the argument using
> the := operator. However, even in VBA, you don't need to use named
> arguments. In VBA, if you want to insert some text after the 30th character,
> you can use either of these lines of code
>
> ActiveDocument.Range(Start:=30, End:=30).InsertAfter Text:="My text to
> add"
> ActiveDocument.Range(30, 30).InsertAfter "My text to add"
>
> VBScript can only use the second approach. Since you will also probably be
> setting an object variable to the Word application, you will also need to
> qualify the ActiveDocument object something like this
>
> objWord.ActiveDocument.Range(30, 30).InsertAfter "My text to add"
>
> Where objWord is the object variable you have assigned to the Word
> application.
>
> In VBA, where a method has optional arguments, you can simply name the ones
> you want, and omit the rest, like this.
>
> ActiveDocument.Printout Range:=wdPrintCurrentPage
>
> The line of code above has omitted the first two arguments (Background and
> Append). To do the same thing without naming the argument, you have to find
> some way of indicating that the argument you are specifying is the third
> argument in the list. You can achieve that by using commas, like this.
>
> ActiveDocument.Printout , , wdPrintCurrentPage
>
> and in VbScript like this
>
> objWord.ActiveDocument.Printout , , 2
>
> You put in a comma for each parameter you are missing that is in front of
> the one you are using. In the example above, two parameters have been
> missed, hence two commas. You don't need to bother adding commas for further
> missing parameters after the last one you use.
>
> In the VBScript, I have used the value (2) instead of the constant
> wdPrintCurrentPage, as VBScript doesn't recognise VBA constant names.
>
> The order of arguments for any Word VBA method is given in the Help file
> entry of the method in the Word VBA Help file.
>
>
> --
> Regards
> Jonathan West - Word MVP
> www.intelligentdocuments.co.uk
> Please reply to the newsgroup
> Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
>
>

Jonathan West
02-17-2006, 04:58 PM
Hi David,

yes, I neglected to point out that you had also gotten a bit mixed up
between the Range method of a Document object (which uses the parentheses)
and a Range object, which doesn't.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org

<dethomas143@charter.net> wrote in message
news:1140185253.033550.290780@g14g2000cwa.googlegroups.com...
>
> Jonathan West wrote:
>
> Thank you for the quick and informative response....
>
> I was trying the ActiveDocument.Range (30, 30) syntax which ran without
> error but then afterward I checked the range and it applied to the
> entired document.
>
> What I was not doing was the .instertafter on the same line. Based on
> your response I'm inferring that setting the range worked but only for
> the current statement. I'll give your suggestion a try ASAP.
>
> I did finally find another way to solve the problem by creating a range
> object and setting it to the activedocument.range and then doing a
> .start = and a .end = on the new range and then calling .insertafter on
> that range. That worked but I like your way better.
>
> Thanks again.
>
> David E. Thomas
>
>> Hi David,
>>
>> The key to this is understanding the difference between VBScript and VBA.
>> In
>> this context the key point is that VBScript does not support named
>> arguments.
>>
>> Named arguments are ones where you assign the value to the argument using
>> the := operator. However, even in VBA, you don't need to use named
>> arguments. In VBA, if you want to insert some text after the 30th
>> character,
>> you can use either of these lines of code
>>
>> ActiveDocument.Range(Start:=30, End:=30).InsertAfter Text:="My text
>> to
>> add"
>> ActiveDocument.Range(30, 30).InsertAfter "My text to add"
>>
>> VBScript can only use the second approach. Since you will also probably
>> be
>> setting an object variable to the Word application, you will also need to
>> qualify the ActiveDocument object something like this
>>
>> objWord.ActiveDocument.Range(30, 30).InsertAfter "My text to add"
>>
>> Where objWord is the object variable you have assigned to the Word
>> application.
>>
>> In VBA, where a method has optional arguments, you can simply name the
>> ones
>> you want, and omit the rest, like this.
>>
>> ActiveDocument.Printout Range:=wdPrintCurrentPage
>>
>> The line of code above has omitted the first two arguments (Background
>> and
>> Append). To do the same thing without naming the argument, you have to
>> find
>> some way of indicating that the argument you are specifying is the third
>> argument in the list. You can achieve that by using commas, like this.
>>
>> ActiveDocument.Printout , , wdPrintCurrentPage
>>
>> and in VbScript like this
>>
>> objWord.ActiveDocument.Printout , , 2
>>
>> You put in a comma for each parameter you are missing that is in front of
>> the one you are using. In the example above, two parameters have been
>> missed, hence two commas. You don't need to bother adding commas for
>> further
>> missing parameters after the last one you use.
>>
>> In the VBScript, I have used the value (2) instead of the constant
>> wdPrintCurrentPage, as VBScript doesn't recognise VBA constant names.
>>
>> The order of arguments for any Word VBA method is given in the Help file
>> entry of the method in the Word VBA Help file.
>>
>>
>> --
>> Regards
>> Jonathan West - Word MVP
>> www.intelligentdocuments.co.uk
>> Please reply to the newsgroup
>> Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
>>
>>
>