Excel crashes when I frequently call this function

Hi,

I m using the Sub as shown in the code section but I call it frequently it application crashes
Is windows API the reason?
I have added the 'Microsoft scripting runtime' library as well

Can one please tell me if the below function could crash the applicaiton
Other it may be the other part of the coee that is cauing it.
If yes, can one please provide me with an alternative.
But I cant use the 'ChDrive' , 'Chdir' either
As I not going to a directory but a global path insted.
Such as \\servername\test\folder..

Please let me know if oyu need further understanding of the issue.

thanks
Public Sub selectFileSource(path As Range, file As Range)


On Error GoTo error_handler

'other way of declaring string pointer strCurrDir = String(255, 0)
    'Application.Dialogs(xlDialogOpen).Show (Range("rngHubPath"))
    GetCurrentDirectory 260, strCurrDir
    SetCurrentDirectory path
    strFileName = Application.GetOpenFilename
    SetCurrentDirectory Trim(strCurrDir)
    file = strFileName
    'Controller path, file
    
normal_exit:
Exit Sub

error_handler:
MsgBox "An error has occured in the selectFileSource() method " & Err.Description, vbCritical, strMsgBoxTitle
Resume normal_exit
    
   ' MsgBox strFileName
End Sub

Open in new window

SameerMirzaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gowflowCommented:
well to answer you we need to look at following subs or functions
GetCurrentDirectory
SetCurrentDirectory

gowflow

0
SameerMirzaAuthor Commented:
Public Declare Function GetCurrentDirectory Lib "kernel32.dll" Alias "GetCurrentDirectoryA" ( _
     ByVal nBufferLength As Long, _
     ByVal lpBuffer As String) As Long
     
     
Public Declare Function SetCurrentDirectory Lib "kernel32.dll" Alias "SetCurrentDirectoryA" ( _
     ByVal lpPathName As String) As Long

Please let me know if you require more information.

Thanks
0
gowflowCommented:
try replacing
SetCurrentDirectory path
by either this
SetCurrentDirectory path.value
or either this
SetCurrentDirectory path.text

gowflow
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

aikimarkCommented:
If you use Application.FileDialog instead of Application.GetOpenFilename, you can set the initial directory via the InitialFileName property.
0
aikimarkCommented:
When state that you call this function "frequently", how often is that?

You might need to instantiate a reusable object at a higher level and pass it into this routine as a parameter.  In one of my commercial applications, I did this to speed up the encryption/decryption routine.  The class that does the heavy lifting takes a while to initialize and I saved my code the overhead of multiple initializations by passing a class variable to the routine.
0
James ElliottManaging DirectorCommented:

gowflow:try replacing
SetCurrentDirectory path
by either this
SetCurrentDirectory path.value
or either this
SetCurrentDirectory path.text


Or changing the parameter types would work...

Public Sub selectFileSource(path As String, file As String)

Open in new window


Call like this...

selectFileSource([A1].text, [A2].text)

Open in new window


jell


0
SameerMirzaAuthor Commented:
Hi,
thanks everyone for help.
I used the same function.
But have increasd the size of strCurrDir and secondly clear the buffer after every run. And it seems to work fine. :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gowflowCommented:
good for you
gowflow
0
SameerMirzaAuthor Commented:
thanks to all experts for their input.
But as I have accepted my own solution because it worked
Hope thats ok
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.