Visual Basic (VB6 and VBA)

Copyright 1999-2001 Christopher Greaves. All rights reserved.
Home Page and email to [email protected]
If in doubt, record a macro and inspect the entrails!

 

Visual Basic Library

This page was last updated on Saturday, November 24, 2001

Public Function blnSearchProcedureFor(strAr() As String, lngId As Long) As Boolean ' Procedure : blnSearchProcedureFor ' Description: Search a Procedure for the use of an identifier. ' Locate any occurrence of the identifier at a line in the Procedure OTHER THAN the definition line ' Copyright: Chris Greaves Inc. ' Inputs: STRING symbol table, LONG index to line-of-declaration. ' Returns: TRUE if a use of the identifier is found. ' Assumes: None. ' Side Effects: None. ' Tested: By the calls shown below. ' A handy function if ever you decide to create a cross-reference table for Procedures. ' It is easy enough to find the line which defines an identifier: ' You parse lines that start DIM and so on. ' In order to see if the identifier is used, you need to find a place OTHER THAN ITS DEFINITION ' where the identifer is referenced. ' The symbol table is a 4-column table, defined as: ' dim strTable(3,100) (if you have 100 identifiers ' The column(2,) contains the identifier ' The column (3,) holds a string representation of the line number at which the identifier is declared. ' Please see the test module for a sample setup of this table. ' Search the Procedure for the USE of the data identifier. blnSearchProcedureFor = False ' default result is "Not Found" ' case-insensitive searches. Dim strId As String strId = UCase(strAr(2, lngId)) Dim objModule As VBComponent For Each objModule In ActiveDocument.VBProject.VBComponents If objModule.Name = strAr(0, lngId) Then ' we are at least in the module that holds our procedure Dim lngLine As Long Dim lngStart As Long ' first line of the procedure definition lngStart = objModule.CodeModule.ProcStartLine(strAr(1, lngId), vbext_pk_Proc) Dim lngEnd As Long ' last line of the procedure definition lngEnd = lngStart + objModule.CodeModule.ProcCountLines(strAr(1, lngId), vbext_pk_Proc) ' Since we are searching within a procedure, ' we restrict ourselves to the lines of the procedure. For lngLine = lngStart To lngEnd If InStr(1, UCase(objModule.CodeModule.Lines(lngLine, 1)), strId) > 0 Then ' We have found the identifier If lngLine = strAr(3, lngId) Then ' it is the definition Else blnSearchProcedureFor = True Exit Function End If Else End If Next lngLine ' Not found here, so we may as well exit. Exit Function Else End If Next objModule 'Sub TESTblnSearchProcedureFor() 'Dim strAr(9, 1) As String 'strAr(0, 0) = "U" ' module 'strAr(1, 0) = "AddStyle" ' procedure 'strAr(2, 0) = "oLstTemplate" ' identifier 'strAr(3, 0) = "188" ' startline " NOTE you must change this value depending on the current position of the procedure AddStyle. 'strAr(4, 0) = "239" ' endline " NOTE you must change this value depending on the current position of the procedure AddStyle. 'strAr(5, 0) = "" 'strAr(6, 0) = "" 'strAr(7, 0) = "Y" 'strAr(8, 0) = "" 'strAr(9, 0) = "" 'MsgBox blnSearchProcedureFor(strAr, 0) ' should yield TRUE 'End Sub End Function

We all knew nothing when we started …
Home Page and Contact Information
Send email to [email protected].
This page was last updated Monday, November 19, 2001

 

 

Hosted by www.Geocities.ws

1