PDA

View Full Version : How to run word macro from a ".bat" file



bobk544
12-29-2007, 03:29 PM
Hello, i'm using word to cross reference hundreds of c++ programs and the overall xref doc is so big i have to split it up into 6 500k byte subdocs, but i now need to run a macro against each of one of those subdocs in it's own directory.

Is there a way i can run a ".bat" file program that will call the word macro for example:

exec wordMacro1 < xref_allref1.doc
exec wordMacro1 < xref_allref2.doc
exec wordMacro1 < xref_allref3.doc
....... ect?

The reason i need to do this is because if i put all the subdocs in 1 directory, word doesn't seem to want to release the memory and i'm continually running into memory problems, but i think if i completely close out of word, it may free up that memory for the next subdoc.:think:

thanks for any insights:dunno
BobK

fumei
12-30-2007, 01:41 AM
Are you doing this from Word? Or are you doing this using an declared Word instance?

"put all the subdocs in 1 directory, word doesn't seem to want to release the memory" - I find this unlikely. The fact that the files are one directory...or five directories should have no bearing. It may...as Word does indeed some odd things now and then with temp files.

"i now need to run a macro against each of one of those subdocs" - sorry, but that is not particularly helpful. What does this macro do?

"Is there a way i can run a ".bat" file program that will call the word macro for example:" Well, sort of. You can most certainly execute code on a series of files in a folder. But there is no direct way to pass parameters (your < xref_allref1.doc
) to a macro that is executing upon the start of Word...which is what you are saying with:

" but i think if i completely close out of word, it may free up that memory "

Maybe...maybe not. Word is, very true, notorious for not handling memory fantastically well.

HOWEVER, quite a bit of memory problems stem from the chair...not the application.

Hard to say. Your file sizes are large, 'tis true, but that is not absolutely going to make Word choke.

More details are needed. Especially if you are creating an instance of Word via code.

bobk544
12-30-2007, 07:28 AM
Hello,

>>>>"Are you doing this from Word? Or are you doing this using an declared Word instance?"

Well i am opening up a ".dot" word document and going to the Macro-Visual Basic Editor and running th macro from there, ie:


Sub xref2()
Dim FileArray() As String, ffile As String, Count As Integer
Count = 0
With Dialogs(wdDialogFileOpen)
.Name = "*.*"
.Show
End With
ffile = Dir("*")
'ffile = Dir("c:\RBC_XPGMS\*")

'MsgBox ("ffile1" + ffile)

ReDim FileArray(Count) ' 0 based array -- dimensioning for 1 entry in the array
FileArray(Count) = LCase(ffile)
Count = 1
Do While ffile <> ""
ffile = Dir()
'MsgBox ("ffile2" + ffile)
If ffile <> "." And ffile <> ". ." Then
ReDim Preserve FileArray(Count) ' Resizing array dynamically
FileArray(Count) = LCase(ffile)
Count = Count + 1
End If
Loop

Dim lb As Integer
Dim ub As Integer
lb = LBound(FileArray(), 1)
ub = UBound(FileArray(), 1)
ub = ub - 1
Dim f As String

'For i = 0 To Count
'For i = 200 To 249
'For i = 250 To 299
'For i = 300 To 349
'For i = 350 To 399
'For i = 400 To 420
'For i = 145 To 155

For i = 201 To 227

f = FileArray(i)

pgmlink (f)

'ScriptLink (f)

'singlelinks (f)

'pgmlink_spec (f)

Next

End Sub

fumei
12-30-2007, 07:31 PM
I wil repeat. More details needed.

I have no idea what the heck pgmlink (f) means. I have no idea why you are doing a For i = 201 To 227. I have no idea why you are using Dir like that...it seems very odd, and kind of inefficient.

ffile = Dir("*")

Say what??????

Do While ffile <> ""
ffile = Dir()

Say what?????

What are you trying to do here? I have no idea why you are building an array. Why not simply USE the Dir function?

I have no idea why you are declaring variables, setting them, but never using them. Dim lb As Integer
Dim ub As Integer
lb = LBound(FileArray(), 1)
ub = UBound(FileArray(), 1)
ub = ub - 1

Sorry, but if that was supposed to help me understand what you are doing, it didn't.

fumei
12-30-2007, 07:33 PM
Oh, and:

"Well i am opening up a ".dot" word document and going to the Macro-Visual Basic Editor and running th macro from there,"

Why are you "opening up" a .dot file?

bobk544
12-31-2007, 07:31 AM
well i think you just answered my question, if i wouldn't be able to pass in any parms then i quess i'll have to keep doing this manually!

thanks
bk

Oorang
12-31-2007, 07:44 AM
Have a look at this: http://support.microsoft.com/kb/210565 Will the "M" switch do what you are looking for?

fumei
12-31-2007, 09:12 AM
Absolutely you can start up with running an macro. But....you still can not pass parameters to it.

winword.exe /m yadda

will execute the yadda macro/procedure (subject to correct path etc.)

HOWEVER, if yadda is:Sub yadda (StrIn As String)
' do yadda yadda yadda
' do more yadda
End Sub
winword.exe /m yadda

will fail, as the yadda Sub has strIn as a required parameter, and since you can not pass that parameter on the command line....

bk,

"well i think you just answered my question, if i wouldn't be able to pass in any parms then i quess i'll have to keep doing this manually! "

Well I am happy for you that you had your questions answered, because you have not been polite whatsoever in answering any of mine.

I was trying to help by asking them. Why? Because there may be a way to do what you want, just different from what you are currently doing. However, since you do not seem to care to bother, and seem happy with doing it manually, then I suggest you mark thread as Solved.

Oorang
12-31-2007, 10:42 AM
Actually you can pass a parameter to it. You simply have to make the macro smart enough to read it. You can generate a temp file with whatever parameters you want in it with the bat file by using the syntax

ECHO foo > %TMP%\data.tmp Then just set up the macro in question to be able to read the temp file and then clean up after itself.

fumei
12-31-2007, 11:09 AM
Yes, like most things using VBA there are alternatives. However, let's look at that one.

1. You have to create the text file with the parameters. Let's use the OP's examples.

exec wordMacro1 < xref_allref1.doc
exec wordMacro1 < xref_allref2.doc
exec wordMacro1 < xref_allref3.doc

OK. So you have a text file with:

xref_allref1.doc
xref_allref2.doc
xref_allref3.doc

These are the parameters you want to pass.

2. the WordMacro1 is used with the /m switch:

winword.exe /m wordMacro1

3. Yes, wordMacro1 can open the text file and get something to use as a parameter. However, in order to know which parameter to use you will have to write some sort of counter/flag at either:

1. the OS level; OR
2. perhaps a Docvariable in Normal.dot.

In either case, Word has to know which one of the text file lines (a parameter) to use. As the effort seems, and does, use a new instance of Word (it is using the /m switch), that some sort of persistent value needs to be set.

My point still stands. You can not pass a parameter to a commandline /m. The called procedure can, with some work, determine a value to use in its processing. But that value is not, in fact, passed to the called procedure.

The procedure (the macro) can NOT have a required parameter, like my example: Sub yadda (StrIn As String)

Can you make a macro "smart enough" to figure stuff out? Most certainly.

My other point to bk still stands as well. There seems to be (IMO) a mis-use of the Dir function. It can probably be fixed. It could possibly be fixed enough to deal with the possible memory problem. In which case, writing a separate text file, figuring out to keep a persistent/incrementing value - in order to use the parameters in the text file....yadda yadda yadda...may not be needed.

Or "doing it manually".

Oorang
12-31-2007, 11:29 AM
I am not saying that it should be done. Simply answering the questions asked.

fumei
01-02-2008, 10:45 AM
Hi Aaron.

"Simply answering the questions asked."

What was the question again? Is it: can I pass a parameter to a Word startup macro? We may be fussing with terms, but I still feel the answer is no, you can't.

If the question is: can I write a smart Word startup macro that can determine/use a variable? Then the answer is, of course, exactly as you have suggested. Yes you can.

However, it is moot I think. I still feel the issue OP has - memory problems - could be fixed with proper/better use of the Dir function. Performing the same instructions on multiple files by loading and unloading Word itself - which would be the case if you did it with repeated startup macros using /m - seems outrageously inefficient. IMO, this route is asking for memory problems. It is like hanging a "Kick Me" sign onto Word.

Not that it needs one......

As the OP has not been forthcoming, or appear to have interest in following this up, I for one consider this thread "solved".

Oorang
01-02-2008, 02:20 PM
Thank you for sharing your perspective, it always good to look at an issue from more than one angle.