Solved

Excel Crashes Running VBA Code

Posted on 2014-07-21
9
636 Views
Last Modified: 2014-07-28
The below code crashes excel 99% percent of the time and I'm not sure why. It will open the groupwise email client but then excel will crash. Since the email message does open, I just close excel but it's a real issue. Any ideas why it may be happening? Thanks!

Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long



Option Explicit

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim lngResponse As Long
Dim URL As String, Email As String, Subj As String, body As String, Msg As String
Dim TheDate As String
Dim TheTime As String
Dim Greeting As String
Dim TrgtRow As Long

    'RB, Q: should the range extend to row 27 (as below) or to row 26 as stated in the question?
    If Intersect(Target, Range("F2:F27")) Is Nothing Then
        Exit Sub
    Else

        TrgtRow = Target.Row
        Email = Range("$E$" & TrgtRow).Value
        TheDate = Format(Date, "Long Date")
        TheTime = Format(Time, "Medium Time")

        ' Determine greeting based on time
        Select Case Time
            Case Is < 0.5: Greeting = "Good Morning"
            Case Is >= 0.7083: Greeting = "Good Evening"
            Case Else: Greeting = "Good Afternoon"
        End Select

        lngResponse = MsgBox("You are about to send an email with a link to course information. Would you like to continue?", vbYesNo)
        If lngResponse = vbYes Then
            'Replace spaces with %20 (hex)
            Subj = "Course Information for " & Range("$D$" & TrgtRow).Value
            Subj = Application.WorksheetFunction.Substitute(Subj, " ", "%20")

            'in EE Q, if F2:F26 (general course categories)
            Msg = "Please visit the following link to view information and register for the course you requested: " _
                  & "%0D%0A" & "%0D%0A" & Range("$L$" & TrgtRow).Value
            '& "%0D%0A" _
             & "%0D%0A" & " If we can be of further assistance, please contact us." & " Thank you."

            If Target.Address = "$F$27" Then
                ''in EE Q, if instersect range is F7 (with an option to only send the above code - this range (F7) _
                 ''will include a specific course and can include a course book but want to give the user an option _
                 ''to not include the course book link if student doesn't need or has course book):
                'Msg = "Please visit the following link to view information and register for the course you requested: " _
                 & "%0D%0A" & "%0D%0A" & Range("$L$" & TrgtRow).Value
                lngResponse = MsgBox("Do you want to include the course book link?", vbYesNo)
                If lngResponse = vbYes Then
                    Msg = Msg & "%0D%0A" & "%0D%0A" & "Your course books can be viewed here: " _
                          & "%0D%0A" & "%0D%0A" & Range("$M$" & TrgtRow).Value
                    '& "%0D%0A" & "%0D%0A" & " If we can be of further assistance, please contact us." & " Thank you."
                End If
            End If
            Msg = Msg & "%0D%0A" & "%0D%0A" & " If we can be of further assistance, please contact us." & " Thank you."

'            'in the original code:
'            Msg = "Please visit the following link to view information and register for the course you requested: " _
'                  & "%0D%0A" & "%0D%0A" & Range("$L$" & TrgtRow).Value & "%0D%0A" _
'                  & "%0D%0A" & "Your course books can be viewed here: " & "%0D%0A" & "%0D%0A" & Range("$M$" & TrgtRow).Value _
'                  & "%0D%0A" & "%0D%0A" & " If we can be of further assistance, please contact us." & " Thank you."
            Msg = Application.WorksheetFunction.Substitute(Msg, " ", "%20")
            'Replace carriage returns with %0D%0A (hex)
            Msg = Application.WorksheetFunction.Substitute(Msg, vbCrLf, "%0D%0A")

            'Create the URL
            URL = "mailto:" & Email & "?subject=" & Subj & "&body=" & Greeting & "," & "%0D%0A" & "%0D%0A" & Msg & "%0D%0A" & "%0D%0A"

            'Execute the URL (start the email client)
            ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus

        End If
    End If

End Sub

Open in new window

0
Comment
Question by:tracyms
9 Comments
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40210591
Does Excel give any error message? Have you tried adding a DoEvents line after line 73?
0
 
LVL 1

Author Comment

by:tracyms
ID: 40210598
LCID:   1033
skulcid:   1033

Above is error, and how do I add a "DoEvents" line? Thanks.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40210670
No points for me...
RE: DoEvents..

After the following pair of lines (71-72)
            'Create the URL
            URL = "mailto:" & Email & "?subject=" & Subj & "&body=" & Greeting & "," & "%0D%0A" & "%0D%0A" & Msg & "%0D%0A" & "%0D%0A"

Open in new window

type in
DoEvents
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 69

Expert Comment

by:Qlemo
ID: 40211178
What you see isn't an error message, it is the output from something telling you which language is used. 1033 is en-US (see http://msdn.microsoft.com/en-us/goglobal/bb964664.aspx).

What do you mean with "Excel crashes"?
0
 
LVL 1

Author Comment

by:tracyms
ID: 40211691
See attached screenshot for crash when I double click link to send email (see last column in screenshot). The email client does open with the message but excel crashes. The "DoEvents" didn't work, it still crashed when I added it.
Crash.pdf
0
 
LVL 1

Author Comment

by:tracyms
ID: 40211703
Actual message when crashes:

Message.JPG
0
 
LVL 1

Accepted Solution

by:
tracyms earned 0 total points
ID: 40212367
I added  "ActiveSheet.Range("A2").Select"  to the end of the code:

  'Execute the URL (start the email client)
            ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus

        End If
    End If

  ActiveSheet.Range("A2").Select

End Sub

It hasn't crashed yet - found information at this link after more research:

http://social.msdn.microsoft.com/Forums/office/en-US/20e73d3c-1fea-4073-8b22-751b88e3410e/excel-2010-xlsm-code-crashing?forum=exceldev
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40212986
Strange workaround. But if it helps ...
0
 
LVL 1

Author Closing Comment

by:tracyms
ID: 40223724
I searched the net and found a solution that seems to be working. I appreciate everyone's help!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
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…

777 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