Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Workaround for ChDir/CurDir when path has unicode characters

Hi guys, long time.

Problem:
VBA.Filesystem works with ansi paths, not with unicode paths.
e.g. CurDir/ChDir and several other methods return converted strings,
without the actual unicode characters.

For testing:
(English WindowsXP and Excel 97 thru 2007 (or actually VBA 6.5.1020))


Create a folder with a unicode character in the name: Arab?Test
where the ? is unicode x06E9, which looks nice in CharMap,
but any extended unicode character will do.

In that folder create a file with another "foreign" char.

Excel has no problems changing folders and opening/saving files.
However VBA is thoroughly confused.
When I've navigated to the "culprit" folder via excel's File/Open
dialog and i try following in VBE immediate:

ChDir CurDir

I get error 76 path not found!


I've done some research and experimenting:
created following workarounds:

With New Scripting.FileSystemObject
'Retrieve the full unicode string for CurDir.
szCurDir = .GetAbsolutePathName(Vba.CurDir$)
'Change to this folder using short 8.3 name.
vba.ChDir .GetShortPath(vba.CurDir$)
End With

I need a trick for ChDir without short (8.3) paths
(looks terrible AND can be turned of in NTFS)...

I dont mind a ton of apis but it must be foolproof.

--
keepITcool

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Workaround for ChDir/CurDir when path has unicode characters

Excel's VBA is converting strings to ANSI before calling windows API functions.
Try calling the "Unicode version" of the Win32 API directly.
Here is an example:

Option Explicit

Private Declare Function GetCurrentDirectory Lib "kernel32" _
Alias "GetCurrentDirectoryW" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Declare Function SetCurrentDirectory Lib "kernel32" _
Alias "SetCurrentDirectoryW" (ByVal lpBuffer As String) As Long

Sub test()
Dim buff As String * 420
Dim len_dir, str_end, old_dir

old_dir = CurDir
str_end = StrConv("\Arab" & ChrW(1769) & "Test" & Chr(0), vbUnicode)

Debug.Print CurDir
len_dir = GetCurrentDirectory(200, buff)
Debug.Print len_dir, "[ " & Left(buff, len_dir + len_dir) & " ]"
Debug.Print SetCurrentDirectory(Left(buff, len_dir + len_dir) & str_end)
Debug.Print CurDir
len_dir = GetCurrentDirectory(200, buff)
Debug.Print len_dir, "[ " & Left(buff, len_dir + len_dir) & " ]"
ChDir old_dir
End Sub
--
Regards,
Luc.

"Festina Lente"


"keepITcool" wrote:

Hi guys, long time.

Problem:
VBA.Filesystem works with ansi paths, not with unicode paths.
e.g. CurDir/ChDir and several other methods return converted strings,
without the actual unicode characters.

For testing:
(English WindowsXP and Excel 97 thru 2007 (or actually VBA 6.5.1020))


Create a folder with a unicode character in the name: Arab?Test
where the ? is unicode x06E9, which looks nice in CharMap,
but any extended unicode character will do.

In that folder create a file with another "foreign" char.

Excel has no problems changing folders and opening/saving files.
However VBA is thoroughly confused.
When I've navigated to the "culprit" folder via excel's File/Open
dialog and i try following in VBE immediate:

ChDir CurDir

I get error 76 path not found!


I've done some research and experimenting:
created following workarounds:

With New Scripting.FileSystemObject
'Retrieve the full unicode string for CurDir.
szCurDir = .GetAbsolutePathName(Vba.CurDir$)
'Change to this folder using short 8.3 name.
vba.ChDir .GetShortPath(vba.CurDir$)
End With

I need a trick for ChDir without short (8.3) paths
(looks terrible AND can be turned of in NTFS)...

I dont mind a ton of apis but it must be foolproof.

--
keepITcool


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Workaround for ChDir/CurDir when path has unicode characters

Fantastic. Thx.
--
Jurgen


PapaDos wrote:

Excel's VBA is converting strings to ANSI before calling windows API
functions. Try calling the "Unicode version" of the Win32 API
directly. Here is an example:

Option Explicit

Private Declare Function GetCurrentDirectory Lib "kernel32" _
Alias "GetCurrentDirectoryW" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Declare Function SetCurrentDirectory Lib "kernel32" _
Alias "SetCurrentDirectoryW" (ByVal lpBuffer As String) As Long

Sub test()
Dim buff As String * 420
Dim len_dir, str_end, old_dir

old_dir = CurDir
str_end = StrConv("\Arab" & ChrW(1769) & "Test" & Chr(0),
vbUnicode)
Debug.Print CurDir
len_dir = GetCurrentDirectory(200, buff)
Debug.Print len_dir, "[ " & Left(buff, len_dir + len_dir) & " ]"
Debug.Print SetCurrentDirectory(Left(buff, len_dir + len_dir) &
str_end) Debug.Print CurDir
len_dir = GetCurrentDirectory(200, buff)
Debug.Print len_dir, "[ " & Left(buff, len_dir + len_dir) & " ]"
ChDir old_dir
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Workaround for ChDir/CurDir when path has unicode characters

You are welcome.

Just out of curiousity, which version of Windows and Excel have you tried it
on ?

I used Windows XP Pro SP2 and Excel 2002.
--
Regards,
Luc.

"Festina Lente"


"keepITcool" wrote:

Fantastic. Thx.
--
Jurgen


PapaDos wrote:

Excel's VBA is converting strings to ANSI before calling windows API
functions. Try calling the "Unicode version" of the Win32 API
directly. Here is an example:

Option Explicit

Private Declare Function GetCurrentDirectory Lib "kernel32" _
Alias "GetCurrentDirectoryW" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Declare Function SetCurrentDirectory Lib "kernel32" _
Alias "SetCurrentDirectoryW" (ByVal lpBuffer As String) As Long

Sub test()
Dim buff As String * 420
Dim len_dir, str_end, old_dir

old_dir = CurDir
str_end = StrConv("\Arab" & ChrW(1769) & "Test" & Chr(0),
vbUnicode)
Debug.Print CurDir
len_dir = GetCurrentDirectory(200, buff)
Debug.Print len_dir, "[ " & Left(buff, len_dir + len_dir) & " ]"
Debug.Print SetCurrentDirectory(Left(buff, len_dir + len_dir) &
str_end) Debug.Print CurDir
len_dir = GetCurrentDirectory(200, buff)
Debug.Print len_dir, "[ " & Left(buff, len_dir + len_dir) & " ]"
ChDir old_dir
End Sub


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Workaround for ChDir/CurDir when path has unicode characters


That VBA bug exists since xl97 and is still present in xl2007
I'm using XP Pro SP2.

Those APIs will not work on some Win95/Win98 systems, but those do not
matter to me. And I've no doubt these APIs will work on Vista.

I was so busy using SHLWAPI i forgot to do the obvious and properly
browse ApiGuide/ApiViewer.

--



PapaDos wrote:

You are welcome.

Just out of curiousity, which version of Windows and Excel have you
tried it on ?

I used Windows XP Pro SP2 and Excel 2002.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to convert Characters between Unicode and ANSI FcoMariscal Excel Programming 0 August 24th 06 10:47 PM
Extra characters exported in unicode(.txt) format PT Excel Discussion (Misc queries) 0 February 7th 06 11:29 AM
inserting unicode characters Wazooli Excel Discussion (Misc queries) 1 February 7th 05 06:54 PM
How do I enter Unicode characters in Excel? MacroAlan[_4_] Excel Programming 0 February 5th 05 08:25 PM
chdir not accepting string for path Alan Excel Programming 1 October 13th 04 06:18 PM


All times are GMT +1. The time now is 10:15 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"