|
|
Chapter 5: VBE- The Code Modules A project is a collection of objects, s.a. UserForms, and the VBA component of a document or template. VBA code modules are stored inside the Visual Basic Editor (VBE) which can be accessed by:
In the upper left corner of the VBE window is the Project Explorer, which you use to navigate among open projects. In the lower left corner you'll find the Properties Window, which shows the available properties of the selected object or module. The main area of the VBE is the Code Window, where you write/store the code for the selected object (module or UserForm). A UserForm is a custom dialog box. Each macro begins with a Sub and ends with End Sub with the VBA code in the middle. Below you will see how the various modules are laid out in the MWEP:
The Project Explorer gives you a tree-structured view of all the files inserted into the application. You can expand these and collapse branches of the views to get more or less detail. The VBE window displays forms, modules or other separators. If you want to select a form on its own simply double click on the project explorer window for a more detailed look. Modules make it easier to follow the flow of the application. A module has code that resembles a Word document in the way the script is organizational and typed. Once completed, these procedures can be run to control your Word project. The 3 main types of modules used in the Word EMR Project are:
Here are the actual global and class modules used in the MWEP: AutoMacrosHere I placed all of the toolbar module codes that can later be selected to activate commands when toolbars are clicked. Here are the first few lines of code: Option Explicit ' The following would be in the BrowseFolder function or class Private Type BROWSEINFO hOwner As Long pidlRoot As Long pszDisplayName As String lpszTitle As String ulFlags As Long lpfn As Long lParam As Long iImage As Long End Type Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _ "SHGetPathFromIDListA" (ByVal pidl As Long, _ ByVal pszPath As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _ "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _ As Long Private Const BIF_RETURNONLYFSDIRS = &H1 Sub autoexec() On Error GoTo Proc_Error Load frmSplashscreen frmSplashscreen.Show Proc_Exit: Exit Sub Proc_Error: Call EMRError("WORD EMR Project.dot/Module AutoMacros: AutoExec()", Err.Number, Err.Source, Err.Description) Resume Proc_Exit End Sub Public Function BrowseFolder(szDialogTitle As String) As String Dim X As Long, bi As BROWSEINFO, dwIList As Long Dim szPath As String, wPos As Integer, XX As String With bi .hOwner = hWndAccessApp .lpszTitle = szDialogTitle .ulFlags = BIF_RETURNONLYFSDIRS End With
dwIList = SHBrowseForFolder(bi) szPath = Space$(512) X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
If X Then wPos = InStr(szPath, Chr(0)) BrowseFolder = Left$(szPath, wPos - 1) Else BrowseFolder = vbNullString End If frmSplashscreen.TextBox2 = szPath yy = frmSplashscreen.TextBox2 & "\zfilemds_Scheduler.mdb" frmSplashscreen.TextBox2 = yy conDBpath2 = yy Select Case XX Case "welcome" Case "handouts" Call ListBoxFill1 Case "workups" Call ListBoxFill2 Call ListBoxFill2 End Select End Function modEMRErrorHere I placed the code for error handling. It’s fairly short (this is the complete module): Option Explicit Public Function EMRError(errMod As String, ErrNum As String, ErrSource As String, ErrDesc As String) Const LogFileName As String = "C:\EMRError.log" Dim FileNum As Integer On Error Resume Next MsgBox "Erorr: " & ErrNum & vbNewLine & ErrDesc & vbNewLine & ErrSource, vbInformation, "Error in " & errMod FileNum = FreeFile Open LogFileName For Append As #FileNum Print #FileNum, "******************************************" Print #FileNum, "Date: " & Now() Print #FileNum, "Module: " & errMod Print #FileNum, "Number: " & ErrNum Print #FileNum, "Source: " & ErrSource Print #FileNum, "Description: " & ErrDesc Close #FileNum End Function modGlobalsThis is the main class module. Here is the beginning of the code: Option Explicit Public Const conDBpath As String = "C:\Program Files\Word EMR Project 2008\ZMDSBE.mdb" Public conDBpath2 As String, yy As String, tt As String, uu As String, vv As String, ww As String Public XX As String, xxx As String, zz As String, zzz As String, yyy As String, strTitle As String Public Const conDebug As Boolean = True Public lun As String, ptn As String, attgname1 As String, acctno As Long Public street1 As String, age1 As Long, sex11 As String Public citystatezip1 As String, NoOfRecords As Long, fvst1 As Date, MyTime, MySecond Public Phones1 As String, ptaddress1 As String, racee As String, tt11 As String Public db As DAO.Database, rs As DAO.Recordset, SQLStmt As String Public xlapp As Object, schdateD As Date, act, aa As String, age2 As String Public dd As Date, dd2 As Date, ii As Long, I As Long Public ftit As String, worddoc1 As String, pdfdoc1 As String, szDialogTitle As String Public lngDBTimesOpened As Long, pty As Property, lngProfileTimesOpened As Long '---------------------------------------------------------------- Type BROWSEINFO 'private hOwner As Long pidlRoot As Long pszDisplayName As String lpszTitle As String ulFlags As Long lpfn As Long lParam As Long iImage As Long End Type Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _ "SHGetPathFromIDListA" (ByVal pidl As Long, _ ByVal pszPath As String) As Long Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _ "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _ As Long Private Const BIF_RETURNONLYFSDIRS = &H1 Option Compare Binary ' Note that although Variants now have ' a VT_GUID type, this type is unsupported in VBA, ' so we must define our own here that will have the same ' binary layout as all GUIDs are expected by COM to ' have. Public Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(7) As Byte End Type Public Declare Function StringFromGUID2 Lib "ole32.dll" _ (rclsid As GUID, ByVal lpsz As Long, ByVal cbMax As Long) As Long Public Declare Function CoCreateGuid Lib "ole32.dll" _ (rclsid As GUID) As Long Overview of a UserForm Module: the Initialize Subroutine of the SOAP FormBelow we'll discuss the main code for the SOAP form, the most complicated UserForm. This is the code that sets up the various objects of this UserForm. Specifics will be discussed between the code snippets and more in depth in chapter 7 which describes important subroutine methods. Comments will be highlighted in green:
Sub UserForm_Initialize()
' set focus on the first tab ' begin defining/attaching the records to the various comboboxes
' here we set the width of
the columns ' Here the SOAP note page 2 is set up, filling all fields with ".";
'
alternatively,
one
can check for null errors by using the "Nz([field],
" "). ' At this point the comboboxes with smaller amount of data are populated
Saving Your VBA Project Frequently
Word's VBE occasionally falters and freezes, and when this occurs there is no backup copy of your work. You need to remember to backup your work every 10 minutes or so. Save your changes (click the Save button on the Standard toolbar, press Ctrl+S, or choose File Save), and then choose File Close and Return to Microsoft Word to close the Visual Basic Editor and return to Word. Alternatively, press Alt+F11 to flip back to Word, leaving the Visual Basic Editor open so that you can return to it later if you need to make further changes.
|