VBA is a non-mainstream language, and there may not be many people involved. Here I will talk about some superficial views on VBA.
VBA is short for Visual Basic for Application. It has two major features: 1. It is a script language (interpretation and execution) that uses the BASIC syntax ), the other is that it is embedded into another application to control the function of calling this application system. In my impression, it first appeared in Microsoft Office, such as Word or Excel macros, and gradually expanded to other applications, such as AutoCad,.. NET integrated development environment supports VBA. In the Microsoft. NET Framework, a VBA running engine is already included. Using this engine can make the developed system support VBA as well.
IE supports VBScript, which uses the Basic syntax and can also call some functions of the browser. However, I personally think it is not VBA, because VBScript is too weak in control and has many security restrictions. However, in addition to controlling the document content processed by the application system, VBA can also control more modules. In addition, there are generally no security restrictions, so it can connect to the database or access binary files. It can be said that VBA is an intermediate of the common programming language and scripting language, and has both advantages and disadvantages. When a function is not complex, it is too troublesome to use the conventional programming method. If the system supports VBA, you can consider using VBA for implementation.
One notable feature of VBA is its close combination with the application system, which can easily call system interfaces, such as for Word or Excel, you can add menus or buttons on the system interface to map to VBA functions and define shortcut keys. This temporarily extends the functions of the application system.
This example shows the development and application of VBA integrated development environment in VS. NET2003.
VS. NET extension programming, the first thing that comes to mind is the plug-in,. NET provides a relatively complex API, you need to write programs and compile the generated executable files, and then. NET. If VBA is used, the process is greatly simplified.
Since then, I want to implement a function to copy all the files in a C # project to a directory, but the files in the project directory but not included in the project will not be copied. VS. the C # project file extension of NET2003 is. csproj is an XML document without an XML declaration header. It adopts the ANSI encoding format and our VBA code will load and parse this XML document, analyze the project reference information and the list of included files, and copy the referenced DLL files and all project files to the specified directory. All of its VBA code is as follows:
Public Sub CopyCSProject ()
Dim myPrj As EnvDTE. Project = DTE. ActiveWindow. Project
If UCase (System. IO. Path. GetExtension (myPrj. FullName) <> ". CSPROJ" Then
MsgBox ("the current project is not a C # Project", MsgBoxStyle. Information, "Copy CS project ")
Exit Sub
End If
Dim myFile As New System. IO. StreamReader (myPrj. FullName, System. Text. Encoding. GetEncoding (936 ))
Dim myDoc As New System. Xml. XmlDocument
MyDoc. LoadXml (myFile. ReadToEnd ())
MyFile. Close ()
Dim strPath As String = InputBox ("Enter the output directory name", "input ")
If strPath Is Nothing Then
Return
End If
If strPath. Length = 0 Then
Return
End If
Dim ThisPath As String = System. IO. Path. GetDirectoryName (myPrj. FullName)
CopyFile (myPrj. FullName, strPath)
Dim FileCount As Integer
Dim myElement As System. Xml. XmlElement
Dim strFileName As String
Dim strNewFileName As String
For Each myElement In myDoc. SelectNodes ("VisualStudioProject/CSHARP/Build/Referencds/Reference ")
StrFileName = myElement. GetAttribute ("HintPath ")
If strFileName. StartsWith ("c:") = False And strFileName. StartsWith ("C:") = False Then
CopyFile (ThisPath, strPath, strFileName)
FileCount = FileCount + 1
End If
Next
For Each myElement In myDoc. SelectNodes ("VisualStudioProject/CSHARP/Files/Include/File ")
StrFileName = myElement. GetAttribute ("RelPath ")
If Not strFileName Is Nothing Then
If strFileName. IndexOf (":") =-1 Then
CopyFile (ThisPath, strPath, strFileName)
FileCount = FileCount + 1
End If
End If
Next
MsgBox ("copying" & FileCount. ToString () & "Files ")
End Sub
Public Sub CreateDirectory (ByVal strDir As String)
If System. IO. Directory. Exists (strDir) = False Then
System. IO. Directory. CreateDirectory (strDir)
End If
End Sub
Public Sub CopyFile (ByVal strPath1 As String, ByVal strPath2 As String, ByVal strFilePath As String)
Dim strName1 As String = System. IO. Path. Combine (strPath1, strFilePath)
Dim strName2 As String = System. IO. Path. Combine (strPath2, strFilePath)
Dim dir1 As String = System. IO. Path. GetDirectoryName (strName1)
If System. IO. Directory. Exists (dir1) = False Then
System. IO. Directory. CreateDirectory (dir1)
End If
Dim dir2 As String = System. IO. Path. GetDirectoryName (strName2)
If System. IO. Directory. Exists (dir2) = False Then
System. IO. Directory. CreateDirectory (dir2)
End If
System. IO. File. Copy (strName1, strName2, True)
End Sub
Public Sub CopyFile (ByVal strFileName As String, ByVal strNewPath As String)
System. IO. File. Copy (strFileName, System. IO. Path. Combine (strNewPath, System. IO. Path. GetFileName (strFileName), True)
End Sub
After the code is compiled, add a button on the VS. NET toolbar to map to this Code. The procedure is as follows.
It can be seen from the above that the implementation of this process is very simple, using a simple process-oriented programming method, there is no complicated configuration, and the VBA here has no security restrictions, can be called. all classes in the. NET Framework can connect to the database, operate binary files, and start other programs. We can write VBA to implement various required functions, such as checking the document content and automatically generating the source code. If you are interested, we will study it together.
Note that this code can only be found in. run in NET2003, because. the format of the NET2005 C # project file is different from that of 2003. Therefore, you must modify part of the code for parsing the c # project file during migration. And VBA calls System. windows. there may be a problem with the types below Forms, while VBA is running, it may affect.. NET works normally. If VBA cannot run, disable.. NET.
Yuan Yongfu (http://www.xdesigner.cn) 2006-12-1