Solved

Error Trapping in a function

Posted on 2013-06-21
9
157 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to line 23 53
Can't get rid of Update Links message in Excel 2013 8 29
List Box Entries Excel 2010 5 29
Highlight changing numbers in column 3 18
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

839 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