问题
How can I catalog, index, and/or print VB6 source code with each sub or function on its own page..? I would like to do so with free or included Visual Studio Add-Ins if possible, but I'm not adverse to writing something myself. I'm familiar with "Microsoft Office Visual Basic for Applications Extensibility", and it seems that VB6 has a similar module, which may allow me to simply "For Each" through the code module collection and throw the subs at the printer one at a time. It would take maybe 10-15 lines of code.
My first priority is the printing, preferably with each sub/function on its own page, but with the normal print function of the IDE, all the code runs together in one long printout. Next after that, I would like to create an index/toc of the names of every sub, function, variable, and constant in each VBP. We have the Visual Studio 6.0 Enterprise Edition, but there does not seem to be anything in it to do any of these kinds of things.
You may laugh and ask, "Why VB6..?? LOL!!". It's because I've been tasked with upgrades and changes to VB6 source code of a large software system that runs a factory. It's on an isolated network with no connection to the outside world, and it's been running fine for 14 years, but now they want to start upgrading some things. The system is composed of many VBP files, each with many modules and forms.
Edit: I did try Googling for answers to this, but that proved impossible. All I got was coded samples about printing from apps written in VB6, not printing the source code from the IDE.
回答1:
You don't need to properly parse the code to meet your requirements. Write something yourself. You could output to a printer or to HTML files. I did this once, it worked fine.
The source code files are just text files. Read the files line by line, printing each line. Start a new page every time you see "end sub" or "end function" or "end property". That way each method will start on a separate page. FRM files have control definitions at the top, you can skip that by just looking for a line containing only "end". The code starts there.
If you are doing a lot of VB6 I also recommend getting the brilliant add in MZ Tools. It has excellent search tools. Unfortunately no longer free, but worth the money IMHO. I have no links with the vendor.
回答2:
Write an Add-In. It is far easier than you might suspect.
Get ahold of the book "Developing Visual Basic Add-Ins" by Steven Roman (O'Really, 1999, ISBN 1-56592-527-0).
回答3:
Parsing the module for procedures and functions is hard, believe me. Particularly with line continuations like this:
_
Public _
Sub _
Foo() ' _
End Sub
_
End _
_
Sub _
Furthermore, a Property can be ended with End Property
, but also with End Function
or End Sub
Fortunately, the VBIDE for VB6 has more classes and methods for working with VB projects than the VBA version of VBIDE.
One of those is the CodePane.Members
property that returns a collection of all of the identifiers (although, I think it omits Type
and Enum
identifiers, but they're declared in the declarations section of a module anyway) in a module.
And each member exposes various properties including it's location in the module:
If you're interested in parsing in more detail, and in a fully-featured add-in for the IDE, you should take a look at Rubberduck-VBA on GitHub (I'm a contributor). It's currently working with VBA hosts, but VB6 is on the road-map. It has one of the most robust VB parsers available, and it's open-source.
回答4:
You might consider using a software code search tool that indexes the langauge elements for fast lookup and retrieval. This would make it easier for you to find code of interest, and even avoid printing all that paper because you simply look up the code instead.
Our Source Code Search Engine knows the lexical elements of many languages, specifically including VB6. It read the source code base (thousands of files) and constructs an index to aid its finding code elements fast. Using its GUI, you can write queries in terms of language elements to look for code fragments such as:
S=*Login*
to find strings (only) that contain the text "Login" (using simple WildCards), or
'SUB' I=/[Aa]ccount.*/
which will find all subroutines whose name starts with "account" regardless of first letter capitalization (full regular expression) or
'IF' ... N>17<35
to find IF statements that use any value from 17 to 35 (".." means "near"). The N query element lets you find any use a constant quickly and easily.
A really powerful property of the query language is "and not". This allows you to build a sloppy query that finds "too much" and then eliminate the "too much" part:
I '+' I - I=xyz
finds two added variables that do not include the variable xyz.
You can click on any of the list of hits, and the text of the code comes up in the GUI window. No paper necessary.
Because it knows the lexical structure of each langauge, it is not confused by whitespace, linebreaks, comments or even strings containing code like text.
来源:https://stackoverflow.com/questions/41031455/how-can-i-catalog-index-and-or-print-vb6-source-code-with-each-sub-function-o