COM DLL `ScriptHelper.RegExp` – Thay thế VBScript.RegExp trong VBA (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

Tôi tuân thủ nội quy khi đăng bài

phuongnam366377

Thành viên hoạt động
Tham gia
25/10/19
Bài viết
193
Được thích
205
1/ Nội dụng bài Viết do ChatGPT biên soạn theo ý của tôi chỉ huy nó

2/ Code viết trên Delphi 13 + ChatGPT hổ trợ tạo nên thư viện này

3/ là bản thử nghiệm nên phát sinh lỗi là điều đương nhiên ...
Nên quá trình sử dụng có lỗi mô tả chi tiết nếu khả năng có thể Tôi sẽ điều chỉnh viết lại



# COM DLL `ScriptHelper.RegExp` – Thay thế VBScript.RegExp trong VBA

Microsoft đã loại bỏ **VBScript.RegExp** (trong `vbscript.dll`) trên Windows mới, khiến nhiều macro VBA cũ bị lỗi.
Giải pháp: **COM DLL `ScriptHelper.RegExp`** viết bằng Delphi, có thể đăng ký vào Windows và sử dụng trực tiếp trong VBA/Excel/Access/Word.

---

## Cài đặt

1. Copy file `ScriptHelper.dll` vào thư mục tùy ý.
2. Đăng ký bằng lệnh (chạy CMD với quyền admin):

Mã:
   regsvr32 ScriptHelper.dll

3. Trong VBA, tạo đối tượng như sau:
Mã:
   Dim re As Object
   Set re = CreateObject("ScriptHelper.RegExp")

---

## ‍ Ví dụ sử dụng

### 1. Execute – tìm tất cả match

Mã:
Sub TestRegExp()
Dim re As Object, m As Variant

Set re = CreateObject("ScriptHelper.RegExp")
re.Pattern = "123.+?abc.+?@@@"
re.Global = True
re.IgnoreCase = False

m = re.Execute("123  abc  @@@")

Debug.Print "Match count: "; UBound(m) - LBound(m) + 1
Debug.Print "First Match: "; m(0)
```

End Sub

Kết quả:

```
Match count: 1
First Match: 123 abc @@@
```

---

### 2. Replace – thay thế toàn bộ

Mã:
Sub TestReplace()
Dim re As Object, result As Variant

```
Set re = CreateObject("ScriptHelper.RegExp")
re.Pattern = "\d+"
re.Global = True

result = re.Replace("Số 123 và 456", "###")
Debug.Print result   ' Kết quả: Số ### và ###
```

End Sub

---

### 3. FirstMatch – tìm match đầu tiên

Mã:
Sub TestFirstMatch()
Dim re As Object, first As Variant

```
Set re = CreateObject("ScriptHelper.RegExp")
re.Pattern = "\w+"
re.Global = True

first = re.FirstMatch("Xin chao cac ban")
Debug.Print first   ' Kết quả: Xin
```

End Sub

---

### 4. Split – tách chuỗi theo Regex

Mã:
Sub TestSplit()
Dim re As Object, parts As Variant, i As Long

```
Set re = CreateObject("ScriptHelper.RegExp")
re.Pattern = "[,; ]+"

parts = re.Split("A,B;C D")

For i = LBound(parts) To UBound(parts)
    Debug.Print parts(i)
Next
' Kết quả:
' A
' B
' C
' D
```

End Sub

---

## ✅ Ưu điểm

* Chạy tốt trên Windows 10/11 (không cần VBScript).
* Hỗ trợ Unicode đầy đủ.
* Giao diện tương tự `VBScript.RegExp`.
* Dùng được ngay trong Excel, Access, Word.

---

Nếu bạn đang bảo trì macro VBA cũ, hãy thay `VBScript.RegExp` bằng **ScriptHelper.RegExp** để code tiếp tục chạy ổn định trên Windows mới.



1758680439371.png

Giao diện sau khi đăng ký COM thành công nếu Add DLL sử dụng trong VBA sẽ thấy các hàm cơ bản như trên

vọc chơi cho vui lúc rảnh hai ngày tạm định hình xong thư viện cơ bản .... thong thả và dong chơi nếu thích bổ sung thêm các hàm tiện ích khác

đính kèm File phía dưới là COM DLL 64 bít vậy chỉ sử dụng cho Excel 64 bít ... nếu ai dùng bản 32 bít thì báo Tôi úp thêm
kèm các ví dụ sử dụng trong File Excel các kiểu


Password WinRaR là : 123
 

File đính kèm

Lần chỉnh sửa cuối:
# Hướng dẫn đăng ký & sử dụng ScriptHelper.RegExp trong VBA

## Đăng ký DLL

1. Copy file `ScriptHelper.dll` vào thư mục tùy ý (ví dụ `C:\Tools\ScriptHelper\`).
2. Mở CMD với quyền **Administrator** và chạy lệnh:
Mã:
   regsvr32 "C:\Tools\ScriptHelper\ScriptHelper.dll"

Nếu hiện thông báo `DllRegisterServer in ScriptHelper.dll succeeded` nghĩa là đã đăng ký thành công.

## ✅ Kiểm tra DLL trong VBA
1. Mở **VBA Editor** (Alt+F11 trong Excel/Word/Access).
2. Vào menu **Tools → References**.
3. Tìm và tick chọn **ScriptHelper RegExp**.

* Nếu thấy trong danh sách → DLL đã đăng ký thành công.
* Nếu không thấy → kiểm tra lại lệnh `regsvr32` và đường dẫn DLL.

Sau khi tick chọn, bạn có thể khai báo kiểu dữ liệu cụ thể:

Dim re As ScriptHelper.RegExp
→ sẽ có IntelliSense hỗ trợ (early binding).

## ‍ Demo đầy đủ (Early Binding)

Mã:
'---------------------------------------------
' Module: RegexExamples\_EarlyBinding
' Purpose: Demo all features of ScriptHelper.RegExp (early binding)
'---------------------------------------------
Option Explicit

Sub RunAllRegexExamples\_EarlyBinding()
Call Example\_4Digit
Call Example\_CatDog
Call Example\_Email
Call Example\_Decimal
Call Example\_UppercaseReplace
Call Example\_Date
Call Example\_URL
End Sub

'-----------------------------
' Example 1: Find 4-digit numbers
'-----------------------------
Sub Example\_4Digit()
Dim re As ScriptHelper.RegExp
Dim matches As Variant, m As Variant

```
Set re = New ScriptHelper.RegExp
re.Pattern = "\b\d{4}\b"
re.IgnoreCase = True
re.Global = True

matches = re.Execute("Year 2025, month 09, day 23")
Debug.Print "Example 1 - 4-digit numbers:"
For Each m In matches
    Debug.Print m
Next
```

End Sub

'-----------------------------
' Example 2: Find all "cat" or "dog"
'-----------------------------
Sub Example\_CatDog()
Dim re As ScriptHelper.RegExp
Dim matches As Variant, m As Variant

```
Set re = New ScriptHelper.RegExp
re.Pattern = "\b(cat|dog)\b"
re.IgnoreCase = True
re.Global = True

matches = re.Execute("I have a Cat, a dog, and a bird")
Debug.Print "Example 2 - Cat or Dog:"
For Each m In matches
    Debug.Print m
Next
```

End Sub

'-----------------------------
' Example 3: Find basic emails
'-----------------------------
Sub Example\_Email()
Dim re As ScriptHelper.RegExp
Dim matches As Variant, m As Variant

```
Set re = New ScriptHelper.RegExp
re.Pattern = "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}"
re.IgnoreCase = True
re.Global = True

matches = re.Execute("Mail me at test@example.com or admin@site.org")
Debug.Print "Example 3 - Emails:"
For Each m In matches
    Debug.Print m
Next
```

End Sub

'-----------------------------
' Example 4: Find decimals
'-----------------------------
Sub Example\_Decimal()
Dim re As ScriptHelper.RegExp
Dim matches As Variant, m As Variant

```
Set re = New ScriptHelper.RegExp
re.Pattern = "\b\d+\.\d+\b"
re.IgnoreCase = True
re.Global = True

matches = re.Execute("Pi ~ 3.14, e ~ 2.718")
Debug.Print "Example 4 - Decimals:"
For Each m In matches
    Debug.Print m
Next
```

End Sub

'-----------------------------
' Example 5: Replace all uppercase letters with "X"
'-----------------------------
Sub Example\_UppercaseReplace()
Dim re As ScriptHelper.RegExp

```
Set re = New ScriptHelper.RegExp
re.Pattern = "[A-Z]"
re.IgnoreCase = False
re.Global = True

Debug.Print "Example 5 - Replace uppercase:"
Debug.Print re.Replace("AbCdeFG", "X")  ' Output: XbXdexXx
```

End Sub

'-----------------------------
' Example 6: Find dates dd/mm/yyyy
'-----------------------------
Sub Example\_Date()
Dim re As ScriptHelper.RegExp
Dim matches As Variant, m As Variant

```
Set re = New ScriptHelper.RegExp
re.Pattern = "\b\d{2}/\d{2}/\d{4}\b"
re.IgnoreCase = True
re.Global = True

matches = re.Execute("Today: 23/09/2025, Tomorrow: 24/09/2025")
Debug.Print "Example 6 - Dates:"
For Each m In matches
    Debug.Print m
Next
```

End Sub

'-----------------------------
' Example 7: Find basic URLs
'-----------------------------
Sub Example\_URL()
Dim re As ScriptHelper.RegExp
Dim matches As Variant, m As Variant

```
Set re = New ScriptHelper.RegExp
re.Pattern = "https?://[^\s]+"
re.IgnoreCase = True
re.Global = True

matches = re.Execute("Visit https://example.com and http://site.org/page")
Debug.Print "Example 7 - URLs:"
For Each m In matches
    Debug.Print m
Next
```

End Sub
 
Lần chỉnh sửa cuối:

Bài viết mới nhất

Back
Top Bottom