PDA

View Full Version : [SOLVED:] Sub to export modules and user forms to hard drive.



hkeiner
10-25-2010, 02:11 PM
How do I write a sub that will export specific named modules and userforms from an active Word 2007 document to my hard drive as BAS and FRM files?

I know how to export these components manually, but I have not figured out how write a sub to do. My overall objective is to have a macro that will export these modules and userforms with one easy click on the QAT.

These are some particulars:
- The names of the active document may vary but they all have these particular modules and userforms.
- The names of the modules are always the same (e.g. mModule1 and mModule2)
- The names of the userforms are always the same (e.g., fUserForm1, fUserform2)

Thanks in advance any advice.

gmaxey
10-25-2010, 08:07 PM
Sub Export()
Dim oComp As VBComponent
For Each oComp In ActiveDocument.VBProject.VBComponents
If oComp.Type = vbext_ct_StdModule Or oComp.Type = vbext_ct_MSForm Then
Select Case oComp.Name
Case "mModule1", "mModule2"
oComp.Export "C:\" & oComp.Name & ".bas"
End Select
End If
Next
End Sub

Create another case for the form names.

hkeiner
10-28-2010, 09:56 AM
Thanks for the above code. I took me a while to implement but it works perfectly. It exports multiple modules to a single directory using each module's name for automatically naming exported BAS files. This is exactly what I asked for and needed. This sub is quite sophisticated for my current level of VBA skill and I would not have figured this out on my own.

In some situations, I would like to use a less complex constructed sub to export only one specified module to a specified location using my own specified name for the BAS file. I wrote the below sub (using your sub as a guide) but without success. The export command fails. For my example, the ExportSingleModule sub is on the Normal template, the module is located on the active document and is named "Module1" , and the export destination is "C:\ModuleLibrary\ProjectModule.bas")

As you may tell, I am a newbie at VBA but I do learn well from examples. Any advice would be appreciated.



Private Sub ExportSingleModule()
Dim GetDoc As Document
Dim vbCom As Object
Set GetDoc = ActiveDocument
Set vbCom = GetDoc.VBProject.VBComponents
On Error GoTo ErrorMessage
Module1.VBProject.VBComponents.Export "C:\ModuleLibrary\ProjectModule.bas"
Exit Sub
ErrorMessage: MsgBox "The export failed"
End Sub

gmaxey
10-28-2010, 11:32 AM
Private Sub ExportSingleModule()
'Note: Add A reference (Tools>References) to Microsoft Visual Basic for Applications Extensibility
Dim vbCom As VBComponent
Set vbCom = ActiveDocument.VBProject.VBComponents("Module1")
On Error GoTo ErrorMessage
vbCom.Name = "ProjectModule" 'Rename componont so the exported module will have the name you want
vbCom.Export "C:\ProjectModule.bas"
vbCom.Name = "Module1" 'Reset the name
Exit Sub
ErrorMessage: MsgBox "The export failed"
End Sub

hkeiner
10-28-2010, 01:36 PM
'Note: Add A reference (Tools>References) to Microsoft Visual Basic for Applications Extensibility


Great code. It worked perfectly as long as the reference to "Microsoft Visual Basic for Applications Extensibility" has been added. I understand that this needs to be done only once manually on a users PC. However, most of my users will likely not have this reference added on their PCs and I will have to do this for them before the sub will run successfully on their PCs. Also, I have occassional new users that I would have to watch out for.

Is there a way for me to automate adding this reference to a users PC (perhaps at the part of the ExportSingleModule sub itself)? I ask because the Word documents that will contain the Export sub will be used by many users and I may not be able to ensure that the "Microsoft Visual Basic for Applications Extensibility" has been added on their PC. Alternatively. can a different sub be written to accomlish the export task using only existing regular installed (added) references and without the addition of the "Microsoft Visual Basic for Applications Extensibility" reference?

Sorry for piling on additional questions, but each great solution seems to bring up more questions. Plus the answers you have been providing have been helping me learn a lot of the the techinques/pitfalls that are not so obvious when I read my VBA manual.

gmaxey
10-28-2010, 02:09 PM
You don't really need the reference it just makes it easier to write the code (i.e., you can see the autolist members after typing the period after the vbCom)

You can add it programatically like this:


Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Application.VBE.ActiveVBProject.References.AddFromGuid "{0002E157-0000-0000-C000-000000000046}", 5, 3
End Sub

Or just delete the reference and declare vbCom as Oject


Dim vbCom as Object

hkeiner
10-29-2010, 11:05 AM
Dim vbCom as Object

Thanks for explaining the alternative for the Dim command. Using "Object" instead of "VBComponent" works fine for me without the need for adding a reference to Microsoft Visual Basic for Applications Extensibility.

Your tip also explains why I was having trouble with some other code I copied from other forum posts that would result in errors for me. I didnt' know why this was happening but now I see now that they contained special variables from Microsoft Visual Basic for Applications Extensibility ( I guess that is what you would call VBComponent, for example).