Solved

Error Trapping in a function

Posted on 2013-06-21
9
144 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 26

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 26

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 26

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

746 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

9 Experts available now in Live!

Get 1:1 Help Now