Export all VBA modules under Word

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Export all VBA modules under Word

Post by Robie »

Is there a way of exporting *all* modules, forms, class modules, etc. from a template to a folder? I am fed up of doing them one at a time.

Thanks.
Robie

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Export all VBA modules under Word

Post by HansV »

Try this:

Code: Select all

Sub ExportAll()
    Dim strPath As String
    Dim vbc As Object
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show Then
            strPath = .SelectedItems(1)
            If Right(strPath, 1) <> "\" Then
                strPath = strPath & "\"
            End If
            For Each vbc In ActiveDocument.VBProject.VBComponents
                vbc.Export strPath & vbc.Name & ".txt"
            Next vbc
        Else
            MsgBox "No folder specified...", vbExclamation
        End If
    End With
End Sub
You can change ActiveDocument to ActiveDocument.AttachedTemplate, for example.
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Export all VBA modules under Word

Post by Robie »

HansV wrote:Try this:

Code: Select all

Sub ExportAll()
    Dim strPath As String
    Dim vbc As Object
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show Then
            strPath = .SelectedItems(1)
            If Right(strPath, 1) <> "\" Then
                strPath = strPath & "\"
            End If
            For Each vbc In ActiveDocument.VBProject.VBComponents
                vbc.Export strPath & vbc.Name & ".txt"
            Next vbc
        Else
            MsgBox "No folder specified...", vbExclamation
        End If
    End With
End Sub
You can change ActiveDocument to ActiveDocument.AttachedTemplate, for example.
:clapping: That is fantastic Hans. Works like a charm and saved me a lot of time exporting each individual modules.

Thank you so much. :thankyou:

Ivka
NewLounger
Posts: 1
Joined: 11 Feb 2018, 18:47

Re: Export all VBA modules under Word

Post by Ivka »

Hans, thank you soooo much for your code!
I've been looking for it for several days! It's cool (and so are you) !

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15814
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Export all VBA modules under Word

Post by ChrisGreaves »

Robie wrote:Is there a way of exporting *all* modules, forms, class modules, etc. from a template to a folder? I am fed up of doing them one at a time.

Thanks.
Robie
Hi Robie.
Might one ask why you want to do this?
Cheers
Chris
I’ve been tidying the junk out of my shed for five years, and now can hardly get into the shed

User avatar
Charles Kenyon
5StarLounger
Posts: 641
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Export all VBA modules under Word

Post by Charles Kenyon »

ChrisGreaves wrote:
Robie wrote:Is there a way of exporting *all* modules, forms, class modules, etc. from a template to a folder? I am fed up of doing them one at a time.

Thanks.
Robie
Hi Robie.
Might one ask why you want to do this?
Cheers
Chris
Don't know about Robie, but it is a quick way to back up the vba or transfer it. I have hundreds of macros I store in my normal.dotm in more than 10 modules. For the most part, they are never used from within the Word UI, but rather serve as the basis for code in other templates. Most are designated as "private" because I don't want them showing up in the macro lists.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15814
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Export all VBA modules under Word

Post by ChrisGreaves »

Charles Kenyon wrote:... but it is a quick way to back up the vba or transfer it. ...
Agreed.
But I'd use Rob Bovey's Code Cleaner for such a simple task.

I was curious as to whether Robie had a greater need that might be solved by existing utilities.

Cheers
Chris
I’ve been tidying the junk out of my shed for five years, and now can hardly get into the shed

User avatar
Charles Kenyon
5StarLounger
Posts: 641
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Export all VBA modules under Word

Post by Charles Kenyon »

ChrisGreaves wrote:
Charles Kenyon wrote:... but it is a quick way to back up the vba or transfer it. ...
Agreed.
But I'd use Rob Bovey's Code Cleaner for such a simple task.

I was curious as to whether Robie had a greater need that might be solved by existing utilities.

Cheers
Chris
Does it work in Word?

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15814
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Export all VBA modules under Word

Post by ChrisGreaves »

Charles Kenyon wrote:Does it work in Word?
Well, yes.
At least, I have a version that works in MSWord2003. I have made it kick in whenever a version of my template changes.
e.g. Going from Playr017.dot to Player018.dot, it asks me if i want to export/import the code (i usually do!).

''' VBA Code Cleaner 4.4 © 1996-2001 by Rob Bovey,
''' all rights reserved. May be redistributed for free but
''' may not be sold without the author's explicit permission.


Right now I can't find a copy on the web, but if you want a version of my downloaded Word copy PM me.
Cheers
Chris


P.S. One of these links might work.
I’ve been tidying the junk out of my shed for five years, and now can hardly get into the shed

User avatar
Charles Kenyon
5StarLounger
Posts: 641
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Export all VBA modules under Word

Post by Charles Kenyon »


User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15814
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Export all VBA modules under Word

Post by ChrisGreaves »

Charles Kenyon wrote:Found one here.
Thanks Charles.
The one you found is (a) for Excel and (b) is a COM rather than an addin and (c) is for 232-bit only, right?

I am using one written in WordVBA, with the source visible - and hence adjustable - that runs in what I think is a 32-bit Word 2003. (Although Hans is waiting to set me straight on that claim(grin))

Cheers
Chris
I’ve been tidying the junk out of my shed for five years, and now can hardly get into the shed

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Export all VBA modules under Word

Post by HansV »

There is a Word Code Cleaner available at the bottom of the following page. It is based on the one for Excel.

Combatting Template Bloat

The page says that it works in Word 97, 2000 and 2002, but although I haven't tested it, I see no reason why it wouldn't work in more recent versions.
Best wishes,
Hans

User avatar
Charles Kenyon
5StarLounger
Posts: 641
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Export all VBA modules under Word

Post by Charles Kenyon »

I've tried to get Bill Coan's Code Cleaner to work but haven't been able to get it to do so. It is an .exe file. I no longer remember how to adjust .com Add-Ins on menu versions of Word. I've asked Chris to send me the vba one that he has via private message.

User avatar
Jay Freedman
Microsoft MVP
Posts: 1320
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: Export all VBA modules under Word

Post by Jay Freedman »

Charles Kenyon wrote:I've tried to get Bill Coan's Code Cleaner to work but haven't been able to get it to do so. It is an .exe file. I no longer remember how to adjust .com Add-Ins on menu versions of Word. I've asked Chris to send me the vba one that he has via private message.
Charles, the Word Code Cleaner.exe file is just an installer made by (probably) PKZip. Rename it with a .zip extension and you can extract the contents: Word Code Cleaner.dot, Setup.doc, and Cleaner.hlp. All you really need is to extract the .dot file to the Startup folder, or use the Templates & Add-Ins dialog to add it to the current session. If you want to read the Cleaner.hlp file, you'll have to install the WinHlp program from https://support.microsoft.com/en-us/hel ... luded-or-h. To install it on Windows 10, you need the answer to https://answers.microsoft.com/en-us/win ... 8bed33265c.

User avatar
Charles Kenyon
5StarLounger
Posts: 641
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Export all VBA modules under Word

Post by Charles Kenyon »

Thank you Jay. I extracted it and the commands show up under the Add-In tab.
I converted it to .dotm and added a QAT button.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15814
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Export all VBA modules under Word

Post by ChrisGreaves »

Charles Kenyon wrote:...a QAT button.
:scratch:

Cheers
Chris
I’ve been tidying the junk out of my shed for five years, and now can hardly get into the shed

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Export all VBA modules under Word

Post by HansV »

QAT = Quick Access Toolbar, a feature in the ribbon version of Office applications.
S2066.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15814
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Export all VBA modules under Word

Post by ChrisGreaves »

HansV wrote:QAT = Quick Access Toolbar, a feature in the ribbon version of Office applications.
:thankyou:

Oh.
One of the features in this new-fangled version of MS-Word.
Ta ever so.
I’ve been tidying the junk out of my shed for five years, and now can hardly get into the shed

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Export all VBA modules under Word

Post by Robie »

ChrisGreaves wrote:
Robie wrote:Is there a way of exporting *all* modules, forms, class modules, etc. from a template to a folder? I am fed up of doing them one at a time.

Thanks.
Robie
Hi Robie.
Might one ask why you want to do this?
Cheers
Chris
Sorry, coming bit late to this. Have been away and not well after the holidays.

I only wanted a copy of the modules so that I could email them (which we are not allowed to do by the company) to myself (when working from home).

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Export all VBA modules under Word

Post by HansV »

I hope you're better now!
Best wishes,
Hans