Microsoft VBScript & Object Expressions ?le Veri Bile?enlerine Komut Yapma ?rne?i

Microsoft VBScript & Object Expressions ?le Veri Bile?enlerine Komut Yapma ?rne?i

VBScript; i?inde ?al??t??? ortam?n ??elerine eri?mek i?in Bile?en Nesne Modelini kullan?r. ?rne?in FileSystemObject (FSO), dosyalar? olu?turmak, okumak, güncellemek ve silmek i?in kullan?l?r. VBScript, Windows 98'den bu yana Microsoft Windows'un her masaüstü sürümünde, Windows NT 4.0 Se?enek Paketi'nden bu yana Windows Server'da ve iste?e ba?l? olarak Windows CE'de varsay?lan olarak yüklenmi?tir.

Bu ?al??mada VBScript UserForm üzerinde uygulanm??t?r. MSO? Excel VBA ortam?nda 'Insert' yerle?ik menüsü ile eklenen UserFor1 üzerin 'ToolBox' yard?m? ile ve ?zensiz olarak Image1, Label1, label2, TextBox1, TextBox2 ve ListBox1 yongalar? eklenmi?tir. UserForm1 ve Module1 VBA kod alanlar?na ihtiyaca uygun kodlar yaz?lm?? ve References listesine Speech i?in sapi.dll ilave edilmi?tir.

'MSO? Excel VBA - UserForm1

'Microsoft VBScript and Object Expressions

'A. Objects

'a. VBScript RegExp Object

'1. Properties: Pattern, Methods: Test (search-string)

'2. Properties: IgnoreCase, Methods : Replace (search-string, replace-string)

'3. Properties: Global, Methods : Execute (search-string)

'Pattern - A string that is used to define the regular expression. This must be set before use of the regular expression object. Patterns are described in more detail below.

'IgnoreCase - A Boolean property that indicates if the regular expression should be tested against all possible matches in a string. By default, IgnoreCase is set to False.

'Global - A Boolean property that indicates if the regular expression should be tested against all possible matches in a string. By default, Global is set to False.

'Test (string) - The Test method takes a string as its argument and returns True if the regular expression can successfully be matched against the string, otherwise False is returned.

'Replace (search-string, replace-string) - The Replace method takes 2 strings as its arguments. If it is able to successfully match the regular expression in the search-string, then it replaces that match with the replace-string, and the new string is returned. If no matches were found, then the original search-string is returned.

'Execute (search-string) - The Execute method works like Replace, except that it returns 'a Matches collection object, containing a Match object for each successful match. It doesn't modify the original string.

'b. VBScript Matches Collection Object

'1. Properties: Count

'2. Properties: Item

'Count - A read-only value that contains the number of Match objects in the collection.

'Item - A read-only value that enables Match objects to be randomly accessed from the Matches collection object. The Match objects may also be incrementally accessed from the Matches collection object, using a For-Next loop.

'c. VBScript Match Object

'1. Properties: FirstIndex

'2. Properties: Length

'3. Properties: Value

'FirstIndex - A read-only value that contains the position within the original string where the match occurred. This index uses a zero-based offset to record positions, meaning that the first position in a string is 0.

'Length - A read-only value that contains the total length of the matched string.

'Value - A read-only value that contains the matched value or text. It is also the default value when accessing the Match object.

'B. Pattern Options

'a. Position Matching [Position matching involves the use of the ^ and $ to search for beginning or ending of strings. Setting the pattern property to "^VBScript" will only successfully match "VBScript is cool." But it will fail to match "I like VBScript."]

'1. Symbol: ^, Function: Only match the beginning of a string. "^A" matches first "A" in "An A+ for Anita."

'2. Symbol: $, Function: Only match the ending of a string. "tquot; matches the last "t" in "A cat in the hat"

'3. Symbol: \b, Function: Matches any word boundary "ly\b" matches "ly" in "possibly tomorrow."

'4. Symbol: \B, Function: Matches any non-word boundary

'b. Literals [Literals can be taken to mean alphanumeric characters, ACSII, octal characters, hexadecimal characters, UNICODE, or special escaped characters. Since some characters have special meanings, we must escape them. To match these special characters, we precede them with a "\" in a regular expression.]

'1. Symbol: Alphanumeric, Function: Matches alphabetical and numerical characters literally.

'2. Symbol: \n, Function: Matches a new line

'3. Symbol: \f, Function: Matches a form feed

'4. Symbol: \r, Function: Matches carriage return

'5. Symbol: \t, Function: Matches horizontal tab

'6. Symbol: \v, Function: Matches vertical tab

'7. Symbol: \?, Function: Matches ?

'8. Symbol: \*, Function: Matches *

'9. Symbol: \+, Function: Matches +

'10. Symbol: \., Function: Matches .

'11. Symbol: \, Function: Matches

'12. Symbol: \{, Function: Matches {

'13. Symbol: \}, Function: Matches }

'14. Symbol: \\, Function: Matches \

'15. Symbol: \[, Function: Matches [

'16. Symbol: \], Function: Matches ]

'17. Symbol: \(, Function: Matches (

'18. Symbol: \), Function: Matches )

'19. Symbol: \xxx, Function: Matches the ASCII character expressed by the octal number xxx.

'20. Symbol: \xxx, Function: "\50" matches "(" or chr (40).

'21. Symbol: \xdd, Function: Matches the ASCII character expressed by the hex number dd.

'22. Symbol: \xdd, Function: "\x28" matches "(" or chr (40).

'23. Symbol: \uxxxx, Function: Matches the ASCII character expressed by the UNICODE xxxx.

'24. Symbol: \uxxxx, Function: "\u00A3" matches "£".

'c. Character Classes [Character classes enable customized grouping by putting expressions within [] braces. A negated character class may be created by placing ^ as the first character inside the []. Also, a dash can be used to relate a scope of characters. For example, the regular expression "[^a-zA-Z0-9]" matches everything except alphanumeric characters. In addition, some common character sets are bundled as an escape plus a letter.]

'1. Symbol: [xyz], Function: Match any one character enclosed in the character set., "[a-e]" matches "b" in "basketball".

'2. Symbol: [^xyz], Function: Match any one character not enclosed in the character set., "[^a-e]" matches "s" in "basketball".

'3. Symbol: ., Function: Match any character except \n.

'4. Symbol: \w, Function: Match any word character. Equivalent to [a-zA-Z_0-9].

'5. Symbol: \W, Function: Match any non-word character. Equivalent to [^a-zA-Z_0-9].

'6. Symbol: \d, Function: Match any digit. Equivalent to [0-9].

'7. Symbol: \D, Function: Match any non-digit. Equivalent to [^0-9].

'8. Symbol: \s, Function: Match any space character. Equivalent to [ \t\r\n\v\f].

'9. Symbol: \S, Function: Match any non-space character. Equivalent to [^ \t\r\n\v\f].

'd. Repetition [Repetition allows multiple searches on the clause within the regular expression. By using repetition matching, we can specify the number of times an element may be repeated in a regular expression.]

'1. Symbol: {x}, Function: Match exactly x occurrences of a regular expression., "\d{5}" matches 5 digits.

'2. Symbol: {x,}, Function: Match x or more occurrences of a regular expression., "\s{2,}" matches at least 2 space characters.

'3. Symbol: {x,y}, Function: Matches x to y number of occurrences of a regular expression., "\d{2,3}" matches at least 2 but no more than 3 digits.

'4. Symbol: ?, Function: Match zero or one occurrences. Equivalent to {0,1}., "a\s?b" matches "ab" or "a b".

'5. Symbol: *, Function: Match zero or more occurrences. Equivalent to {0,}.

'6. Symbol: +, Function: Match one or more occurrences. Equivalent to {1,}.

'e. Alternation & Grouping [Alternation and grouping is used to develop more complex regular expressions. Using alternation and grouping techniques can create intricate clauses within a regular expression, and offer more flexibility and control.]

'1. Symbol: (), Function: Grouping a clause to create a clause. May be nested. "(ab)?(c)" matches "abc" or "c".

'2. Symbol: , Function: Alternation combines clauses into one regular expression and then matches any of the individual clauses., "(ab)(cd)(ef)" matches "ab" or "cd" or "ef".

'f. BackReferences [Backreferences enable the programmer to refer back to a portion of the regular expression. This is done by use of parenthesis and the backslash (\) character followed by a single digit. The first parenthesis clause is referred by \1, the second by \2, etc.]

'1. Symbol: ()\n, Function: Matches a clause as numbered by the left parenthesis., "(\w+)\s+\1" matches any word that occurs twice in a row, such as "hubba hubba."

'C. Example

'"^\s*((\$\s?)(£\s?))?((\d+(\.(\d\d)?)?)(\.\d\d))\s*(UKGBPGBUSAUSUSD)?)\s*quot;

'"^\s*…" and "…\s*quot; - means that there can be any number of leading and end space characters, and the input must be on a line by itself

'"((\$\s?)(£\s?))?" - means an optional $ or £ sign followed by an optional space

'"((\d+(\.(\d\d)?)?)(\.\d\d))" - searches for at least one digit, followed by an optional decimal and two digits (which are optional) or a decimal and two digits. This means that input such as 6., 23.33, .88 are all allowed, but 5.5 is not.

'"\s*(UKGBPGBUSAUSUSD)?" - means that any number of space characters are valid followed by optional and acceptable arguments to the string.

  • Option Explicit
  • Private i As Single
  • Private No As Double, Uzunluk As Double
  • Private Metin As String, Kelime As String
  • Private ObjRegExp As Object
  • Private Eleman, Küme, Change
  • Private WithEvents Seslendirme As SpVoice
  • Private Sub UserForm_Initialize()
  • On Error Resume Next
  • Me.Caption = "[PB?D?] Microsoft VBScript and Object Expressions"
  • Call EkranDüzenle
  • Call VeriDüzenle
  • Set Seslendirme = New SpVoice
  • End Sub
  • Private Sub ListBox1_Click()
  • On Error Resume Next
  • No = ListBox1.ListIndex
  • Uzunluk = (ListBox1.List(No, 2) - ListBox1.List(No, 0))
  • Kelime = ListBox1.List(No, 1)
  • TextBox1.SelStart = ListBox1.List(No, 0)
  • TextBox1.SelLength = Uzunluk
  • TextBox1.SetFocus
  • TextBox2.SelStart = ListBox1.List(No, 0)
  • TextBox2.SelLength = Uzunluk
  • TextBox2.SelText = ListBox1.List(No, 1)
  • Seslendirme.Speak Kelime, SVSFlagsAsync
  • VBA.DoEvents
  • End Sub
  • Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  • On Error Resume Next
  • For i = 0 To (ListBox1.ListCount - 1)
  • No = i
  • Uzunluk = (ListBox1.List(No, 2) - ListBox1.List(No, 0))
  • Kelime = ListBox1.List(No, 1)
  • With TextBox1
  • .SelStart = ListBox1.List(No, 0)
  • .SelLength = Uzunluk
  • .SetFocus
  • End With
  • DoEvents
  • With TextBox2
  • .SelStart = ListBox1.List(No, 0)
  • .SelLength = Uzunluk
  • .SelText = ListBox1.List(No, 1)
  • End With
  • Seslendirme.Speak Kelime, SVSFlagsAsync
  • DoEvents
  • Next i
  • End Sub
  • Private Sub EkranDüzenle()
  • On Error Resume Next
  • With Me
  • .Height = 256
  • .Width = 426
  • '.Picture = LoadPicture("C:\....\*.jpg")
  • .Picture = Resim(URL1)
  • .PictureAlignment = fmPictureAlignmentTopLeft
  • .PictureSizeMode = fmPictureSizeModeStretch
  • .PictureTiling = False
  • With Image1
  • .Left = 6
  • .Top = 6
  • .Height = 24
  • .Width = 24
  • .BorderColor = vbWhite
  • .BorderStyle = fmBorderStyleSingle
  • .BackStyle = fmBackStyleTransparent
  • '.Picture = LoadPicture("C:\....\*.ico")
  • .Picture = Resim(URL2)
  • .PictureAlignment = fmPictureAlignmentCenter
  • .PictureSizeMode = fmPictureSizeModeClip
  • .PictureTiling = False
  • End With
  • With Label1
  • .Top = 6
  • .Left = 36
  • .Height = 12
  • .Width = 228
  • .AutoSize = False
  • .BackStyle = fmBackStyleTransparent
  • .BorderStyle = fmBorderStyleNone
  • .Caption = "Mustafa ULUSARA?"
  • .Font.Bold = True
  • .ForeColor = vbBlue
  • .SpecialEffect = fmSpecialEffectFlat
  • .TextAlign = fmTextAlignLeft
  • End With
  • With Label2
  • .Top = 18
  • .Left = 36
  • .Height = 12
  • .Width = 228
  • .AutoSize = False
  • .BackStyle = fmBackStyleTransparent
  • .BorderStyle = fmBorderStyleNone
  • .Caption = "Program Büt?eleme - ?zleme De?erlendirme"
  • .Font.Bold = True
  • .ForeColor = vbBlue
  • .SpecialEffect = fmSpecialEffectFlat
  • .TextAlign = fmTextAlignLeft
  • End With
  • With TextBox1
  • .Left = 6
  • .Top = 36
  • .Height = 93.75
  • .Width = 258
  • .BackStyle = fmBackStyleTransparent
  • .AutoTab = True
  • .AutoSize = False
  • .AutoWordSelect = True
  • .BorderStyle = fmBorderStyleSingle
  • .BorderColor = &HFFFF80
  • .Font.Bold = False
  • .ForeColor = vbBlue
  • .Locked = True
  • .MultiLine = True
  • .ScrollBars = fmScrollBarsBoth
  • .SpecialEffect = fmSpecialEffectFlat
  • End With
  • With TextBox2
  • .Left = 6
  • .Top = 133
  • .Height = 93.75
  • .Width = 258
  • .BackStyle = fmBackStyleTransparent
  • .AutoTab = True
  • .AutoSize = False
  • .AutoWordSelect = True
  • .BorderStyle = fmBorderStyleSingle
  • .BorderColor = &HFFFF80
  • .Font.Bold = False
  • .ForeColor = &H404000
  • .Locked = True
  • .MultiLine = True
  • .ScrollBars = fmScrollBarsBoth
  • .SpecialEffect = fmSpecialEffectFlat
  • End With
  • With ListBox1
  • .Left = 267
  • .Top = 36
  • .Height = (TextBox2.Top + TextBox2.Height) - TextBox1.Top
  • .Width = 150
  • .BorderColor = &HFFFF80
  • .BorderStyle = fmBorderStyleSingle
  • .BackColor = vbWhite
  • .ColumnCount = 3
  • .ColumnWidths = "36;72;36"
  • .ForeColor = vbBlue
  • .ListStyle = fmListStylePlain
  • .Locked = False
  • .MultiSelect = fmMultiSelectSingle
  • .SpecialEffect = fmSpecialEffectFlat
  • End With
  • End With
  • End Sub
  • Private Sub VeriDüzenle()
  • On Error Resume Next
  • TextBox1.Text = "VBScript Regular Expressions Regular expression reference and examples for VBScript. Regular expressions in VBScript are two words that can bring many to their knees, weeping, but they are not as scary as some would have you believe. With their roots in Perl, regular expressions in VBScript use similar syntax, and the chances are that you may already be familiar with the concepts here if you have played with regular expression matching before."
  • Metin = TextBox1.Text
  • Set ObjRegExp = VBA.CreateObject("VBScript.RegExp")
  • With ObjRegExp
  • .Pattern = "\w+"
  • .IgnoreCase = False
  • .Global = True
  • Set Küme = .Execute(Metin)
  • End With
  • No = 0
  • For Each Eleman In Küme
  • ListBox1.AddItem (Eleman.FirstIndex & " ")
  • ListBox1.List(No, 1) = Eleman.Value
  • ListBox1.List(No, 2) = VBA.Len(Eleman.Value) + (Eleman.FirstIndex & " ")
  • No = No + 1
  • Next
  • For i = 0 To ListBox1.List((No - 1), 2)
  • TextBox2.Text = TextBox2.Text & "_" '" "
  • Next i
  • End Sub

'MSO? Excel VBA - Module1

  • Option Explicit
  • #If VBA7 Or Win64 Then
  • Public Declare PtrSafe Function CLSIDFromString Lib "ole32" (ByVal lpstrCLSID As LongPtr, lpCLSID As Any) As LongPtr
  • Public Declare PtrSafe Function OleLoadPicturePath Lib "oleaut32" (ByVal szURLorPath As LongPtr, ByVal punkCaller As LongPtr, ByVal dwReserved As LongPtr, ByVal clrReserved As OLE_COLOR, ByRef riid As Any, ByRef ppvRet As Any) As LongPtr
  • #Else
  • Public Declare Function CLSIDFromString Lib "ole32" (ByVal lpstrCLSID As Long, lpCLSID As Any) As Long
  • Public Declare Function OleLoadPicturePath Lib "oleaut32" (ByVal szURLorPath As Long, ByVal punkCaller As Long, ByVal dwReserved As Long, ByVal clrReserved As OLE_COLOR, ByRef riid As Any, ByRef ppvRet As Any) As Long
  • #End If
  • Public IPic(15) As Byte
  • Public Const ClsID As Variant = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}"
  • Public Const URL1 As String = "https://2.bp.blogspot.com/_hsHTxo_5L8E/S7rn6KHVfNI/AAAAAAAACRs/fxVMg9YGzb4/s1600/VectorBackround.jpg" 'Microsoft Office Excel? Kod K?lavuzu [UserFormBackround]
  • Public Const URL2 As String = "https://2.bp.blogspot.com/_hsHTxo_5L8E/S78EbvJyhRI/AAAAAAAACS0/txbOQ1qubg8/s1600/PB%C4%B0D_jpg.jpg" 'Microsoft Office Excel? Kod K?lavuzu [PB?D Icon]
  • Sub UserForm_A?()
  • On Error Resume Next
  • UserForm1.Show 0
  • Call VBPReference_Listele
  • End Sub
  • Function Resim(URL) As Picture
  • On Error Resume Next
  • CLSIDFromString StrPtr(ClsID), IPic(0)
  • OleLoadPicturePath StrPtr(URL), 0&, 0&, 0&, IPic(0), Resim
  • End Function
  • Private Sub VBPReference_Listele()
  • On Error Resume Next
  • Dim Eleman, No
  • VBA.Err.Clear
  • ThisWorkbook.Sheets("VBP_References").Select
  • If VBA.Err.Number <> 0 Then
  • ThisWorkbook.Worksheets.Add(Before:=Sheets(1)).Select
  • ActiveSheet.Name = "VBP_References"
  • End If
  • No = 2
  • Cells.Delete
  • With Sheets("VBP_References")
  • With .Range("A1:C1")
  • .Cells(1, 1).Value = "Name"
  • .Cells(1, 2).Value = "Description"
  • .Cells(1, 3).Value = "Full Path"
  • .Font.Bold = True
  • .Interior.Color = VBA.RGB(228, 228, 228)
  • End With
  • For Each Eleman In ThisWorkbook.VBProject.References
  • .Cells(No, 1) = Eleman.Name
  • .Cells(No, 2) = Eleman.Description
  • .Cells(No, 3) = Eleman.FullPath
  • No = No + 1
  • Next Eleman
  • End With
  • Columns("A:C").EntireColumn.AutoFit
  • End Sub

要查看或添加评论,请登录

Mustafa ULUSARA?的更多文章

社区洞察

其他会员也浏览了