Về Trang đầu - Các Phần mềm Tiện ích


MsgBoxUni hiển thị font Unicode trong VB/VBA

Tôi xin giới thiệu các bạn hàm MsgBoxUni hiển thị font Unicode trong VB/VBA thể thay thế hàm MsgBox của VB.

 

MsgBoxUni.jpg

 

Tạo một module sau đó dán các khai báo hàm dưới đây.

 

'****************************************

'Tac gia: Nguyen Duy Tuan

'Tel : 0904.210.337

'E.Mail : tuanktcdcn@yahoo.com

'Website: www.bluesofts.net

'****************************************

'Khai bao cac ham API trong thu vien User32.DLL

Private Declare Function GetActiveWindow Lib "user32" () As Long

Private Declare Function MessageBoxW Lib "user32" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long

Function MsgBoxUni(ByVal PromptUni As Variant, Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, Optional ByVal TitleUni As Variant = vbNullString) As VbMsgBoxResult

   'Function MsgBoxUni(ByVal PromptUni As Variant, Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, Optional ByVal TitleUni As Variant, Optional HelpFile, Optional Context) As VbMsgBoxResult

   'BStrMsg,BStrTitle : La chuoi Unicode

   Dim BStrMsg, BStrTitle

   'Hàm StrConv Chuyen chuoi ve ma Unicode

   BStrMsg = StrConv(PromptUni, vbUnicode)

   BStrTitle = StrConv(TitleUni, vbUnicode)

   MsgBoxUni = MessageBoxW(GetActiveWindow, BStrMsg, BStrTitle, Buttons)

End Function

'Neu muon thay the ham MsgBox cua VB, hay dung ham duoi day

'Function MsgBox(ByVal PromptUni As Variant, Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, Optional ByVal TitleUni As Variant = vbNullString) As VbMsgBoxResult

' Dim BStrMsg, BStrTitle

' BStrMsg = StrConv(PromptUni, vbUnicode)

' BStrTitle = StrConv(TitleUni, vbUnicode)

'

' MsgBox = MessageBoxW(GetActiveWindow, BStrMsg, BStrTitle, Buttons)

'End Function

Sub Test()

   'Test trong Excel

   MsgBoxUni Range("B3").Value, vbInformation, Range("B4").Value

   MsgBox Range("B3").Value, vbInformation, Range("B4").Value

End Sub

Sub Test2()

   'UNC la ham chuyen tu ma TCVN3 sang Unicode

   MsgBoxUni UNC("Céng hoµ x· héi chñ nghÜa ViÖt Nam")

End Sub

Sub Test3()

   'VNI la ham chuyen tu ma VNI sang Unicode

   MsgBoxUni VNI("Coäng hoaø xaõ hoäi chuû nghóa Vieät Nam")

End Sub

 

Trong VB, chuỗi khai báo dạng AnsiString (single-byte), các tham số kiểu chuỗi trong hàm MessageBoxW kiểu WideString (trong C, Delphi PWCHAR). Dùng hàm StrConv("Chuỗi", vbUnicode) để chuyển "chuỗi" về Unicode, kiểu WideString.

Download file "MsgBoxUnicode.zip"

                                                                                                                Người viết bài: Nguyễn Duy Tuân

                                                                                                                    Website: www.bluesofts.net