vbscript VBA - 基础

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vbscript VBA - 基础相关的知识,希望对你有一定的参考价值。

' It is usually best if global variables are declared into a separate module
' It is usually best if we include a function to initialize all global variables, which 
' will be called when the form starts.

'ie Global variable Declaration
' As we see it follows the Dim <name> As <Type> pattern.
 Global lngRed As Long
 Global lngBlack As Long
 Global lngYellow As Long
 Global lngWhite As Long
 Global lngGreen As Long
 Global TempDir As String
 Global WorkDir As String
 Global OutputDir As String
 Global OutputFile As String
 Global TodaysDate As String
 


' Value assignment in variables (Use Set or straight)

Set lngYellow =  RGB(255, 255, 0)
'or lngYellow =  RGB(255, 255, 0)

 
'ie Sub to initialize global variables
' Called in Form_Load event by:
' Private Sub Form_Load()
'
'    ' Initialize global variables
'     Call Init_Globals
Public Sub Init_Globals()

    ' Function : Init_Globals
    ' Work     : Initialize global variables
    ' Called as: Init_Globals
    ' Called at: Main Form Load
    
    ' Colors are initialized by an RGB(X,Y,Z)
    lngRed = RGB(255, 0, 0)
    lngBlack = RGB(0, 0, 0)
    lngYellow = RGB(255, 255, 0)
    lngWhite = RGB(255, 255, 255)
    lngGreen = RGB(0, 255, 0)
    
    ' Dir for Temporary File Dump
    TempDir = "C:\Users\p.doulgeridis\Desktop\out.txt"
    
    ' Dir for file lookup
    WorkDir = "C:\Users\p.doulgeridis\Desktop\anakliseis\"
    
    ' Dir for output file
    OutputDir = "C:\Users\p.doulgeridis\Desktop\anakliseis\"
    
    ' Output File name
    TodaysDate = Format(Date, "mmddyy")
    OutputFile = OutputDir & "AnakCheck." & TodaysDate & ".txt"
    
End Sub
' The main difference between functions and subs is that
' functions RETURN a value
' subs do not return a value.

' Functions are declared with:
' The return value is handled with assignment on the function name
Public Function FunctionName(ByVar var1 As String) As <ReturnType>
  FunctionName = true
End Function

' Examples of functions:
Public Function FileExists(ByVal path_ As String) As Boolean

    ' Function : FileExists
    ' Work     : Check if specific file exists
    ' Called as: FileExists(path/to/file)
    ' Called at: Main Form Checks
    ' Input    : Filename
    
    FileExists = (Len(Dir(path_)) > 0)
End Function



Public Function DirExists(ByVal path_ As String) As Boolean
    'DirExists = (Len(Dir(path_)) > 0)
    ' Function : DirExists
    ' Work     : Check if specific directory exists
    ' Called as: FileExists(path/to/file)
    ' Called at: Main Form Checks
    ' Input    : Directory path
    
    If Dir(path_, vbDirectory) <> "" Then
        DirExists = True
        Exit Function
    Else
        DirExists = False
        Exit Function
    End If
    
    
' Example of a Sub
Private Sub ClearButton_Click()

    ' Status : Done

    ' Clean fields
    Me.TextBoxPod.Value = Null
    Me.TextBoxDate.Value = Null
    Me.TextboxFound.Value = Null
    Me.TextBoxPod.SetFocus
    
    ' Update LabelStatus
    If DirExists(WorkDirectory) Then
        ' Debug
        MsgBox ("Work directory located.")
        Me.LabelStatus.Value = "Initial Checks Successful."
        Me.LabelStatus.BackColor = lngGreen
    Else
        MsgBox ("Work directory: " & WorkDirectory & " was not located. Please restart application.")
        Me.LabelStatus.Value = "Initial Checks Unsuccesful. Work Directory not located"
        Me.LabelStatus.BackColor = lngRed
        Exit Sub
    End If

End Sub

以上是关于vbscript VBA - 基础的主要内容,如果未能解决你的问题,请参考以下文章

使用 VBA 控制 VBScript

将 Staad 与 VBS 一起使用(将 VBA 文档转换为 VBscript)

vbscript VBA - Excel - Hack受保护的Excel文档或表格(VBA) - 代码

在VBScript文件中集成VBA

vbscript 在VBA中运行Shell命令

vbscript VBA准备代码