PDA

View Full Version : Exporting code modules via code...



Frosty
03-19-2012, 01:28 PM
Anyone have any tricks or code for this process? I've never developed it, and was hoping for an initial leg up. So, does anyone have some code which will export all the code modules (forms, modules and classes) in a project without having to manually click on each one within the VBA window?

Cheers in advance!

macropod
03-19-2012, 01:54 PM
You could use code like:
Sub ExportModule()
' Delete any old copies of the module
On Error Resume Next
Kill ("Module1.bas")
On Error GoTo 0
' Export Module 1
ActiveDocument.VBProject.VBComponents("module1").Export ("Module1.bas")
End Sub
Do note that you'll need trusted access to the Visual Basic Project. See: http://support.microsoft.com/kb/282830

For going the other way, see: http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/6754e433-41aa-42fd-ad28-07653d8f580b

Frosty
03-19-2012, 02:28 PM
Perfect... thanks. Will post in this again with the actual code solution to my "real" problem
(wanting to export all the components of two different projects, and run redlines between them)
Not to be a "Lucy" -- but I didn't want to ask for the whole kit and kaboodle since I know that's a bit of work to do... :)

Frosty
03-22-2012, 12:19 PM
Well, here's a little project which may help someone somewhere-- so I post it as-is, no implied warranty, etc :) It's free. But it could be helpful to someone else, as it basically allows the ability to redline code differences between two projects.

NOTE: this is really for the advanced developer. I will do my best to answer any questions, but no guarantees. Basic structure is as follows:

3 modules: Compare, Export, Main.
1 Button on the QAT, which runs the code to export all code from two projects, as well as redline those two projects.
1 Class: myFSO (really just an easier way for me to use the FileSystemObject)

You probably would want to turn off the warning in Word before saving/printing/emailing a document with track changes, as this uses native redline functionality.

This was developed in Word 2010. Some adaptation would be required for Word 2003 (or 2002-- I'm looking at you Fumei!), although those modifications would be relatively minor, and relegated to the fRedlineADoc function, I *think*

Any comments or improvements would be more than welcome. I'm kind of an open source guy at heart :)

Cheers, and thanks again to Macropod for the quick leg up. Hadn't played around with the VBProject object before. Very useful on this thing, which I've been meaning to write for a long time, and finally had a real reason to do so.

- Frosty/Jason

Talis
03-22-2012, 12:42 PM
As someone with Word 2003 may not be able to open the .docm unless they've got the compatibility pack from MS installed I'm posting a version as .doc. It lacks the QAT.

fumei
03-22-2012, 11:41 PM
What exactly do you mean by redline the projects?

BTW: does NOT work with 2002 (my version). wdGranularityCharLevel is not valid. What the hell is this anyway??



Oh. never mind. Not a bad idea. Although it seems this was added in 2007. Talis, are you sure this works in 2003?

fumei
03-23-2012, 12:16 AM
Actually it fails on at least five variables (wdCompareDestinationNew does not exist in 2002, and also appears to have been added for 2007).

Frosty
03-23-2012, 07:06 AM
Yes, the redline part will have to be completely modified. I'd record a macro of redlining two documents. Apart from the additional arguments available, I believe the 2003 version of compare documents does not return a document object, so that part has to be approached slightly differently.

I haven't needed to use it on 2003 projects yet, but I will need to at some point. Was just offering up the current state, if this was something interesting to anyone.

Years ago, when I was working on a team of programmers, we used visual sourcesafe and checked out individual modules of a global addin. My work isn't so structured anymore, but there are still times when I want to redline multiple code modules to make sure all my updates are going to the right places.

That's the genesis of this

Talis
03-23-2012, 11:20 AM
Talis, are you sure this works in 2003?

No, it's there just so the code could be read. Sorry, I should have made that clear.:hide:

Frosty
03-26-2012, 01:52 PM
Here's a version that *mostly* works in 2003, including a number of additions I've made. I don't have an XP version to test, but I'm curious to know if it works.

For 2003, it contains a limitation/bug: it seems you need to have the confirm conversions dialog prompt in order to tell Word not to choose "Encoded Text" but rather "Plain Text" when generating a comparison. I'm open to ideas. I have not coded any work-around for this (like changing the extension to ".txt" or something before opening) as I'm interested in someone else's idea before doing that.

There are two macros accessible from the Tools > Macro > Macros... dialog.
UI_AdvancedCompare
UI_AdvancedCompare_NoUserInterface

In one you'll see where I'm going with the user interface, if you use the NoUI version, then it will simply grab the file paths from the table in the document.

The best way to test this would be to do the following:
1. Create a new document in word.
2. Add a basic module to it, and put in a sub routine with some "Hello World" type stuff.
3. Save it.
4. Do a Save As.
5. Modify the sub routine slightly.
6. Save again, and close.
7. Modify the table in the attachment to point to the files you just created for Original and Revised, and then type in a path for Redline (you can leave the two export paths alone, if you want).
8. Run the UI_AdvancedCompare_NoUserInterface from the Macros menu.

This is still a work-in-progress... but maybe this would be interesting to... someone. Fumei? ;)