Link to home
Start Free TrialLog in
Avatar of Robert Berke
Robert BerkeFlag for United States of America

asked on

Export all vba modules from outlook vbaproject.otm

My Outlook vbaproject.Otm file has dozens of modules and classes. I want to export them periodically for a another project I am working on.

I found bunches of code that will automate export for Excel modules, but no luck for Outlook.

I even found a 2004 post from the famous Sue Mosher saying she did it manually when needed.

I have a solution but it requires several manual steps, but I hope somebody has an easier way.


My solution is to use Outlook IDE control P then selected Code and "Current Project". Setup used "Microsoft Print to PDF" with 11x15 landscape (long lines of code won't split).  

I also tried "print to file" but that did not seem to work.

After creating the 298 page PDF, I opened it with Foxit Reader and Saved as TXT.

The resulting TXT looks pretty good and a simple program could create multiple BAS modules. But before I do that I want to see if anybody here has already done something similar.

Each of my TXT pages start with 4 lines that look like this:

----------------------- Page 148-----------------------  top line has the PDF's page number
                                                                          2nd line is always blank.
mdlMicrosecondTimer - 8                                  3rd line has the module's name and page number
                                                                         4th line is always blank.
                                                                         subsequent lines have the actual code
        ReDim Preserve SwNextSlowestCase(0 To ixSegment)
        ixSegment = Ixsegment + 1
        etc
        etc
Avatar of Bill Prew
Bill Prew

Okay, so good news and bad news.

First the bad news:

The VBIDE object isn't exposed in the Outlook VBA object model.  This is for security sake, the thinking being that since Outlook is typically directly connected to the internet, and constantly processing foreign emails that could contain malware, allowing programmatic access to the VBIDE object could be a major exploit.  So no way to do it in code...

Now the good news:

With just a little manual effort you can get it exported all to one file.  Use the same technique you are using, but first, install a new printer driver.  Select from the Generic category, and pick the following driver.  Then print the top level of the project explorer and when prompted enter the name of a text file.

With a little creativity and trial and error you should be able to split that if needed, there are very minimal headers for each part that have "XXXXXX  -  nn" on a line, where XXXXXX is a module name, and nn a segment number.

~bp

User generated image
Avatar of Robert Berke

ASKER

That was a good idea which would eliminate several cumbersome steps, but I was disappointed.  


When I used Text Printer,  11 x 17 wasn't available so I chose Tabloid which is supposedly the same as 11 x 17.  Any line longer than 170 characters was word wrapped to the second line.
For instance  column 170  occurs right before  2)blah in the following line of code.
exit_routine:   msg = "This product has 2 features " & vbcrlf & "        1)  blah " & vbcrlf & "     2)blah blah"

When I used PDF, I chose 11 x 17 landscape and none of my lines of code word wrapped even when they were 210 characters long.

I suspect this is because the PDF printer supports proportional fonts and the generic printer does not.

This complicates the clean up program a little. I was worried that the generic printer might do an RTrim on the 170 lines, but that does not appear to be the case.  The following should probably work 99.9% of the time

 if len(thisline) = 170 and right(thisline) <> "" then
      thisline = thisline & Nextline
      discard Nextline
end if

Do you know of any Generic Text Printer  drivers that might support custom paper sizes?
With that driver I can get 165 on a line if that helps?

When printing, try these options.  You may be able to find an even wider paper than A3 in Landscape with a little research on all the ones listed, I don't know them all off hand.

~bp

User generated image
I should have mentioned that I had researched them all.  Tabloid is the largest and a3 is a little smaller.  You confirmed that when you said you got 165 and I got 170.

If somebody can find a alternative  generic text printer that allows custom forms, I would be thrilled.

It turns out my cleanup program cannot be simple.  Right now it screws up on this 3 lines of code
line1:  I = I +3714 '  comment that ends exactly at column 170
line2:  I = 2* I
line3:  I = I +3714 '  comment that ends in column 123line4:  I = 2* I

If I print that to generic text printer then review it with a hex editor there is no way tell if it is 3 lines of code or 4 lines of code

This is pathological, and 99.9% of the time my program would work, but I hate programs that almost work.

Instead, I am back to my original solution of a pdf printer. Plus, I just used Pdffactory to make a custom form  8.5 inches high by 40 inches wide which should never ever do word wrap.  99.99999% reliable I can live with.

The cumbersome steps only add about 3 minutes to my manual process. Seems worth it for a more reliable. solution
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fantastic !!  50 inches wraps at exactly 500 characters which is way more than enough.

On my windows 10 machine  I could not find "printer servers" under devices as printers.  Here is how I did it.

Start  > "Print management"  which opened a Microsoft console
expand "printer servers" which showed "cpu123" which is my laptop computer
Right click on cpu123 > properties
click on "Forms" tab  and followed you instructions
Forms tab
•I selected Folio existing form to start from
•Clicked on Create a New Form

•Changed form name to Folio 50x50
•Set the Width to 50in and height 50 in  (that way landscape vs portrait does not matter)
•Clicked OK button

Start  > devices and printers > add a printer >
Click "the printer I want is not listed"
Select "Add a local printer or network printer with manual settings" >next
Use an existing port:  FILE:(Print to file) > Next
Wait a minute while drop downs populate
Manufacturer: Generic
Printers: Generic / Text only
> Next > Use the driver that is currently Installed > next
I used "Print to file" as my printer name.


Pretty slick.  

Thanks a bunch
Thanks again.
The following is a more detailed version of my Feb 2017 response.

You probably know how to tell Excel, Word, and Outlook to print onto many different forms such as "Letter" or "Legal".

The following procedure shows how to add a new form name "Folio 50x50" that is useful for printing VBA code to a file. There are 3 main steps.

1) create a form named "Folio 50x50" which is 50 inches high and 50 inches wide.
2) create print driver name "Print to File".
3) print Outlook VBA code to a file using the "Print to File" printer driver and the "Folio 50x50" form.

Thereafter, the "Print to File" driver can be used to print from virtually any Windows application.


STEP 1) Create a form named Folio 50x50.


Start  > "Print management"  which opened a Microsoft console
expand "printer servers" which showed "cpu123" which is my laptop computer
Right click on cpu123 > properties
click on "Forms" tab  and followed you instructions
Forms tab
•Turn on "Create a New Form" then change Form Name to be "Folio 50x50"
•Set the Width to 50in and height 50 in  (that way landscape vs portrait does not matter)
•Clicked OK button

Step 2) Create a "Print to File" printer.

Please note, several of the follow steps will pause for up to a minute while dropdown menus are populated.

Start  > devices and printers > add a printer >   
(or on some version of windows) Start > Printers and Scanners > Add a Printer or Scanner

Click "the printer I want is not listed"
Select "Add a local printer or network printer with manual settings" >next
Use an existing port: FILE:(Print to file) > Next     < ===== THIS CORRECTS THE FEB 2017 SOLUTION.

Manufacturer: Generic
Printers: Generic / Text only  (sometimes you'll have several choices. I took the topmost.)
> Next > Use the driver that is currently Installed > next

I used "Print to file" as my printer.

step 3) Use the following steps to print the project

Open Outlook > F11 to vba > Ctrl P to open print dialog

Turn on "Current Project" and "Code" radio buttons then click "Setup".  (Do NOT turn on the "Print to File" checkbox)
Select "Print to file" from the printer Name dropdown
Select "Folio 50x50" from the Size dropdown.
Click Ok to start the print
Enter the full path for the desired output.  For instance c:\temp\printout.prn


Hope this helps future researchers.

rberke


When you print vba code to a file ofter a single 120 character line will print as if there were two lines


The following June 29, 2023 documentation is an improved version showing how to avoid line wrapping.
 
You probably know how to tell Excel, Word, and Outlook to print onto Printer Forms such as "Letter" or "Legal".
 
The following procedure shows how to add a new Printer Form name "Folio 50x50inch" that is useful for printing VBA code to a file.  There are 3 main steps.
 
Step 1) create a form named "Folio 50x50inch" which is 50 inches high and 50 inches wide.
Step 2) create print driver name "Print to 50x50inch file".
Step 3) print Outlook or Excel VBA code to a file using the "Print to 50x50inch file". printer driver.
 
Thereafter, any Windows application  can print to the new printer. (The results be great for .txt files and terrible for MS Word .doc files.)



 
STEP 1) Create a form named Folio 50x50Inch.
 
Start  > "Printers & scanners"  

Scroll down to "Related Settings" (on some Windows versions Related Settings is on the right side of the screen)


Click "print server properties" then click on "Forms" tab  

Turn on "create a new form" then change Form Name to be "Folio 50x50inch"

Set the Width and Height to 50in  (that way landscape vs portrait does not matter

Clicked OK button

User generated image
 

Step 2) Create a "Print to 50x50inch File" printer.
 
Please note, several of the following steps will pause for up to a minute while dropdown menus are populated.
 
Start  > devices and printers > add a printer >   
(or on some version of windows Start > Printers and Scanners > Add a Printer or Scanner
 
Click "the printer I want is not listed"
Select "Add a local printer or network printer with manual settings" >next
Use an existing port: FILE:(Print to file) > Next 
 
Manufacturer: Generic
Printers: Generic / Text only  10.0.16299.15  (if there are other choices use the first.)
> Next > Use the driver that is currently Installed > next
 
Start > devices and printers > click on previously created “Print to 50x50inch file” 

On windows 10 & 11 Manage > Printing Preferences

On windows 7:   Right click > Printing Preferencs

 

Select: Portrait > Advanced > Paper size: Folio 50x50inch  > Ok > Ok


 
step 3) Use the following steps to print the project
 
Open Outlook or Excel > F11 to vba > Ctrl P to open print dialog
 
Turn on "Current Project" and "Code" radio buttons then click "Setup".  (Do NOT turn on the "Print to File" checkbox)
Select "Print to Folio 50x50inch file" from the printer Name dropdown
Select "Folio 50x50inch" from the Size dropdown.
Click Ok to start the print
Enter the full path for the desired output.  For instance c:\temp\printout.prn

 

Please note, the new printer will do a terrible job of printing  MS Word documents and excel spreadsheets.
 
 
Hope this helps future researchers.
 
rberke