Automate compiling a template?

D

DuncanL

As part of a software build process, I need to compile some VBA in a Word
template.

Currently we have to manually open the .dot file, open VBA (Alt+F11), select
Compile from the debug menu, save the template and exit.

Using Visual Build Pro I can script the opening, save and close using
VBScript - eg:

---------------
Dim oWord
Dim oDoc

Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Open("C:\SomePlace\SomeFile.dot")

' xxxx Do Compile here? xxxx

oDoc.Save
oDoc.Close

oWord.Quit

Set oDoc = Nothing
Set oWord = Nothing
 
Y

yves

Hi Duncan,

there's a "Debug/compile" menu in VBA, but "Compile" is perhaps
misleading if you come from another language. The chief purpose of
"Compile" here is to check the code for errors.

Word DOT templates that contain VBA code do not need to be compiled;
actually, they are never compiled. The source code you write will
always remain "as is". Password protection is no real protection,
since there are cracks, and OpenOffice can reveal VBA code too, AFAIK.

Worse. if you create a new project, add code and save it, the
resulting DOT will have a size of X. If you "compile" before saving,
the size will be roughly 2X (twice as large). VBA compilation bloats,
with no apparent gain that I know of, apart from code verification.
But I can be proven wrong.

Digitally signing a project forces "compilation" and bloating, a side
remark for what it's worth.

Hope this helps. There may be more expert advice herewith.

- Yves Champollion

IMHO, and unless I am mistaken, you can skip that "Compile" step,
unless you need to make sure the code does not contain errors.
 
D

DuncanL

Yves,
there's a "Debug/compile" menu in VBA, but "Compile" is perhaps
misleading if you come from another language. The chief purpose of
"Compile" here is to check the code for errors.

It's not that it has to be compiled in the traditional sense; the problem
is that the code in the template has references (early binding) to other code
that may change, so needs to be "compiled" to refresh the references. In an
ideal world we would change the code to do late binding; but that isn't
possible at the moment, so I have to work around what we have.

Basically I just want to be able to do from VBScript code what I currently
have to do by hand - open the template, "compile" it and save it again.

I can do the open/save, it's just automating the compile that confuses me.

Thanks for the help though!

Duncan
 
D

DuncanL

Answering my own question, for anyone still interested...
Is there a way to do the compile step from automation?

I have a solution that, while not optimal, works well enough for now -
though it does just involve programmatically pressing the right keys!

-----------------------------------
With oWord
.Visible = True
.Application.Activate
WScript.Sleep 500 'Various sleeps to slow process down, otherwise VBA won't
have time to do it's thing
.Wordbasic.SendKeys "%tmv" ' Tools -> Macros -> VBA
WScript.Sleep 500
.Wordbasic.SendKeys "%dl" ' Debug -> Compile
WScript.Sleep 1000
......
-----------------------------------

This isn't ideal, since it requires Word to be open as a visible window,
which means it cannot run as part of an automated script without the user
being logged on.

So, if anyone has a way of "compiling" a Word template without resorting
to SendKeys, I'd love to hear it! Thanks.


Duncan
 

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