Change the system Region/Location setting using vbs

筅森魡賤 提交于 2019-12-02 01:01:55

问题


I was wondering if perhaps somebody could assist with a query I have.

Once a week i get file from my US counterpart that i need to save locally to my machine, and change the format of the file. I have since created a VBScript that can perform a SaveAs routine and save it, however in order to retain the date formats in US, I am having to change my Region setting to US in control panel before the procedure and after to change to default local setting.

I have been researching this and got some ideas from the Microsoft site that shows how to use the Set Locale and Get locale, but my code doesnt seem to actually change anything, even though it executes without error.

Can somebody please give me some pointers...

option explicit
dim currentLocale
currentLocale = GetLocale()

SetLocale 1033       ' 1033 is the EN-US locale

' Revert back to the default system locale
SetLocale currentLocale

回答1:


Option Explicit

'Define a key registry path
Dim strComputer
Dim objRegistry
Dim strKeyPath
Dim strValueName
Dim getValue
Dim regKeyPath
Dim strLocaleName, strCountry, strshortDateValue, strlongDateValue, strshortTimeValue, strlongTimeValue, strfirstDayOfWeekValue

Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
regKeyPath = "Control Panel\International"

strLocaleName = "en-US" 
strCountry = "United States" 
strshortDateValue = "M/d/yyyy"
strlongDateValue = "dddd, MMMM d, yyyy"
strshortTimeValue = "h:mm tt"
strlongTimeValue = "h:mm:ss tt"
strfirstDayOfWeekValue = "6"

objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "LocaleName", strLocaleName
objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "sCountry", strCountry
objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "sShortDate", strshortDateValue
objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "sLongDate", strlongDateValue
objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "sShortTime", strshortTimeValue
objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "sTimeFormat", strlongTimeValue
objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "iFirstDayOfWeek", strfirstDayOfWeekValue


WScript.Echo "Successfully changed system regional settings."

This script will change your regional settings in registry. Use with caution. This will change to US, so if you intend on using this script, make sure you know what the default settings are before you change them. Merely reverting the settings in Control Panel will not work.

The script below will change everything back to UK again.

Option Explicit

'Define a key registry path
Dim strComputer
Dim objRegistry
Dim strKeyPath
Dim strValueName
Dim getValue
Dim regKeyPath
Dim strLocaleName, strCountry, strshortDateValue, strlongDateValue, strshortTimeValue, strlongTimeValue, strfirstDayOfWeekValue

Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
regKeyPath = "Control Panel\International"

strLocaleName = "en-GB" 
strCountry = "United Kingdom" 
strshortDateValue = "dd/MM/yyyy"
strlongDateValue = "dd MMMM yyyy"
strshortTimeValue = "HH:mm"
strlongTimeValue = "HH:mm:ss"
strfirstDayOfWeekValue = "0"

objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "LocaleName", strLocaleName
objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "sCountry", strCountry
objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "sShortDate", strshortDateValue
objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "sLongDate", strlongDateValue
objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "sShortTime", strshortTimeValue
objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "sTimeFormat", strlongTimeValue
objRegistry.SetStringValue HKEY_CURRENT_USER, regKeyPath, "iFirstDayOfWeek", strfirstDayOfWeekValue


WScript.Echo "Successfully changed system regional settings."


来源:https://stackoverflow.com/questions/32779094/change-the-system-region-location-setting-using-vbs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!