Hỏi cách Set định dạng Long Date của Windows = VBA ?

Liên hệ QC

campha

Thành viên mới
Tham gia
4/5/13
Bài viết
29
Được thích
7
Các bạn cho mình hỏi nếu muốn SET/ CÀI ĐẶT định dạng Long date trong Control Panel/ Regional thì làm như thế nào? ví dụ định dạng long date muốn set "MMMM dd, yyyy"

Ví dụ: kiểm tra định dạng long Date
Mã:
Sub checkLongDateFormat()

Dim oShell As Object
Dim longDate

Set oShell = CreateObject("wscript.shell")
longDate = oShell.RegRead("HKEY_CURRENT_USER\Control Panel\International\sLongDate")
MsgBox longDate

End Sub

+ Nếu dùng CreateObject("wscript.shell") thì code như thế nào sửa được giá trị của Key "sLongDate" trong Regedit để nhận được "MMMM dd, yyyy"?
+ Nếu viết = hàm API thì CODE ntn
?

Many tks
 
Lần chỉnh sửa cuối:
Các bạn cho mình hỏi nếu muốn SET/ CÀI ĐẶT định dạng Long date trong Control Panel/ Regional thì làm như thế nào? ví dụ định dạng long date muốn set "MMMM dd, yyyy"

Ví dụ: kiểm tra định dạng long Date
Mã:
Sub checkLongDateFormat()

Dim oShell As Object
Dim longDate

Set oShell = CreateObject("wscript.shell")
longDate = oShell.[B][COLOR=#ff0000]RegRead[/COLOR][/B]("HKEY_CURRENT_USER\Control Panel\International\sLongDate")
MsgBox longDate

End Sub

+ Nếu dùng CreateObject("wscript.shell") thì code như thế nào sửa được giá trị của Key "sLongDate" trong Regedit để nhận được "MMMM dd, yyyy"?
+ Nếu viết = hàm API thì CODE ntn
?

Many tks
Có phương thức RegRead (đọc) thì ắc có phương thức RegWrite (ghi)
Cú pháp:
RegRead(Name)
RegWrite(Name, Value, Type)

Bạn tự mình "suy đoán" và thí nghiệm xem
 
Lần chỉnh sửa cuối:
Upvote 0
Có phương thức RegRead (đọc) thì ắc có phương thức RegWrite (ghi)
Cú pháp:
RegRead(Name)
RegWrite(Name, Value, Type)

Bạn tự mình "suy đoán" và thí nghiệm xem
Cảm ơn thầy đã gợi ý
Mã:
Sub LongDateFormat()

Dim oShell As Object
Dim longDate

Set oShell = CreateObject("wscript.shell")
longDate = oShell.RegRead("HKEY_CURRENT_USER\Control Panel\International\sLongDate")
MsgBox longDate

[B][COLOR=#0000ff]oShell.Regwrite "HKEY_CURRENT_USER\Control Panel\International\sLongDate", "MMMM dd, yyyy"[/COLOR][COLOR=#ff0000], "REG_SZ"[/COLOR][/B] '<====Chỗ chữ đỏ không có thì win tự mặc định "REG_SZ"
longDate = oShell.RegRead("HKEY_CURRENT_USER\Control Panel\International\sLongDate")
MsgBox longDate

End Sub
Xin thầy chỉ giúp cho cách khác can thiệp vao Regedit, có người nói có thể dùng hàm API hoặc kĩ thuật WMI gì đó? xin cho hỏi có tài liệu hay trang nào tham khảo được không ạ?
Many tks
 
Upvote 0
Xin thầy chỉ giúp cho cách khác can thiệp vao Regedit, có người nói có thể dùng hàm API hoặc kĩ thuật WMI gì đó? xin cho hỏi có tài liệu hay trang nào tham khảo được không ạ?
Many tks

Bạn muốn chơi API hả? Thì API:

Mã:
Private Const KEY_ALL_ACCESS = &H3F
Private Const ERROR_SUCCESS = 0

Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_USERS = &H80000003

Private Const REG_SZ = (1)

Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" _
  (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
  ByVal samDesired As Long, phkResult As Long) As Long

Private Declare Function RegSetValueExStr Lib "advapi32" Alias "RegSetValueExA" _
  (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
   ByVal dwType As Long, ByVal szData As String, ByVal cbData As Long) As Long
   
Private Declare Function RegSetValueExLong Lib "advapi32" Alias "RegSetValueExA" _
  (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
   ByVal dwType As Long, szData As Long, ByVal cbData As Long) As Long
   
Private Declare Function RegSetValueExByte Lib "advapi32" Alias "RegSetValueExA" _
  (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
   ByVal dwType As Long, szData As Byte, ByVal cbData As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long

Function SetLongDate(ByVal sValue As String) As Boolean
Dim key As Long, c As Long
    If RegOpenKeyEx(HKEY_CURRENT_USER, "Control Panel\International", 0, _
        KEY_ALL_ACCESS, key) = ERROR_SUCCESS Then
        c = Len(sValue) + 1
        SetLongDate = RegSetValueExStr(key, "sLongDate", 0, REG_SZ, sValue, c) = ERROR_SUCCESS
        RegCloseKey key
    End If
End Function

Sub test()
    If SetLongDate("MMMM dd, yyyy") Then
        MsgBox "SetLongDate thanh cong"
    End If
End Sub
 
Upvote 0
Bạn muốn chơi API hả? Thì API:
@ siwtom: thế mới thấy có những trường hợp dùng API dài lê thê mà cũng ko hiệu quả hơn gì, phải không?

@ campha: Nên hết sức thận trọng khi can thiệp vào Registry, lơ ngơ là “tèo” như chơi đấy!

Một số hướng dẫn xử lý Registry trong VB6 sưu tầm trên mạng (NGUỒN: caulacbovb) bạn có thể tham khảo và áp dụng

Mã:
1- Đầu tiên bạn tạo một biến đối tượng thuộc WScript.Shell như code sau:
[COLOR=#0000ff]Set obj = CreateObject("WScript.Shell")[/COLOR]

2- Muốn đọc Registry, bạn chi cần gọi phương thức [COLOR=#0000ff][B]obj.RegRead (Chuỗi Registry)[/B][/COLOR]

3- Muốn xóa Registry, bạn chi cần gọi phương thức [COLOR=#0000ff][B]obj.RegDelete (Chuỗi Registry)[/B][/COLOR]

4- Muốn viết Registry, bạn chi cần gọi phương thức [COLOR=#0000ff][B]obj.RegWrite (Chuỗi Registry, giá trị, kiểu chuỗi)[/B][/COLOR]

VD CHO 2-
Mã:
Dim obj, subject
Set obj = CreateObject("WScript.Shell")
subject = obj.RegRead  ("HKCU\Software\Policies\Microsoft\Internet Explorer\Control Panel\HomePage")

VD CHO 3-
Mã:
obj.RegDelete  "HKCU\Software\Policies\Microsoft\Internet Explorer\Control Panel\HomePage"

VD CHO 4- (Không cho phép thay đổi nội dung trang Homepage khi giá trị là 1)
Mã:
obj.RegWrite  "HKCU\Software\Policies\Microsoft\Internet Explorer\Control Panel\HomePage", 1



* Kỹ thuật WMI thao tác với Registry


1- Đầu tiên bạn cũng tạo một biến đối tượng, nhưng lần nầy lại gọi WMI theo code sau:
Mã:
Set objRegistry = GetObject("winmgmts:\\" & "." & "\root\default:StdRegProv")
'Const HKEY_CURRENT_USER = &H80000001: Const HKEY_LOCAL_MACHINE = &H80000002

2- Muốn đọc Registry, bạn chi cần gọi phương thức objRegistry.GetStringValue &H80000001
,strKeyPath,strValueName,strValue
(hay phương thức objRegistry.GetDWORDValue và dwValue)

3- Muốn xóa Registry, bạn chi cần gọi phương thức objRegistry.DeleteValue &H80000001, strKeyPath, strValueName

4- Muốn viết Registry, bạn chi cần gọi phương thức objRegistry.CreateKey &H80000001, strKeyPath
và gán giá trị với phương thức objRegistry.SetDWORDValue &H80000001, strKeyPath, strValueName, dwValue
hay objRegistry.SetStringValue &H80000001, strKeyPath, strValueName, strValue

strKeyPath là đường dẫn sau nhánh gốc của ô bên trái Registry Editor
strValueName là record trong cột Name của ô bên phải Registry Editor
strValue là record trong cột Data của ô bên phải Registry Editor
Phương thức GetDWORDValue, SetDWORDValue dwValue đi kèm dwValue
Phương thức GetStringValue, SetStringValue dwValue đi kèm strValue


* Đọc nhiều giá trị; gọi phương thức: objReg.EnumValues để tạo các tên khóa và giá trị đi kèm vào 2 mảng arrValueNames, arrValueTypes.
Mã:
objReg.EnumValues HKEY_CURRENT_USER, strKeyPath, arrValueNames, arrValueTypes
Sau đó tiến hành đọc theo phương thức như trên
Code đọc các giá trị khai báo trong Regional and Language Options như sau:
Mã:
Private Sub Form_Load()
Const HKEY_CURRENT_USER = &H80000001: Const HKEY_LOCAL_MACHINE = &H80000002
 
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & "." & "\root\default:StdRegProv")
strKeyPath = "Control Panel\International"
objReg.EnumValues HKEY_CURRENT_USER, strKeyPath, arrValueNames, arrValueTypes
 
For i = 0 To UBound(arrValueNames)
 a = a & "File Name: " & arrValueNames(i)
 objReg.GetStringValue HKEY_CURRENT_USER, strKeyPath, arrValueNames(i), arrValueTypes(i)
 a = a & vbTab & vbTab & "Value: " & arrValueTypes(i) & vbCrLf
Next
MsgBox a
End Sub

* Ghi theo kỹ thuật nhị phân (Write Binary Data to the Registry)
Ta tạo mảng gồm các số muốn ghi, vd
arrValues = Array(88, 205)
xong gọi phương thức objRegistry.SetBinaryValue
Mã:
Private Sub Form_Load()
Const HKEY_CURRENT_USER = &H80000001
Set objRegistry = GetObject("winmgmts:\\" & "." & "\root\default:StdRegProv")
 
strKeyPath = "Software"
strValueName = "BinaryTest"
arrValues = Array(88, 205)
 
errReturn = objRegistry.SetBinaryValue(HKEY_CURRENT_USER, strKeyPath, strValueName, arrValues)
End Sub

KÈM THEO 1 FILE SƯU TẦM - TÁC GIẢ VÕ QUANG HOÀ CÁC BẠN THAM KHẢO
 

File đính kèm

  • Registry in VISUAL BASIC - VO QUANG HOA.rar
    56.9 KB · Đọc: 19
Lần chỉnh sửa cuối:
Upvote 0
Bạn muốn chơi API hả? Thì API:

Mã:
Private Const KEY_ALL_ACCESS = &H3F
Private Const ERROR_SUCCESS = 0

Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_USERS = &H80000003

Private Const REG_SZ = (1)

Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" _
  (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
  ByVal samDesired As Long, phkResult As Long) As Long

Private Declare Function RegSetValueExStr Lib "advapi32" Alias "RegSetValueExA" _
  (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
   ByVal dwType As Long, ByVal szData As String, ByVal cbData As Long) As Long
   
Private Declare Function RegSetValueExLong Lib "advapi32" Alias "RegSetValueExA" _
  (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
   ByVal dwType As Long, szData As Long, ByVal cbData As Long) As Long
   
Private Declare Function RegSetValueExByte Lib "advapi32" Alias "RegSetValueExA" _
  (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
   ByVal dwType As Long, szData As Byte, ByVal cbData As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long

Function SetLongDate(ByVal sValue As String) As Boolean
Dim key As Long, c As Long
    If RegOpenKeyEx(HKEY_CURRENT_USER, "Control Panel\International", 0, _
        KEY_ALL_ACCESS, key) = ERROR_SUCCESS Then
        c = Len(sValue) + 1
        SetLongDate = RegSetValueExStr(key, "sLongDate", 0, REG_SZ, sValue, c) = ERROR_SUCCESS
        RegCloseKey key
    End If
End Function

Sub test()
    If SetLongDate("MMMM dd, yyyy") Then
        MsgBox "SetLongDate thanh cong"
    End If
End Sub
Nếu dùng API em thấy hàm này:
Mã:
Private Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" _
   (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Boolean
Xài cho trường hợp "cụ thể" này khá "ngon" đấy anh!
Ví dụ:
Mã:
Private Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" _
   (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Boolean
Private Declare Function GetUserDefaultLCID% Lib "kernel32" ()
Private Const LOCALE_SLONGDATE = &H20
Sub Main()
  Dim bRet As Boolean
  bRet = SetLocaleInfo(GetUserDefaultLCID(), LOCALE_SLONGDATE, "MMMM dd, yyyy")
End Sub
Anh nghĩ sao?
 
Lần chỉnh sửa cuối:
Upvote 0
Nếu dùng API em thấy hàm này:
Mã:
Private Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" _
   (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Boolean
Xài cho trường hợp "cụ thể" này khá "ngon" đấy anh!
Ví dụ:
Mã:
Private Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" _
   (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Boolean
Private Declare Function GetUserDefaultLCID% Lib "kernel32" ()
Private Const LOCALE_SLONGDATE = &H20
Sub Main()
  Dim bRet As Boolean
  bRet = SetLocaleInfo(GetUserDefaultLCID(), LOCALE_SLONGDATE, "MMMM dd, yyyy")
End Sub
Anh nghĩ sao?

Dĩ nhiên là được. Bản thân tôi đã từng dùng code

Mã:
Public Function SetCPsDateTime(ByVal sdateformat As String, Optional ByVal stimeformat As String = "") As Boolean
Dim locale As Long, Arr() As Byte
    If sdateformat = "" And stimeformat = "" Then Exit Function
    
    locale = GetSystemDefaultLCID()
    If sdateformat <> "" Then
        If SetLocaleInfo(locale, LOCALE_SSHORTDATE, sdateformat) = False Then Exit Function
    End If
    If stimeformat <> "" Then
        If SetLocaleInfo(locale, LOCALE_STIMEFORMAT, stimeformat) = False Then Exit Function
    End If
    
    SetDateTime = True
...

Tuấn không nhớ à?

Tôi thấy chủ topic nhận được câu trả lời giải quyết được vấn đề rồi nhưng vẫn muốn biết thêm về API. Tôi hiểu là muốn biết về cách thao tác trực tiếp với Registry. Vì thế mà tôi đưa code đọc Registry. Mà code đó không phụ thuộc vào LongDate hay thậm chí là Date. Hiểu được thì đọc những cái khác tương tự.
Còn Set/GetLocaleInfo là gắn liền với chủ đề Locale. Thứ nữa là hiểu được nó cũng chưa có nghĩa là hiểu được cách ghi vào Registry dùng API.

Nếu chỉ cần được việc thì cả 2 code đều được. Mà được việc thì ngay code ở bài #2 đã làm được việc rồi. Nhưng nếu nói về thao tác với Registry thì chỉ có code của tôi. Tùy mỗi người chủ ý nói về vấn đề gì.
 
Upvote 0
Thêm 1 cách thao tác (sưu tầm của JohnWalkenbach)
Mã:
Option Explicit

Private Declare Function RegOpenKeyA Lib "ADVAPI32.DLL" _
    (ByVal hKey As Long, ByVal sSubKey As String, _
    ByRef hkeyResult As Long) As Long

Private Declare Function RegCloseKey Lib "ADVAPI32.DLL" _
    (ByVal hKey As Long) As Long

Private Declare Function RegSetValueExA Lib "ADVAPI32.DLL" _
    (ByVal hKey As Long, ByVal sValueName As String, _
    ByVal dwReserved As Long, ByVal dwType As Long, _
    ByVal sValue As String, ByVal dwSize As Long) As Long

Private Declare Function RegCreateKeyA Lib "ADVAPI32.DLL" _
    (ByVal hKey As Long, ByVal sSubKey As String, _
    ByRef hkeyResult As Long) As Long

Private Declare Function RegQueryValueExA Lib "ADVAPI32.DLL" _
    (ByVal hKey As Long, ByVal sValueName As String, _
    ByVal dwReserved As Long, ByRef lValueType As Long, _
    ByVal sValue As String, ByRef lResultLen As Long) As Long


[B]Sub UpdateRegistryWithTime()[/B]
    Dim RootKey As String
    Dim Path As String
    Dim RegEntry As String
    Dim RegVal As Date
    Dim LastTime As String
    Dim Msg As String
    
    RootKey = "hkey_current_user"
    Path = "software\microsoft\office\10.0\excel\LastStarted"
    RegEntry = "DateTime"
    RegVal = Now()
    
    LastTime = GetRegistry(RootKey, Path, RegEntry)
    Select Case LastTime
        Case "Not Found"
            Msg = "This routine has not been executed before."
        Case Else
            Msg = "This routine was lasted executed: " & LastTime
    End Select
    Msg = Msg & Chr(13) & Chr(13)
    
    Select Case WriteRegistry(RootKey, Path, RegEntry, RegVal)
        Case True
            Msg = Msg & "The registry has been updated with the current date and time."
        Case False
            Msg = Msg & "An error occured writing to the registry..."
    End Select
    MsgBox Msg, vbInformation, "Registry Demo"
End Sub

[B]Private Function GetRegistry(Key, Path, ByVal ValueName As String)[/B]
'  Reads a value from the Windows Registry

    Dim hKey As Long
    Dim lValueType As Long
    Dim sResult As String
    Dim lResultLen As Long
    Dim ResultLen As Long
    Dim x, TheKey As Long

    TheKey = -99
    Select Case UCase(Key)
        Case "HKEY_CLASSES_ROOT": TheKey = &H80000000
        Case "HKEY_CURRENT_USER": TheKey = &H80000001
        Case "HKEY_LOCAL_MACHINE": TheKey = &H80000002
        Case "HKEY_USERS": TheKey = &H80000003
        Case "HKEY_CURRENT_CONFIG": TheKey = &H80000004
        Case "HKEY_DYN_DATA": TheKey = &H80000005
    End Select
    
    If TheKey = -99 Then
        GetRegistry = "Not Found"
        Exit Function
    End If

    If RegOpenKeyA(TheKey, Path, hKey) <> 0 Then _
        x = RegCreateKeyA(TheKey, Path, hKey)
    
    sResult = Space(100)
    lResultLen = 100
    
    x = RegQueryValueExA(hKey, ValueName, 0, lValueType, _
    sResult, lResultLen)
        
    Select Case x
        Case 0: GetRegistry = Left(sResult, lResultLen - 1)
        Case Else: GetRegistry = "Not Found"
    End Select
    
    RegCloseKey hKey
End Function

[B]Private Function WriteRegistry(ByVal Key As String, _
    ByVal Path As String, ByVal entry As String, _
    ByVal value As String)[/B]
    
    Dim hKey As Long
    Dim lValueType As Long
    Dim sResult As String
    Dim lResultLen As Long
    Dim TheKey As Long
    Dim x
    
   
    TheKey = -99
    Select Case UCase(Key)
        Case "HKEY_CLASSES_ROOT": TheKey = &H80000000
        Case "HKEY_CURRENT_USER": TheKey = &H80000001
        Case "HKEY_LOCAL_MACHINE": TheKey = &H80000002
        Case "HKEY_USERS": TheKey = &H80000003
        Case "HKEY_CURRENT_CONFIG": TheKey = &H80000004
        Case "HKEY_DYN_DATA": TheKey = &H80000005
    End Select
    
    If TheKey = -99 Then
        WriteRegistry = False
        Exit Function
    End If

    If RegOpenKeyA(TheKey, Path, hKey) <> 0 Then
        x = RegCreateKeyA(TheKey, Path, hKey)
    End If

    x = RegSetValueExA(hKey, entry, 0, 1, value, Len(value) + 1)
    If x = 0 Then WriteRegistry = True Else WriteRegistry = False
End Function

[B]Sub test()[/B]
    Dim RootKey As String
    Dim Path As String
    Dim RegEntry As String
    
    RootKey = "HKEY_CURRENT_USER"
    Path = "Control Panel\International"
    RegEntry = "sLongDate"
    MsgBox GetRegistry(RootKey, Path, RegEntry), _
      vbInformation, Path & "\RegEntry"
End Sub
 
Upvote 0
Web KT

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

Back
Top Bottom