Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error Trapping in a function

Posted on 2013-06-21
9
Medium Priority
?
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

715 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