Printing VBA code to a Word file?

I

Ian

Using Word97.

I want to save all the VBA code of a template into a Word document, so I
can massage the layout for a presentation. (For example, I want to
introduce margins, headers and page numbers).

In the VBA Explorer window I have selected the top of the template tree,
then clicked File > Print and checked "Current Project" and "Print to
File". Whatever options or setup I try, I always get a PostScript file.

Q1. Is there any way of saving all of the VBA code as a plain text file?

Q2. Why does Word let me select a printer (and its setup) when I have
elected to save to a file?
 
M

Martin Seelhofer

Hi Ian
Q1. Is there any way of saving all of the VBA code as a plain text file?

You might be better of using VBA to achieve this ;-)

Here's some code:

' Creates a new doc with all VB-code of the given template object
Sub CopyVBCodeToNewDocument(tmpl As Template)
Dim newDoc As Document
Dim vbcomp As VBComponent
Dim code As CodeModule

Set newDoc = Documents.Add

With newDoc.Range
For Each vbcomp In tmpl.VBProject.VBComponents
If vbcomp.Type <> vbext_ct_ActiveXDesigner Then
.InsertAfter "Module: " & vbcomp.name & vbCr
Set code = vbcomp.CodeModule
.InsertAfter code.Lines(1, code.CountOfLines) & _
vbCr & vbFormFeed
End If
Next
End With
End Sub

To actually use the above procedure, call it giving a template
reference as an argument. An example for this is given in the
following Sub:

Sub CopyVBCodeToNewDocumentTest()
Call CopyVBCodeToNewDocument(ActiveDocument.AttachedTemplate)
End Sub



Cheers,
Martin
 
T

Tom Winter

Ian said:
Using Word97.

I want to save all the VBA code of a template into a Word document, so I
can massage the layout for a presentation. (For example, I want to
introduce margins, headers and page numbers).

In the VBA Explorer window I have selected the top of the template tree,
then clicked File > Print and checked "Current Project" and "Print to
File". Whatever options or setup I try, I always get a PostScript file.

Q1. Is there any way of saving all of the VBA code as a plain text file?

Q2. Why does Word let me select a printer (and its setup) when I have
elected to save to a file?

For question one, you can try my VBA-To-Word add-in. It is an add-in to VBA
that can convert your VBA source code into nicely formatted Word documents:

http://www.amosfivesix.com/downloads/

As for question two, saving the file to postscript is what that option does.
You are basically saving a bunch of printer commands to file. That's why you
get to select a printer - it's saving commands that the selected printer
understands. This options is basically useless for what you want to to.

-Tom
(e-mail address removed)
www.AmosFiveSix.com
 
C

Charles Kenyon

Right click on your modules and Export them to files. These files have a
..bas extension but they are text files and can be opened in Notepad or Word.
(UserForms and class modules have different extensions but I believe they
still are text files.) Or, you can copy and paste from the VBE into a Word
doc. I would use the latter method for a single macro or module and the
export method for more.
--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
I

Ian

Thanks, Tom. I'll take a look.

--
Ian

Tom Winter said:
For question one, you can try my VBA-To-Word add-in. It is an add-in to VBA
that can convert your VBA source code into nicely formatted Word documents:

http://www.amosfivesix.com/downloads/

As for question two, saving the file to postscript is what that option does.
You are basically saving a bunch of printer commands to file. That's why you
get to select a printer - it's saving commands that the selected printer
understands. This options is basically useless for what you want to to.

-Tom
(e-mail address removed)
www.AmosFiveSix.com
 
I

Ian

Thanks Charles. I see what you mean, but what I really wanted was to do
the job in one go, rather than exporting dozens of modules by hand one
by one.
 

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