レジストリキーを開閉するコードブロックを使用して、適切なファイルパスを選択できるようにユーザーの場所を特定する情報を見つけますデータファイルを開くとき。 Windows XPではOffice 2002と2007でうまく動作しますが、Excel 2010のWindows 7の32ビット版または64ビット版では機能しません。
これを行うには何を変更する必要があるのですか?Win7/Excel 2010で動作しないVBAコードがXP/Excel 2007で動作する
'\* Module Level Constant Declarations follow...
Private Const cvarRegistrySize = 1
Private Const cvarHkeyLocalMachine = &H80000002
Private Const cvarKeyQueryValue = &H2
'\* Private API Function Declarations follow...
Declare PtrSafe Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (_
ByVal hKey As LongPtr, ByVal lpSubKey As String, ByVal ulOptions As Long, _
ByVal samDesired As Long, phkResult As LongPtr) As Long
Declare PtrSafe Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (_
ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal lpReserved As LongPtr, _
lpType As Long, lpData As Any, lpcbData As Long) As Long
Declare PtrSafe Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As LongPtr) As Long
'\* Dimension variables at module level...
Private strSearchKey As String
Private strRegion As String
Private intCharLen As Integer
Private intSubChar As Integer
Private lngRegKey As Long
Private lngSizeVar As Long
Private lngReturnCode As Long
'****************************************************************************
'* Function to extract the current region from the registry *
'****************************************************************************
Function GETREGION() As String
'\* registry key for user's location...
strSearchKey = "SOFTWARE\CompanyName\LogonProcess"
'\* open registry key...
lngReturnCode = RegOpenKeyEx(cvarHkeyLocalMachine, strSearchKey, 0, cvarKeyQueryValue, lngRegKey) 'returns 2
'\* return value from specified key...
strSearchKey = "CurrentLocation"
'\* return section of string from specified key...
strRegion = String(20, 32)
'\* returns the length of the string...
lngSizeVar = Len(strRegion) - 1
'\* query the registry key...
lngReturnCode = RegQueryValueEx(lngRegKey, strSearchKey, 0, cvarRegistrySize, ByVal strRegion, lngSizeVar) 'returns 6
'\* close the registry key...
Call RegCloseKey(lngRegKey)
'\* select the location from the string...
lngReturnCode = GETSTR(GETREGION, strRegion, 1, vbNullChar)
'\* return result to function as uppercase...
GETREGION = StrConv(GETREGION, vbUpperCase)
End Function
'****************************************************************************
'* Function to extract a section from a string from a given start position *
'* up to a specified character. *
'****************************************************************************
Function GETSTR(strX As String, strY As String, intStartPos As Integer, intSearchChar As String) As Integer
'\* initialisation of variables follows...
GETSTR = intStartPos
strX = ""
intCharLen = Len(strY)
intSubChar = intStartPos
'\* if comparison character at start position then leave function with empty extracted string... *
If Mid(strY, intStartPos, 1) = intSearchChar Then Exit Function
'\* begin loop...
Do
'\* create integer value based on character positions...
strX = strX + Mid(strY, intSubChar, 1)
'\* increment counter...
intSubChar = intSubChar + 1
'\* if counter exceeds string length, exit loop...
If intSubChar > intCharLen Then Exit Do
'\* define loop conditions...
Loop Until Mid(strY, intSubChar, 1) = intSearchChar
'\* return character position to function...
GETSTR = intSubChar
End Function
それは、このコードがExcelの一部は、アドインに配備されている機能で使用されているとして、それが私たちの新しいデスクトップイメージのロールアウトをバック保持していてもよいように私はこの問題を解決することが重要になってきていますすべてのマシンで使用され、多数のアソシエートによって使用されます。
RegOpenKeyExとRegQueryValueExのリターンコードはそれぞれ2と6が私を投げているものです。
おかげで、事前に
マーティン
優秀!どうもありがとう。私はそれを変更しようとしましたが、本当に混乱しているのは、$ H20019を使用していて、KEY_READで$ H20019をスワップすると、オンラインでKEY_READをパラメータとして使用する例がたくさんあります。また、32ビットと64ビットのWindoes環境で動作するようにしたいと思っていますが、VBAではORセパレータも失敗します。おかげで再びマーティン –
あなたはそれを定義していますか? 'public const KEY_READ as long =&h20019' –
いいえ、バリアントとして、つまり型が定義されていません。プライベートConst cvarKeyQueryValue =&H20019 –