Getting source code from Normal.dot

R

Rhino

I've spent the last few days working on a series of interrelated macros;
basically, there is a main macro and it farms out lots of its work to lesser
procedures. All of this code is working well and is stored in Normal.dot.

Is there any way that I can get the source code from Normal.dot and write it
to a text file invisibly? I realize that I could do this job manually by:
- opening the Visual Basic Editor from my document
- clicking Edit/Select All
- clicking Copy
- opening a text file outside of Word
- pasting the clipboard into the text file
- saving the text file

But I will probably make several more minor changes to the macros before
they're absolutely finished so I'd like to find a way to get the source code
of the macros "automagically", ideally with a VBScript that invokes a macro
that writes all of the macros in Normal.dot to a specified text file.

Can anyone tell me what to do?

I can write the VBScript that will drive all of this but I'm really not sure
how to capture the contents of Normal.dot so that I can write it to a file.
Even if I just record a macro that does the manual steps that I've listed
above, I don't have any idea how to open a text file, paste the clipboard
and save the text file programmatically.

I just tried recording a macro that did all that but the only line in the
macro is 'ShowVisualBasicEditor = True' so that's obviously not going to do
the job.

I'm using Word 2002.
 
J

Jezebel

Use the Export function --

application.VBE.ActiveVBProject.VBComponents(2).Export "C:\MyModuule.txt"
 
J

Jay Freedman

Rhino said:
I've spent the last few days working on a series of interrelated
macros; basically, there is a main macro and it farms out lots of its
work to lesser procedures. All of this code is working well and is
stored in Normal.dot.

Is there any way that I can get the source code from Normal.dot and
write it to a text file invisibly? I realize that I could do this job
manually by:
- opening the Visual Basic Editor from my document
- clicking Edit/Select All
- clicking Copy
- opening a text file outside of Word
- pasting the clipboard into the text file
- saving the text file

But I will probably make several more minor changes to the macros
before they're absolutely finished so I'd like to find a way to get
the source code of the macros "automagically", ideally with a
VBScript that invokes a macro that writes all of the macros in
Normal.dot to a specified text file.

Can anyone tell me what to do?

I can write the VBScript that will drive all of this but I'm really
not sure how to capture the contents of Normal.dot so that I can
write it to a file. Even if I just record a macro that does the
manual steps that I've listed above, I don't have any idea how to
open a text file, paste the clipboard and save the text file
programmatically.

I just tried recording a macro that did all that but the only line in
the macro is 'ShowVisualBasicEditor = True' so that's obviously not
going to do the job.

I'm using Word 2002.

First, you need to go to Tools > Macro > Security, on the Trusted Sources
tab, and check the box for "Trust access to Visual Basic Project". Otherwise
your code will be denied access to the VBA projects.

Then, in the editor, go to Tools > References and check "Microsoft Visual
Basic for Applications Extensibility 5.3".

Finally, run this code (alter the dumpPath string as needed first):

Sub DumpNormalMacros()
Dim comp As VBComponent
Dim fn As String
Const dumpPath = "C:\temp\vba\"

For Each comp In VBE.VBProjects("Normal").VBComponents
With comp
Select Case comp.Type
Case vbext_ct_ClassModule, vbext_ct_Document
fn = dumpPath & comp.Name & ".cls"
Case vbext_ct_StdModule
fn = dumpPath & comp.Name & ".bas"
Case vbext_ct_MSForm
fn = dumpPath & comp.Name & ".frm"
End Select

.Export fn

End With
Next
End Sub

This automates what you can do manually: In the Project pane, right-click
each module and select Export File, and supply a name.

You can read these files into any template project by right-clicking a
module and selecting Import File.

The .cls, .bas, and .frm files are plain text that you can read with
Notepad. The .frm file is accompanied by a .frx file that contains the
binary data for the form.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
R

Rhino

Wow, that was a lot easier than I expected!

FYI, I had to go to Tools/Macro/Security and check the "Trust access to
Visual Basic Project" box on the Trusted Sources page before the Export
would work. Luckily, I'd seen Jay's reply as well as yours before trying
either solution :)

I'm just mentioning this in case anyone else sees this thread and finds that
the Export command doesn't work for them; they may not have had that box
checked either :)

Rhino


Jezebel said:
Use the Export function --

application.VBE.ActiveVBProject.VBComponents(2).Export "C:\MyModuule.txt"
 
R

Rhino

Jay Freedman said:
First, you need to go to Tools > Macro > Security, on the Trusted Sources
tab, and check the box for "Trust access to Visual Basic Project".
Otherwise
your code will be denied access to the VBA projects.

Then, in the editor, go to Tools > References and check "Microsoft Visual
Basic for Applications Extensibility 5.3".

Finally, run this code (alter the dumpPath string as needed first):

Sub DumpNormalMacros()
Dim comp As VBComponent
Dim fn As String
Const dumpPath = "C:\temp\vba\"

For Each comp In VBE.VBProjects("Normal").VBComponents
With comp
Select Case comp.Type
Case vbext_ct_ClassModule, vbext_ct_Document
fn = dumpPath & comp.Name & ".cls"
Case vbext_ct_StdModule
fn = dumpPath & comp.Name & ".bas"
Case vbext_ct_MSForm
fn = dumpPath & comp.Name & ".frm"
End Select

.Export fn

End With
Next
End Sub

This automates what you can do manually: In the Project pane, right-click
each module and select Export File, and supply a name.

You can read these files into any template project by right-clicking a
module and selecting Import File.

The .cls, .bas, and .frm files are plain text that you can read with
Notepad. The .frm file is accompanied by a .frx file that contains the
binary data for the form.
Thanks, Jay! Your solution works as well as Jezebel's with the bonus of also
getting the .cls and .frm files. I don't actually have any forms at this
point so I'm not getting any .frm file and I'm not sure what to do with the
..cls file that I am getting but your solution is more complete and will be
very handy when and if I start using forms.

By the way, what was the point of adding that VBA manual to my
Tools/References?
 
J

Jean-Guy Marcil

Rhino was telling us:
Rhino nous racontait que :

By the way, what was the point of adding that VBA manual to my
Tools/References?

The "VBComponent" type is not part of the Word VBA Object library.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Rhino

Jean-Guy Marcil said:
Rhino was telling us:
Rhino nous racontait que :



The "VBComponent" type is not part of the Word VBA Object library.
Ahh, so installing the reference manual also installs code into VB? That's
unexpected!

Thanks for explaining that.
 
J

Jean-Guy Marcil

Rhino was telling us:
Rhino nous racontait que :
Ahh, so installing the reference manual also installs code into VB?
That's unexpected!

Well, actually, you are not installing a manual.
You are telling the Word VBA compiler that you are going to use some objects
from a library that is not loaded by default, so you loaded it yourself.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jay Freedman

Jean-Guy Marcil said:
Rhino was telling us:
Rhino nous racontait que :


Well, actually, you are not installing a manual.
You are telling the Word VBA compiler that you are going to use some
objects from a library that is not loaded by default, so you loaded
it yourself.

Hi Rhino,

In case you aren't familiar with the term, "library" as Jean-Guy used it
means a file of compiled code, usually in a "dynamic link library" or DLL.
In the References dialog, when you select the item "Microsoft Visual Basic
for Applications Extensibility", the bottom of the dialog shows you which
DLL it's pointing to (or at least it would, if somebody at MS had the
foresight to make the label area larger so the path wasn't truncated):

C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6.DLL

This does have the side effect of enabling the Help topics for the objects
that live in the DLL. On a computer that uses the English version of
Windows, those topics are in the file ...\VBA6\1033\VBOB6.CHM. To get to
them easily in the editor, put the cursor on the name of any object, method,
or property that belongs to the VBE stuff and press F1.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
J

Jean-Guy Marcil

Jay Freedman was telling us:
Jay Freedman nous racontait que :
Hi Rhino,

In case you aren't familiar with the term, "library" as Jean-Guy used
it means a file of compiled code, usually in a "dynamic link library"
or DLL. In the References dialog, when you select the item "Microsoft
Visual Basic for Applications Extensibility", the bottom of the
dialog shows you which DLL it's pointing to (or at least it would, if
somebody at MS had the foresight to make the label area larger so the
path wasn't truncated):

C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6.DLL

This does have the side effect of enabling the Help topics for the
objects that live in the DLL. On a computer that uses the English

If there is a help file...
version of Windows, those topics are in the file
...\VBA6\1033\VBOB6.CHM. To get to them easily in the editor, put the
cursor on the name of any object, method, or property that belongs to
the VBE stuff and press F1.

Thanks Jay...

One day.. I'll be as thorough as you are!


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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