Link to home
Start Free TrialLog in
Avatar of Laurence Martin
Laurence MartinFlag for United Kingdom of Great Britain and Northern Ireland

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:=QuoteTemplatePath(stVersion))

End sub
Function QuoteTemplatePath(OCVersion)

        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
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

On Windows machines you should be able to use:
createobject("Wscript.shell").specialfolders("MyDocuments") & "\Quote Processing\John Lewis Quote Template vN JLP Classic Shutters.xltx"

Open in new window

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:
Function QuoteTemplatePath(OCVersion)

    QuoteTemplatePath = GetSpecialFolder(&H5) & "\Quote Processing\John Lewis Quote Template vN JLP Classic Shutters.xltx"

End Function

Open in new window

Avatar of Laurence Martin

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

Open in new window

btw, if you feel your question wasn't answered you shouldn't accept the answers.

I recommend re-opening the question.
ASKER CERTIFIED SOLUTION
Avatar of Laurence Martin
Laurence Martin
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No more comments were recieved