Excel crashes when I frequently call this function


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.

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
Exit Sub

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

Open in new window

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.

well to answer you we need to look at following subs or functions


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.

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

Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

If you use Application.FileDialog instead of Application.GetOpenFilename, you can set the initial directory via the InitialFileName property.
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.
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


SameerMirzaAuthor Commented:
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. :)

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
good for you
SameerMirzaAuthor Commented:
thanks to all experts for their input.
But as I have accepted my own solution because it worked
Hope thats ok
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.