Solved

Error Trapping in a function

Posted on 2013-06-21
9
155 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:LJKMartin
  • 3
  • 3
9 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39265490
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

0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39265587
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

0
 

Author Comment

by:LJKMartin
ID: 39280430
Thanks guys for the solution to the error, but what about the answer to the question?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:MacroShadow
ID: 39280448
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

0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39280451
btw, if you feel your question wasn't answered you shouldn't accept the answers.

I recommend re-opening the question.
0
 

Accepted Solution

by:
LJKMartin earned 0 total points
ID: 39283669
Firstly, apologies for causing such confusion.

I accepted the solutions from the experts because I was in a hurry and felt that fixing the problem is, obviously, better than just trapping the error.  I was, however, still interested in how the error should have been trapped.

I can't see a way of re-opening the question - should I really delete it?

Thank-you
0
 

Author Closing Comment

by:LJKMartin
ID: 39674082
No more comments were recieved
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now