Laurence Martin
asked on
Error Trapping in a function
Hi,
This code crashes because "My Documents" should be "Documents" on some systems with error code 1004.
How do I trap the error?
Sub OpenQuoteTemplate()
Set objQuoteTemp = Workbooks.Add(Template:=Qu oteTemplat ePath(stVe rsion))
End sub
Function QuoteTemplatePath(OCVersio n)
QuoteTemplatePath = Environ("UserProfile") & "\My documents\Quote Processing\John Lewis Quote Template vN JLP Classic Shutters.xltx"
End Function
I've tried putting error traps in both the function and the procedure but neither seem to be triggered.
Thanks
This code crashes because "My Documents" should be "Documents" on some systems with error code 1004.
How do I trap the error?
Sub OpenQuoteTemplate()
Set objQuoteTemp = Workbooks.Add(Template:=Qu
End sub
Function QuoteTemplatePath(OCVersio
QuoteTemplatePath = Environ("UserProfile") & "\My documents\Quote Processing\John Lewis Quote Template vN JLP Classic Shutters.xltx"
End Function
I've tried putting error traps in both the function and the procedure but neither seem to be triggered.
Thanks
Also see this link for an alternative to rorya's suggestion http://www.cpearson.com/excel/SpecialFolders.aspx
Using the code on that website (one of the best on Excel programming!) you could do the following:
Using the code on that website (one of the best on Excel programming!) you could do the following:
Function QuoteTemplatePath(OCVersion)
QuoteTemplatePath = GetSpecialFolder(&H5) & "\Quote Processing\John Lewis Quote Template vN JLP Classic Shutters.xltx"
End Function
ASKER
Thanks guys for the solution to the error, but what about the answer to the question?
How about this:
Function QuoteTemplatePath(OCVersion)
On Error GoTo err_handler
QuoteTemplatePath = Environ("UserProfile") & "\My documents\Quote Processing\John Lewis Quote Template vN JLP Classic Shutters.xltx"
err_handler:
If Err.Number = 1004 Then
QuoteTemplatePath = Environ("UserProfile") & "\Documents\Quote Processing\John Lewis Quote Template vN JLP Classic Shutters.xltx"
End If
End Function
btw, if you feel your question wasn't answered you shouldn't accept the answers.
I recommend re-opening the question.
I recommend re-opening the question.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No more comments were recieved
Open in new window