Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Run a string as code

13 views
Skip to first unread message

Luciano

unread,
Mar 20, 2003, 5:54:58 AM3/20/03
to
Does anyone know how to run/execute a string in VBA? More
specifically, I want to parse an external text file and run each line
as VBA code. I'd need an "eval" or "execute" instruction, but VBA
(Word 97) doesn't have that. Even more specifically, I have sort of a
glossary list turned into a list of calls to a function:
myRunFunction = func_replace("find this", "replace with that",
"NoCase", "WholeWord", "NoWild")
myRunFunction = func_replace("now find this", "and replace with
that", "NoCase", "WholeWord", "NoWild")
etc...

Anyone has any idea? Calling the function via COM with a VBS
script would not be ideal, but acceptable.

Thank you very much,
Luciano ES

Peter Hewett

unread,
Mar 20, 2003, 6:53:45 AM3/20/03
to
Luciano

You can't directly do what you want. There's no mechanism to say "execute
this string". However, like all problems you may be able to get around it.

If each line of your text file contains simple data, like:
search/replace/qualifiers. You can read the file and parse the data and
then pass the parsed data to the appropriate predefined function to
process. As long as you can predefine all the necessary procedures you can
get this method to work.

If it's more complex you may be able to parse your input and generate your
own VBA code which you can then execute (simplified version of the
techniques VBA viruses use).

I hope this helps + Cheers - Peter


Luciano <luci...@gmx.net> wrote in news:Xns93444CB49BB6Dlucianoavgmxnet@
207.46.248.16:

Luciano

unread,
Mar 20, 2003, 7:40:45 AM3/20/03
to
You're not the first eprson to suggest that: parse and pass. But how
am I supposed to parse lines in a language that does not support
RegeEx, has no Split function or any decent parsing tools at all?

Thanks for your reply,
Luciano ES

********************************
Peter Hewett <Nos...@xtra.co.nz> wrote in
news:Xns9344F2E2...@207.46.248.16:

Peter Hewett

unread,
Mar 20, 2003, 8:18:25 AM3/20/03
to
Luciano

If you'rw using Word 2000 or later you can use the following:

Split
Join
StrReverse
Left$
Right$
Mid$
Len
Instr
InStrRev
Filter
Replace

It's a little crude but you should be able to put something together to
tokenise the input and the pull out the tokens you want.

If that does not meet your needs you can always Shell to the Win32 verion
of Perl.

Godd luck + Cheers - Peter


Luciano <luci...@gmx.net> wrote in news:Xns93445EA43E8Elucianoavgmxnet@
207.46.248.16:

Luciano

unread,
Mar 20, 2003, 10:03:04 AM3/20/03
to
No, I am using Word 97. I said that.
You say I can Shell to the Win32 version of Perl. Could you give a
few more details? You see, my entire problem is getting text from an
external source and passing it to a function that resides in a Word
module. Once I Shell to Perl, how am I going to get Perl's output
back into Word and into my function? If I can do that, I don't even
need Perl in the first place. I think I don't really follow you, so
please be a little clearer if possible.

Thank you for all the help.

Best regards,

Luciano ES

********************************
Peter Hewett <Nos...@xtra.co.nz> wrote in

news:Xns9345D18...@207.46.248.16:

> If you'rw using Word 2000 or later you can use the following:
> Split

> Join...
> [snip]

Peter Hewett

unread,
Mar 20, 2003, 11:34:54 PM3/20/03
to
Luciano

If you're processing functions already exist in VBA then your going to have
to bite the bullet and parse the data using the limited features Word
offers. I only suggested Perl because you seemed to want something more
flexible.

If you check the VBA online help you'll see exactly what string handling
functions are available for Word97. I can't give you this info as I do not
currently have Word97 installed.

Cheers - Peter


Luciano <luci...@gmx.net> wrote in news:Xns934476C488156lucianoavgmxnet@
207.46.248.16:

Luciano

unread,
Mar 21, 2003, 5:08:24 AM3/21/03
to
Sigh... :-(

Thanks for the help, at any rate.

Luciano ES

********************************
Peter Hewett <Nos...@xtra.co.nz> wrote in

news:Xns9345A87A...@207.46.248.16:

Peter Hewett

unread,
Mar 21, 2003, 9:35:20 AM3/21/03
to
Luciano

I've found some "old" code that was written to parse stuff, it should do most
of what you want. At least it's better than nothing and having to write it
yourself. It's a bas module so you can import it into your project. Because
of the way posting wraps text and buggers up code i've emailed it to you. If
your email address is not valid, post your valid email address and I'll try
again.

BTW It was written 6 years ago so it'll work with Word 97!

I hope this helps + Cheers - Peter


Luciano <luci...@gmx.net> wrote in news:Xns934544D01595Blucianoavgmxnet@
207.46.248.16:

Luciano

unread,
Mar 21, 2003, 12:57:33 PM3/21/03
to
Thank you, Peter. I received the file. That's very kind of you.
I'll certainly study that because it has some very useful functions,
but I've almost found a good solution:
I took the function from the Word module and brought it entirely
to the VBS script. Everything seems to work except the last line,
which in the original module is:
.Execute Replace:= wdReplaceAll
I have to pass this named argument to .Execute, but WHS doesn't
like the := syntax. I'm trying to figure out how to do it, I'm really
confused with that. I even tried another way: instead of passing that
one named argument, passing all the arguments to the function:
.Execute (mySource, myMcase, myWhole, myWild, False, False,
True, wdFindContinue, False, myTarget, wdReplaceAll)
The document open and the first occurrence is selected, but not
replaced. What could I have missed? I'd really rather be able to pass
that named argument, though.

Thank you very much,

Luciano ES

********************************
Peter Hewett <Nos...@xtra.co.nz> wrote in

news:Xns93461A22...@207.46.248.16:

Peter Hewett

unread,
Mar 21, 2003, 4:20:19 PM3/21/03
to
Luciano

I don't know if I'm on the right track but you show the following line of
code:

.Execute (mySource, myMcase, myWhole, myWild, False, False, _


True, wdFindContinue, False, myTarget, wdReplaceAll)

Since the Execute methods parameters are all optional I'm wodering if the
real problem is with the statement itself. Generally when using script you
need an explict object reference, E.g.:

wdApp.Selection.Find.Execute

Is this ok? Sorry, I've no other ideas at the moment.

I hope this helps + Cheers - Peter


Luciano <luci...@gmx.net> wrote in news:Xns9345945BC251Dlucianoavgmxnet@
207.46.248.16:

Luciano

unread,
Mar 21, 2003, 5:30:32 PM3/21/03
to
Sorry, no. When I called that line you mention, it was inside
With myWord.Application.Selection.Find
...
End With
... so it was all implied. Besides, nothing happens when I run
it. The first occurrence of the string is found and highlighted, but
not replaced. If what you suggest were the case, I'd have been given
a syntax error message.

This is really important to me, I've been struggling with it for
days, but maybe it's time to give up...

Thanks again.
Cheers,

Luciano ES

********************************
Peter Hewett <Nos...@xtra.co.nz> wrote in

news:Xns93465ECC...@207.46.248.16:

Peter Hewett

unread,
Mar 21, 2003, 5:52:03 PM3/21/03
to
Luciano

Can you post the code in question (or at least the relevant procedure(s))
so that we can try it? I may not be able to get to this until tomorrow -
but maybe someone else can pick this up?

Cheers - Peter

Luciano <luci...@gmx.net> wrote in news:Xns9345C2A383CCClucianoavgmxnet@
207.46.248.16:

Luciano

unread,
Mar 21, 2003, 6:17:50 PM3/21/03
to
Sure, why not.

------------------------ 8< ------------------------
Dim myWord
Set myWord = Wscript.CreateObject("Word.Application")
With myWord.Application
.Visible = True
.Documents.Open "C:\WINDOWS\Desktop\teste.doc"
func_replace "find this", "replace with that", "NoCase",
"Whole", "NoWild"
' .Quit
End With
Set myWord = Nothing

'# ====================================================
Public Function func_replace(mySource, myTarget, myMcase, myWhole,
myWild)

If myMcase = "Case" Then
myMcase = "True"
ElseIf myMcase = "NoCase" Then
myMcase = "False"
Else
MsgBox " Erro!"
Exit Function
End If

If myWhole = "Whole" Then
myWhole = "True"
ElseIf myWhole = "NoWhole" Then
myWhole = "False"
Else
MsgBox " Erro!"
Exit Function
End If

If myWild = "Wild" Then
myWild = "True"
ElseIf myWild = "NoWild" Then
myWild = "False"
Else
MsgBox " Erro!"
Exit Function
End If


With myWord.Application.Selection.Find
.ClearFormatting
.Font.ColorIndex = wdAuto
.Replacement.ClearFormatting
.Text = mySource
.Replacement.Text = myTarget
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = myMcase
.MatchWholeWord = myWhole
.MatchWildcards = myWild
.Execute Replace:= wdReplaceAll
End With

End Function
------------------------ >8 ------------------------

Luciano ES

********************************

Peter Hewett <Nos...@xtra.co.nz> wrote in

news:Xns93466E5A...@207.46.248.16:

Peter Hewett

unread,
Mar 22, 2003, 9:33:08 AM3/22/03
to
Luciano

For the solution see my answer to your follow up post.

Cheers - Peter


Luciano <luci...@gmx.net> wrote in news:Xns9345CAA8E7064lucianoavgmxnet@
207.46.248.16:

> Sure, why not.
<---Snip--->
>> Luciano

Tony Strazzeri

unread,
Mar 23, 2003, 4:16:14 AM3/23/03
to
Hi Luciano,
Have a look at this article
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnofftalk/html/office07042002.asp

Particularly the section on "Adding Text to Code Modules
Programmatically" and the "InsertLines" method.

The techniques discussed there should work for the VBA in Word97.

Cheers
TonyS.

Luciano <luci...@gmx.net> wrote in message news:<Xns93444CB49BB6...@207.46.248.16>...

John Nurick

unread,
Mar 23, 2003, 1:32:56 PM3/23/03
to
Hi Luciano,

Coming in late: why not use the VBScript Regex object (and for
that matter the VBScript Split() function)? The VBScript regex
engine isn't quite as good as Perl's but it works just fine
when called from VBA.


On Thu, 20 Mar 2003 04:40:45 -0800, Luciano <luci...@gmx.net>
wrote:

John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.

Luciano

unread,
Mar 24, 2003, 5:57:39 PM3/24/03
to
Man, I worked all day long, I am now almost sleeping on the keyboard.
But I saved the page, will read it tomorrow morning and let you know
if I understand it. Thanks for the valuable tip!

Meanwhile, I think that bashing Microsoft is an old and tired sport,
but I can't help screaming that I REALLY HATE THEIR SITE!!!! I
REALLY HATE THEIR SITE!!!! I REALLY HATE THEIR SITE!!!! I REALLY
HATE THEIR SITE!!!! I REALLY HATE THEIR SITE!!!!

I REALLY HATE THAT SITE LIKE NO OTHER IN THE ENTIRE WORLD WIDE
WEB!!!!

First off, it always takes me ages to find what I want there. Every
search returns an endless list of links to links to other links that
have to do with something that is related and might possibly somehow
be connected with something that resembles or sounds like what I am
actually looking for.

Second, WHY, OH, WHY and please let me moan again, WHY, OH, WHY do
they invest such great effort on stopping us from saving their
precious pages?
- Try to save it and you'll get an empty set of frames when you log
off;
- try to open the frame in a standalone window and the frames are
loaded nevertheless!
- try to open the stinking pages through a filter that nudges away
almost all JS from the stinking source, and the wicked JS still gets
through!
- try to view the frame's source code and you still get the source
code of a frame set!

WHY, OH, WHY DO THEY HAVE TO DO THAT????!!!! If the pages are not
free and we cannot save them, how long is it going to be until they
only provide HTML pages in some sort of sick streaming format so we
cannot save them? Do they get a percentage of my phone bill for
forcing me to remain online to read the entire content of typically
long technical pages instead of downloading them and reading them
off-line?

Sorry for the rant, but I've kept it stuck in my throat for a long
long time. I just happened to reach my limit today.

Once again, thanks for the link.

Best regards,
Luciano
********************************

str...@my-Deja.com (Tony Strazzeri) wrote in
news:b1382a7c.03032...@posting.google.com:

Luciano

unread,
Mar 24, 2003, 6:01:15 PM3/24/03
to
Hi, John. Thanks for the tip, but I cannot follow you. What do you
mean with "when called from VBA"? Do you mean it is possible to use
VBS features from VBA, from inside Word? That would be a big surprise
to me...

Thank you,
Luciano, feeling a little better already

********************************
John Nurick <j.mapSo...@dial.pipex.com> wrote in
news:5bvr7v8lj9jd6kj16...@4ax.com:

Peter Hewett

unread,
Mar 24, 2003, 9:53:43 PM3/24/03
to
Luciano

Yeah, you can access VBScripts RegEx features (late discovery). For within
an office application create a Reference to "Microsoft VBScript Regular
Expressions n.n" library. Then you can use RegEx:

Public Sub RegExTest()
MsgBox (RegExpTest("is.", "IS1 is2 IS3 is4 HisX"))
End Sub

Function RegExpTest(strPattern, strToSeach)
Dim regEx As RegExp
Dim Match As Match
Dim Matches As MatchCollection
Dim strReturn As String
Dim lngCount As Long

Set regEx = New RegExp
With regEx
.Pattern = strPattern
.IgnoreCase = True
.Global = True
Set Matches = .Execute(strToSeach)
End With
For Each Match In Matches
lngCount = lngCount + 1
strReturn = strReturn & "Match " & lngCount & " found at position "
strReturn = strReturn & Match.FirstIndex & ". Match Value is "
strReturn = strReturn & Match.Value & ". Match Length is "
strReturn = strReturn & Match.Length & "." & vbCrLf
Next

RegExpTest = strReturn
End Function

And you acn always use Words Find/Replace RegEx.

Cheers - Peter


Luciano <luci...@gmx.net> wrote in news:Xns9348CBCEB1AF4lucianoavgmxnet@
207.46.248.16:

JGM

unread,
Mar 24, 2003, 10:53:54 PM3/24/03
to
Luciano,

I am not in any shape or form a great Microsoft defender....but, to save you
pains... why don't you just select the page content, do CTRL-C, then open a
blank Word document and do CTRL-V?

I do it all the time, especially when I find long articles which, like you
said, could be possibly remotely related to the general topic connected to
the information I am looking for, but want to read it later. Form MS KB or
MSDN I always get neatly spaced documents, with all the graphics and the
tables intacts, the only thing is that it can be a litttle bit too spaced
out.

The only inconeniant with this is when you have a very long Web page, it can
take up to 30 seconds to select it by dragging down the mouse...

Cheer up!

--
_________________________________________

Jean-Guy Marcil
proje...@sympatico.ca
"Luciano" <luci...@gmx.net> a écrit dans le message de news:
Xns9348CB32A238...@207.46.248.16...

Kamal el Mar.

unread,
Mar 26, 2003, 11:55:11 AM3/26/03
to
I'm trying to do almost the same thing:
I have an interface wich can pilot MS.Word document through 2 maniers:
1)- Through OLE-Automation Objects: I'd ruther not use it.
2)- Through generating a file (*.txt or *.vbs) that contains the
VBA-script that will be excuted.

BUT my problem is that the code of a Macro/Module can not be saved in
a separated file from a word document. so i have to read the generated
file and import programatically (from Visual Basic Editor of Word 97)
that code and execute it right away.

IS this possible?? because i've been loking into this now for about 5
day's and NOTHING ;-(

So Peter, Could you please send me that code you sent to Luciano .. It
sounds like it could help me.

And if you have any other suggestions i would be very happy to hear
them

Thank you and all by advance for any input.

Kamal (Rotterdam - Holland)
email: kelmar...@fundamental-software.com


Peter Hewett <Nos...@xtra.co.nz> wrote in message news:<Xns93461A22...@207.46.248.16>...

Peter Hewett

unread,
Mar 26, 2003, 4:37:33 PM3/26/03
to
Kamal

What's the problem with COM? If you're going to generate the code for a
script why not execute it directly through COM? What's the host application
that needs to drive Word (VB, Excel, etc.)? Oh and what version of Word are
you using?

The code I offered Luciano, is very simple parsing stuff. It's not necessary
if you are using Word 2000 or later. In any event if you need to parse text
you can use VBScripts RegEx through COM.

If you create a Word document from a template it will already have any
boilerplate text you require. It can also contain macros that you can start
through the COM interface. Given the COM option combined with macros in the
template, I'm unclear about why you want code external to the
document/template?

I hope this helps (some!) + Cheers - Peter


kelmar...@fundamental-software.com (Kamal el Mar.) wrote in
news:206c422f.03032...@posting.google.com:

Kamal el Mar.

unread,
Mar 27, 2003, 9:32:15 AM3/27/03
to
first of all, thank you for your reaction Mr. Peter
The thing is that i'm realy a niewbie in this VBA voor word.
I'm using Windows98 and Office Word 97 8.0b
I'm on a traineeship-program and the company where i work uses the
software WinDev5.0 (something like Java or Delphi) of PCSoft to
develop some administrative applications.
In this applications, repports (like bills, artikel prices...) are
generated from database files called Hyper Files. The repports are
flexible for our users. so to make them flexible and let the user
change layout and displayed data, i was asked to write an interface
(in WinDev) that does pilot a word document by constructing it and
injecting the data in it.
I tried to construct templates that will do the work, but it does not
help because i have to predifine tables and other components in the
document..the thing that we don't want. moreover we would have then to
send that (*.doc or *.dot or *.wiz) to all of our customers to be able
to use it.
So what i did is this: an interface written in WinDev gives the user
posibility to define tables, labels, text, header, footer, position of
the lables, ... when the user is done with constructing the layout of
his repport (graphically on this interface), WinDev has to read then
the properties of the layout desired by the user, write the VBA-code
to an external file (*.vbs or *.txt), launch MSWord, open a new
document, some how attach to it the VBA-code from the external file,
load that code, run it and save the document.
I do not know if this is a smart way to do what i wanna do ;-<<
I tried to write the code to a file and save it as *.vbs and then run
it.
waht i mean: for example if you open a text file and write this line
MsgBox "This is just a test" to it, and then save it as *.vbs or
*.vba, when you dubbel clik the saved file you get a Message Box whit
the sentence.
But if i choose for this approach: i would have to write all the
vba-code wich i do NOT know well!
In Word VBA Editor i use the Macro Recorder to generate peaces of code
to help me write the code that my WinDev interface should generate.

I have the feeling that i'm using a wrong approch.

I'm sorry to take a lot of your time. but i was trying to make my
misery as clear as possible ;->>

Thank you by advance for you help.

Peter Hewett <Nos...@xtra.co.nz> wrote in message news:<Xns934B61B8...@207.46.248.16>...

Peter Hewett

unread,
Mar 28, 2003, 9:05:46 AM3/28/03
to
Kamal

I know nothing at all about WinDev5.0. I've literally no idea about what it
can and can't do! I've been programming a looong time and what you've been
asked to do sounds pretty damn complex. If I were you I'd try to use COM then
you can "talk" to Word directly, your alternatives stink!

I think the idea of trying to create VBA code that gets added to a Word
documents VBProject is a non starter. For a start you've then got to write
code to add the code to the documents VBProject and then get the code to run!

If you have to do anything I'd consider using VBScript. That way at least you
can execute the code directly without first having to load it into Word!

I don't have another suggestion at this time (other than ask whoever asked
you to do this to rescope the project requirements and deliverables).

The problem you have is that you've got to create code in the WinDev5
environment that creates flexible, accurate and robust Word/VBA/VBScript
code. Using Word VBA directly it can be hard enough generating or assembling
a document programatically, then you've got the problem of getting the data
into your document! I don't envy you the task of writing code that writes
code in a different language!

If you're a newbie to Programming and/or Word/VBA/VBScript I'd try and
persuade whoever asked you to do this to find another way. Unless you can use
COM I can't see how it's going to be robust enough. Also this task is complex
enough that you need to spend time producing a good design before you start
coding.

I hope you've allowed a long time for this project!

I don't mean to be pessimistic but I've enough experience to forsee a large
number of problems!

In any event good luck - I'm not sure I'm going to be of much help to you on
this project!

Cheers - Peter

> first of all, thank you for your reaction Mr. Peter

Luciano

unread,
Mar 29, 2003, 1:02:27 PM3/29/03
to
Hi, Peter. I read your suggestion with great interest, but could
not really use it because it's gone over my head. It touched one hair
or two, but still over my head. I investigated all I could and
understood that we can link our VBA code to external libraries. Very
interesting indeed. And I certainly understand how to add references
to those libraries. But then what? How am I supposed to know which
objects or methods are made available by the library? Where will they
be documented? I know a little VBS, for example. I know its objects
and methods, but how would I call them and/or use them? Do you think
you can make that part a little clearer in my foggy mind?


Thank you very much,
Luciano

********************************


Peter Hewett <Nos...@xtra.co.nz> wrote in

news:Xns93499752...@207.46.248.16:

Luciano

unread,
Mar 29, 2003, 1:06:49 PM3/29/03
to
So, like I said in the other message, I finally found a
solution, an almost perfect one. I tried to to it with VBS, but it
didn't pan out because it was too slow. I found this macro on the
Web: http://www.trigeminal.com/lang/1033/codes.asp?ItemID=6#6 and it
can do what I wanted all along: run a string as VBA code.
It's almost perfect because it has shown a very odd behavior:
sometimes it runs, sometimes it fails. Nothing happens. I added a
pop-up box that says "Done" when the macro is done, and sometimes
nothing happens, I just see the "Done" box. And here is the weirdest
part. It won't work again until I "tweak" my macro. Any silly tweak
usually will do, like deleting an arbitrary line, running it,
watching it fail, than restoring the deleted line and running it
again. I have debugged more than a man can take it, and I am pretty
sure that all other parts of my macro run fine. It's just the call to
that third-party macro, which in turn calls a function, that fails to
do its job. I shall contact the author and see if he has any idea of
why this odd behavior is occurring. If I find out anything
interesting, I'll let you know. Meanwhile, try the macro. It might
run fine on your system.

Best regards,
Luciano ES

Luciano

unread,
Mar 29, 2003, 1:05:53 PM3/29/03
to
Hello, guys. I finally found a solution, an almost perfect one.

********************************

> I'm trying to do almost the same thing:

John Nurick

unread,
Mar 29, 2003, 4:51:49 PM3/29/03
to
Hi Luciano,

If you set a reference to the library as Peter suggested, the
objects, methods and properties appear in the VBE Object
Browser and the Intellisense dropdowns, just like the regular
libraries.

The VBScript regexp object is documented (in the usual sketchy
way) in the VBscript help file VBSCRIP5.CHM. The documentation
on regular expression syntax there is incomplete: the version
5.5 library can also handle some of the Perl (?...) extended
syntax.

On Sat, 29 Mar 2003 10:02:27 -0800, Luciano <luci...@gmx.net>
wrote:

John Nurick [Microsoft Access MVP]

Luciano

unread,
Mar 30, 2003, 7:42:23 AM3/30/03
to
Peter, I investigated your suggestion further and finally begin
to see what you mean. But I cannot find any such item as "Microsoft
VBScript Regular Expressions" in my Reference list. I searched high
and low and found this page:
http://featurecam.com/api/Examples/General%20Purpose/example_of_using
_the_regular_exp.htm
So it seems the precious RE are stored in vbscript.dll. OK, I
added it to my Reference list, but it came in as "Microsoft VBScript
Globals", not "Microsoft VBScript Regular Expressions". And calls to
"New RegExp" generate an error (user type not defined). Do you have
any suggestions?

Thanks,
Luciano

********************************
Peter Hewett <Nos...@xtra.co.nz> wrote in

news:Xns93499752...@207.46.248.16:

> Yeah, you can access VBScripts RegEx features (late discovery).
> For within an office application create a Reference to
> "Microsoft VBScript Regular Expressions n.n" library. Then you
> can use RegEx:
>
> Public Sub RegExTest()
> MsgBox (RegExpTest("is.", "IS1 is2 IS3 is4 HisX"))
> End Sub
>
> Function RegExpTest(strPattern, strToSeach)
> Dim regEx As RegExp
> Dim Match As Match
> Dim Matches As MatchCollection
> Dim strReturn As String
> Dim lngCount As Long
>
> Set regEx = New RegExp
> With regEx

>...

Luciano

unread,
Mar 30, 2003, 7:48:35 AM3/30/03
to
PCRE in VBA? Wow. That's even more than I expected.
Provided I can make it work... :-(

Luciano

********************************
John Nurick <j.mapSo...@dial.pipex.com> wrote in

news:bj4c8v4tb1vl2bc0j...@4ax.com:

John Nurick

unread,
Mar 30, 2003, 12:11:05 PM3/30/03
to
Luciano,

These instructions seem to work. Please tell me if they don't
so I can improve them.

'Requires:
' Reference to Microsoft VBScript Regular Expressions 5.5
'
'If this is not available on your installation of Office,
'1) Verify that VBSCRIPT.DLL exists (usually it's in
' C:\WINNT\System32 or C:\Windows\System32). If not,
' install VBScript (or upgrade Internet Explorer to
' version 5.0 or later)
'
'2) If it exists, but is not available in the
'list of references in the VBA window register
'it by using the following command in Start|Run:
'
' regsvr32.exe C:\PATH\VBSCRIPT.DLL
'
'where PATH is the actual path such as WINNT\System32
'
'3) If it is still not available, go to the Immediate
'window (in Access 97, the Debug window) and execute this
'statement:
'
' Application.References.AddFromFile "VBScript.dll\3"

On Sun, 30 Mar 2003 04:42:23 -0800, Luciano <luci...@gmx.net>
wrote:

John Nurick [Microsoft Access MVP]

John Nurick

unread,
Mar 30, 2003, 12:18:33 PM3/30/03
to
Here's a function that uses the regexp object to to validate a
string against a pattern. I wrote it for data entry in Access,
and it uses late binding so it doesn't need any references
setting. It demonstrates a very simple use of the regexp object
with reasonably thorough error trapping. (It's all so much
simpler in Perl, isn't it?<g>).

Function rgxValidate(Target As Variant, _
Pattern As String, _
Optional CaseSensitive As Boolean = False, _
Optional MatchSubString As Boolean = False, _
Optional FailOnError As Boolean = True) _
As Boolean

'Returns True if the whole of Target matches Pattern.

'By John Nurick, October 2002 - January 2003

'NOTES
'Target will normally be a String. If Target is Null,
'rgxValidate returns False. Otherwise if Target cannot be
'converted to a string with CStr(), rgxValidate fails
'with Error 13, Type Mismatch.

'Pattern should be a regular expression. See VBScript
'help file and other documentation for information.

'CaseSensitive does the expected.

'MatchSubString: if True, rgxValidate returns True if any
'substring of Target matches Pattern. If False or omitted,
'the function only returns True if the whole of Target
'matches Pattern.
' E.g. Target "12345" only matches Pattern "234" if
' MatchSubString is True.

'FailOnError: if this is True or omitted, rgxValidate passes
'any run time error to the calling procedure. If it is False,
'the function returns True on a successful match and False if
'the match fails for any reason including a run time error.

'rgxValidate is suitable for use in data entry forms and the
'like. It can also be used in queries and in looping through
'recordsets, but since it creates a RegExp object and
compiles
'the regular expression (Pattern) every time it is called,
'it is rather inefficient for repetitive operations.

Const rgxPROC_NAME = "rgxValidate"
Const rgxERRMSG_CREATE = "Could not create VBScript.RegExp
object: "
Const rgxERRMSG_UNEXPECTED = "Unexpected error: "

'VBScript.Regexp error messages
Const rgxERRMSG_5017 = "Syntax error in regular expression"
Const rgxERRMSG_5019 = "Expected ']' in regular expression"
Const rgxERRMSG_5020 = "Expected ')' in regular expression"

Dim oRE As Object

On Error GoTo ERRHANDLER

rgxValidate = False 'Default return value
If IsNull(Target) Then Exit Function

Set oRE = CreateObject("VBScript.RegExp")
oRE.Global = False
oRE.IgnoreCase = Not CaseSensitive
oRE.Multiline = False
If MatchSubString Then
oRE.Pattern = Pattern
Else
'Add anchors to Pattern to force match of whole Target.
'(RegExp engine doesn't mind if this duplicates existing
anchors)
oRE.Pattern = "^" & Pattern & "$"
End If
'Do the job
rgxValidate = oRE.Test(CStr(Target))
'Tidy up
Set oRE = Nothing
Exit Function

ERRHANDLER:
If FailOnError Then
With Err
Select Case .Number
'Replace the default "object-defined error" message
Case 5017: .Description = rgxERRMSG_5017
Case 5019: .Description = rgxERRMSG_5019
Case 5020: .Description = rgxERRMSG_5020
Case Else
If oRE Is Nothing Then 'Failed to create Regexp object
.Description = rgxERRMSG_CREATE & Err.Description
Else 'Unexpected error
.Description = rgxERRMSG_UNEXPECTED & Err.Description
End If
End Select
Set oRE = Nothing
Err.Raise Err.Number, , rgxPROC_NAME & "(): " &
.Description
End With
Else 'Fail silently
Err.Clear
Set oRE = Nothing
End If
End Function

On Sun, 30 Mar 2003 04:48:35 -0800, Luciano <luci...@gmx.net>
wrote:

John Nurick [Microsoft Access MVP]

Luciano

unread,
Mar 30, 2003, 2:08:48 PM3/30/03
to
Hmmm... that certainly sounds auspicious. However...

> '1) Verify that VBSCRIPT.DLL exists (usually it's in
> ' C:\WINNT\System32 or C:\Windows\System32). If not,
> ' install VBScript (or upgrade Internet Explorer to
> ' version 5.0 or later)

It is there, C:\Windows\System\VBScript.dll, and I have VBS 5.6.

> '2) If it exists, but is not available in the
> 'list of references in the VBA window register
> 'it by using the following command in Start|Run:
> '
> ' regsvr32.exe C:\PATH\VBSCRIPT.DLL

Not needed, but already had done that just in case.

> '3) If it is still not available, go to the Immediate
> 'window (in Access 97, the Debug window) and execute this
> 'statement:
> ' Application.References.AddFromFile "VBScript.dll\3"

VB Editor says:
"Compilation error:
Data member or method not found"

... or something liek that, in Portuguese.

Any other ideas?

Thanks,
Luciano

Luciano

unread,
Mar 30, 2003, 2:14:42 PM3/30/03
to
Oh, I forgot to say that I have the OleView program and I saw
that "VBScript Regular Expressions" is indeed in there, somewhere in
my system. I could find the Type Library. And when I browse to its
entry, I can see the Regexp method in the CoClasses list, but right
above it is MS VBScript Globals, which also is the
c:\windows\system\vbscript.dll, and it does not have any classes at
all.

Does it mean anything?

Luciano ES

Luciano

unread,
Mar 30, 2003, 2:51:21 PM3/30/03
to
Hmmm... very interesting indeed. But you say that this code is
not good for repetitive operations. Bad. My application currently
replaces about 2,000 entries per minute. But I keep all the entries
in the macro itself, which is bloating, hard to maintain and
abhorrent coding practice. That's why I really need to be able to
pull data from the outside, preferably a plain-text file.
Your code works fine and should be a very interesting study
subject for the rest of my day. I'd still have to add replace
functionality, LOTS of speed and some way to make it formatting-
aware. It should only work on text with certain colors, for example.
This is all a lot more efficient and easier if done with Word's
native search and replace functions. So I'd really rather find a way
to pull data from an external file. Using VBS with early binding,
however, could make it all faster and a lot more interesting, because
then I could use actual Regular Expressions instead of Word's sad
"wildcards"...

> (It's all so much simpler in Perl, isn't it?<g>).

- - Geez, tell me about it! =8-O

Luciano

********************************

John Nurick <j.mapSo...@dial.pipex.com> wrote in

news:8d9e8v4r16jq2k7mn...@4ax.com:

John Nurick

unread,
Mar 30, 2003, 4:47:48 PM3/30/03
to
As it stands, the function creates the regexp object, sets a
pattern and executes the match (which requires the pattern to
be compiled), then destroys the object. This doesn't seem to
cause any problems except for wasting a lot of processor
cycles: calling the function a million times in succession with
a variety of patterns and targets took a long time but didn't
reveal any memory leaks or other problems that I could detect.
There are also no problems that I know of in having multiple
regexp objects in existence at the same time.

To speed it up, just move the code that creates the regexp
object and sets the Pattern property outside the loop so it
only runs once (or once for each new pattern).

This will make it easy to keep the data for the replace
operations in an external file. I guess I'd read the contents
of a file into an array or a collection at the beginning of the
code. (BTW, did you know that VBScript has a Split() function,
although it can only split on strings, not on patterns).

But using the RegExp object for search/replace operations that
are aware of Word formatting is going to be difficult, because
you can only pass it plain text, not formatted text.

One approach might be to use a simple Word search to find a
block of text with the relevant formatting, pass this to your
plain-text search/replace routine, then repeat the Word search
to get the next block of text, and so on.


On Sun, 30 Mar 2003 11:51:21 -0800, Luciano <luci...@gmx.net>
wrote:

John Nurick [Microsoft Access MVP]

John Nurick

unread,
Mar 30, 2003, 5:00:55 PM3/30/03
to
On Sun, 30 Mar 2003 11:08:48 -0800, Luciano <luci...@gmx.net>
wrote:

>> '3) If it is still not available, go to the Immediate
>> 'window (in Access 97, the Debug window) and execute this
>> 'statement:
>> ' Application.References.AddFromFile "VBScript.dll\3"
>
> VB Editor says:
> "Compilation error:
> Data member or method not found"
>
> ... or something liek that, in Portuguese.
>

It turns out that the Word and Excel Application objects don't
have References collections. The VBProject object in the Visual
Basic 6 Extensibility library does have a References collection
with an AddFromFile method. I don't know how to use it but I'm
sure someone does. Alternatively, if you have Access on your
computer, fire that up and use Application.References. Once the
library has been added to one project in any Office app I
believe it will remain available to be selected in the
Tools|References dialog of the other apps.

0 new messages