External commands for Word?

C

CLR

Hi All......
Background:
I have an Excel Add-in that will create a textScript of AutoCAD commands and
store it in a file. Then, I have an AutoCAD menu which allows me to seize
that Scriptfile and have it to start feeding AutoCAD it's commands and
AutoCAD responds, specifically by opening AutoCAD files and printing them
out...........

Problem:
I would like to know if it's possible to get Word to find a similar Script
file and use it to execute the list of commands therein to open and print a
list of
Word documents. Of course, the contents of the new Scriptfile would be
modified to be Word commands rather than AutoCAD ones, and the list would
vary each time.

Any ideas would be appreciated.....
Vaya con Dios,
Chuck, CABGx3
 
J

Jonathan West

Hi Chuck,

Word works a bit differently, in that the Word commends get embedded in
templates.

For a very good starter on how to deal with Word programming, take a look at
these articles.

Creating a macro with no programming experience using the recorder
http://www.mvps.org/word/FAQs/MacrosVBA/UsingRecorder.htm

Getting To Grips With VBA Basics In 15 Minutes
http://www.mvps.org/word/FAQs/MacrosVBA/VBABasicsIn15Mins.htm


--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
C

CLR

Thanks Jonathan.........I appreciate the response........

I do know how to Record a macro and even do minor editing of them as I do it
frequently in Excel......and I even have an Excel add-in that someone gave
me that will create an external "script file", as it does for Autocad, and I
can probably edit even that for my purpose.........my problem, as I see it,
is that I need to find out how to get Word to pick up on an external file
and start executing the code therein............the code being the path and
filenames (and maybe print parameters) of .doc files that I want printed.

I will check the references you gave me tho, and thanks again........

Vaya con Dios,
Chuck, CABGx3
 
L

Lars-Eric Gisslén

Chuck,

VBA does not have a script engine, that I'm aware of. That would require VBA
to have a macro compiler or a runtime sourcecode interpreter. I have never
came across such functionality in VB/VBA. There are programming laguages
like CA-Visual Objects, SmallTalk, Clipper that has a macro compiler that
can compile scripts at runtime and execute them. The compiler is then in the
runtime environment.

The closest I can come up with would be a XML file with all your commands
and parameters. The commands are VBA Sub calls that takes the parameters you
supply. When parsing the XML file (using the XMLDom parser) you can call the
Sub's with Application.Run. My example below should explain what I'm
thinking about.

Sub Test()
Dim CallMySub As String
Dim sParams As String

CallMySub = "DispMsg"
sParams = "Hello there!; Sub called with Application.Run"

Application.Run CallMySub, sParams

End Sub

Sub DispMsg(sMsg As String)
Dim aParam

aParam = Split(sMsg, ";")

If UBound(aParam) = 0 Then
MsgBox aParam(0), vbInformation
ElseIf UBound(aParam) = 1 Then
MsgBox aParam(0), vbInformation, aParam(1)
Else
MsgBox "Wrong numbers of parameters passed to DispMsg", _
vbCritical, "Error"
End If

End Sub

Regards,
Lars-Eric
 
J

Jonathan West

CLR said:
Thanks Jonathan.........I appreciate the response........

I do know how to Record a macro and even do minor editing of them as I do it
frequently in Excel......and I even have an Excel add-in that someone gave
me that will create an external "script file", as it does for Autocad, and I
can probably edit even that for my purpose.........my problem, as I see it,
is that I need to find out how to get Word to pick up on an external file
and start executing the code therein............the code being the path and
filenames (and maybe print parameters) of .doc files that I want printed.

I will check the references you gave me tho, and thanks again........

Don't think in terms of Word executing *code* stored in an external file.
Can't do it. Think in terms of a Word VBA macro reading *data* for an
externa file and performing actions based on the content of that data.

Its not that hard to open a text file and get hold the data in it a line at
a time (Look up the Input and Line Input commands in the VBA Help file). If
you know the data is in the form of pathnames, then you can use it to open
the relevant file and carry out whatever operations you want.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
C

CLR

Many thanks Lars-Eric and Jonathan.......you guys are great. Of course
you're both talking waaaaay over my head right now, but at least you
indicate that what I want to do is do-able.......and that was the main thing
I was after. I do believe that what I was trying to describe is more like a
text file that is called into a Word macro line by line and executed as the
path and filenames I want to open and print......at least thats the route I
think I will try first.....

Many thanks again to both of you.....I'm off to check the Input and Line
Input commands........

Vaya con Dios,
Chuck, CABGx3
 
J

Jay Freedman

Hi, Chuck,

In case you haven't seen it, I posted working code for you in the
microsoft.public.word.vba.beginners newsgroup, in response to another of the
three identical posts you made.

This is an excellent example of why we ask people to post in only one
newsgroup, or at least to use the cross-posting feature of their newsreader
to link all the copies together instead of scattering threads all over
creation. (In case you weren't aware of it: in the message editor of Outlook
Express, you can click the word "Newsgroups" in the header area and choose
more than one group to post to simultaneously -- but PLEASE don't use it to
spam the groups!)
 
L

Lars-Eric Gisslén

Chuck,

I still think an XML approach would be more easy to write and easier to
maintain. You normally already have the XML parser on your computer and
don't have to parse the file by your self.

Assume you have a XML file looking like below which is specifying the Sub's
you want to call and the parameters:
<?xml version="1.0" encoding="UTF-8"?>
<ChuckScript>
<Commands>
<Command>
<Name>MyOpenDoc</Name>
<Parameters>C:\temp\mydoc.doc</Parameters>
</Command>
<Command>
<Name>MyPrintDoc</Name>
<Parameters/>
</Command>
<Command>
<Name>MyCloseDoc</Name>
<Parameters/>
</Command>
</Commands>
</ChuckScript>

This XML file specifies three 'Command' and a parameter for only the first
Command. Then it would be very easy to add/remove Command tags depending on
your needs. One Command tag for each Sub you want to call.

Then your macros could look like this:

Sub XMLScript()
Dim sXMLFile As String
Dim oXML As Object
Dim oDocNode As Object
Dim oCommand As Object
Dim oCommandNode As Object
Dim oSub As Object
Dim oParam As Object
Dim oError As Object

sXMLFile = "c:\temp\chucktest.xml"

'**** This code is to get the XML Parser up and trunning with the XML file
' Create an instance of the Parser with the version independent
interface
' Sometimes you should use MSXML2.DOMDocument
Set oXML = CreateObject("MSXML.DOMDocument")
oXML.validateOnParse = False
oXML.async = False

oXML.Load sXMLFile ' Load the XML file

Set oError = oXML.parseError
' Check if there was any error open/parsing the XML file
If oError.errorCode <> 0 Then
MsgBox "Error parsing " & sXMLFile & vbCrLf & _
"Error: " & oError.reason
Exit Sub
End If

'**** If we get here we are ready to go

' Get the document tag (ChuckScript)
Set oDocNode = oXML.documentElement
' Get the whole node tree for the Commands node
Set oCommandNode = oDocNode.selectSingleNode("Commands")

'**** This is the logic for calling the Sub routines
' Loop through all Command nodes in the Commands node
For Each oCommand In oCommandNode.childNodes
' Get the Name and Parameters nodes in the Command node
Set oSub = oCommand.selectSingleNode("Name")
Set oParam = oCommand.selectSingleNode("Parameters")
' Call the sub defined in the Name node and the parameter string
Application.Run oSub.Text, oParam.Text
Next
'**** That's all

' Be polite and clean up after you
Set oXML = Nothing
Set oDocNode = Nothing
Set oCommand = Nothing
Set oCommandNode = Nothing
Set oSub = Nothing
Set oParam = Nothing
Set oError = Nothing

End Sub
'-------------------------------------
Sub MyOpenDoc(sParams As String)
MsgBox "MyOpenDoc was called with this parameter:" & vbCrLf & sParams
End Sub
'------------------------------------
Sub MyPrintDoc(sParams As String)
MsgBox "MyPrintDoc was called with this parameter:" & vbCrLf & sParams
End Sub
'------------------------------------
Sub MyCloseDoc(sParams As String)
MsgBox "MyCloseDoc was called with this parameter:" & vbCrLf & sParams
End Sub
'------------------------------------

If you try this code with the XML file I think you will get the whole idea.
As you can see you don't have to bother about opening, reading, parsing a
text file if you take the XML approach. The XML Parser does it all for you.

We actually used a similar solution in a large system. It was a system for
power suppliers that was reading the power meters remotely. Each power
supplier (with self respect) has their own billing system that expects data
in a specific format. We craeted a small application where the users could
specify which methods to use to collect the data (like calling Sub's above),
which data to output, the output format of the binary file and very detailed
formatting of the data. All the settings were written to an XML file and the
system used this XML file when generating the output for the different
billing systems. This solution turned out to be very flexibel and very easy
to maintain. When output formats changes we do not have to tuch the system
as all definitions are stored outside the system in XML.

One other good thing about working with XML is that there are many tools
available. I prefer XMLSpy. XML is also becomming the standard for data
exchange between different systems.

Regards,
Lars-Eric
 
J

Jay Freedman

Hi, Lars-Eric,

With all due respect, I think for this application a Word-dedicated non-XML
approach is simpler and easier to understand. Have a look at the code I
posted in Chuck's other thread in microsoft.public.word.vba.beginners
(subject: "Print list from external Script.scr file"). It's at least 50%
shorter, and a third of that is error-trapping code. It's also less wasteful
of resources -- everything is inside Word, rather than pulling in an
external parser.

I can see where your method would have an advantage if different documents
needed to be run through different procedures, or if the list of procedures
might change with time; but simply opening and printing a list of files
doesn't require the machinery to call out procedure names file by file.
 
L

Lars-Eric Gisslén

Jay,

I haven't followed the discusion on the other NG as I don't subscribe on it.
I got the feeling that he wanted some kind of scripting functionality and
therefor presented the example. Yes, if the file is only a list of filenames
I can agree that using XML might be an overkill. My example was also to let
those not familiar with XML se that working with XML is not any rocket
science. The new features in Office 2003 revolves so much around XML so I
think a good understanding about XML, XSL, Schemas, NameSpaces and so on
will soon be required. For sure, W3C Schemas will be the hardest part to
learn for those new to the XML world.

Regards,
Lars-Eric
 
C

CLR

Hi Guys......

Many thanks to Jonathan, Lars-Eric, and Jay.......you guys "rock"........I
really appreciate the time you pros take to help the rest of us
out.........although I think probably Lars-Eric's suggestion would probably
be better for me in the long run, I had to go with Jonathan's idea and Jays
code because I could just barely understand it and the other thing was 'way
beyond me right now. And thanks especially to you Jay, your code worked
perfectly after I blundered along and finally got things in their right
directories and names, etc........

Long live the newsgroups and you guys who support them.......

Thanks again,
Vaya con Dios,
Chuck, CABGx3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top