Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook VBA insert subjectline using input boxes

Status
Not open for further replies.

Turtleman10

Technical User
Sep 13, 2012
40
US
I am very new to this so please excuse my ignorance.
I am trying to implement this code

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

'Declaration
Dim strSubject As String
Dim strPartN As String
Dim strShipDT As String
Dim strPO As String
Dim strDesc As String
Dim strPiec As String


strSubject = Item.Subject

If strSubject = "KEC" Then

strPartN = InputBox("Part Number", "Please insert Part number")

strShipDT = InputBox("Ship Date", "Please insert Ship date")

strPO = InputBox("P.O", "Please insert P.O number")

strSubject = "PN" + strPartN + "_SD_" + strShipDT + "_PO_" + strPO

End If

If strSubject = "GNC" Then

strPartN = InputBox("Part Number", "Please insert Part number")

strDesc = InputBox("Part Description", "Please insert Part Description")

strPO = InputBox("P.O", "Please insert P.O number")

strPiec = InputBox("Number of Pieces", "Please insert number of Pieces")

strShipDT = InputBox("Ship Date", "Please insert Ship date")

strSubject = strPartN + ", " + strDesc + ", " + strPO + ", " + strPiec + ", " + strShipDT

End If

End Sub

How I am doing it is I am creating a new module and then putting in this code unfortunately it is not being recognized as a macro so I cannot execute it. does anyone have an idea what I am doing wrong?
 
What exactly do you mean by "it is not being recognized as a macro". What error message are you getting?

This is set as a Private sub so if you are calling it from somehwere outside the module you will get a "Sub or Function not defined" error. Change it to public to avoid that.

 
No error what so ever. When I hit f5 to run it I get a box asking what macro I would like to run and this is not one of the options. I am using outlook 2010. Anything else I create theirs no problem this is to only part I cant get to work.
 
The Private Sub part is keeping the macro from being visible to the macro menu. Use Public instead.
 
Public Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Still not seeing it
 

I am no Outlook expert, but when I go to my Outlook Visual Basic Window, I see Project 1> Microsoft Outlook Objects> This Outlook Session. On that window select Application (instead of General) from the drop-down at the top of the window and place your code there.

You won't run it from a menu, rather, it will automatically trigger when an email is sent (is that what you want done?). You may need to enable macros on your Outlook version.
 
Also, I am assuming you are trying to change the suject line when it is sent. Your code doesn't change it, rather it identifies the new subject line, but never assigns it. If I am correct in understanding what you are trying to do then try this:


Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim strSubject As String, strPartN As String, strShipDT As String, strPO As String, strDesc As String, strPiec As String

    strSubject = Item.Subject
    If strSubject = "KEC" Then
        strPartN = InputBox("Part Number", "Please insert Part number")
        strShipDT = InputBox("Ship Date", "Please insert Ship date")
        strPO = InputBox("P.O", "Please insert P.O number")
        Item.Subject = "PN" + strPartN + "_SD_" + strShipDT + "_PO_" + strPO
    ElseIf strSubject = "GNC" Then
        strPartN = InputBox("Part Number", "Please insert Part number")
        strDesc = InputBox("Part Description", "Please insert Part Description")
        strPO = InputBox("P.O", "Please insert P.O number")
        strPiec = InputBox("Number of Pieces", "Please insert number of Pieces")
        strShipDT = InputBox("Ship Date", "Please insert Ship date")
        Item.Subject = strPartN + ", " + strDesc + ", " + strPO + ", " + strPiec + ", " + strShipDT
     End If

End Sub
 
OK I think I see what I am doing wrong I was trying to execute it as a macro not am application and because of how This works I cannot do that. I only gave you a little piec of my code I have handled it. Thank you very much for the help I will let you know how it goes.
 
OK so far so good. I've got the program running for the most part thanks to FractalWalk pointing out that I was doing it as a macro and I needed to do it as a application.
 
Can anyone recommend a good book on this? I have a feeling I will be doing more of this in the future.
 
OK here is my next hang up
If strSubject = "KECCERT" Then

Prompt$ = "Do you want to send this Cert to Kohler?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, " ") = vbNo Then Cancel = True

strPartN = InputBox("Part Number", "Please insert Part number")

strShipDT = InputBox("Ship Date", "Please insert Ship date")

strPO = InputBox("P.O", "Please insert P.O number")

Set Item = Outlook.Application.ActiveInspector.CurrentItem

Item.Subject = "PN" + strPartN + "_SD_" + strShipDT + "_PO_" + strPO

strSubjectA = strA + strShipDT + strPartN + strRev

End If
At the prompt if I hit cancel it still takes me thought the input boxes and populates the subject line but doesn't send. I would like to end the program at this point.
 
Replace this:
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, " ") = vbNo Then Cancel = True
with this
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, " ") = vbNo Then Exit Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OK I added the new code and it still sends the E-mail. I need it to cancel the E mail all together.
 

Your original line worked for me in that it did not send it.

Code:
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, " ") = vbNo Then Cancel = True

If you are only running off of a single event (sending 1 email) you can just end the rotuine.
Code:
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, " ") = vbNo Then End
 
AFter re-reading, maybe I am not clear on your problem.

You have a yes/no box and if they hit no you want the email not to send AND not to ask for the user input? If that is the case then you just need to put it itnot an If then else format:

Code:
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, " ") = vbNo Then 
     Cancel = True
else
     Other stuff happens. 
Endif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top