Consulting

Results 1 to 9 of 9

Thread: Solved: Auto creating Subject headers

  1. #1

    Question Solved: Auto creating Subject headers

    Hello

    I have created a form which allows users to create subject headers for emails in the format that my company wants them in. Using text boxes, check boxes, option boxes etc I have finally arrived at a preview textbox with the text in that I want people to put in their subject lines. How do I get the text in that text box into the subject line of the email? Here is the relevant line of my code as it stands (there is a lot more of "if blah = true then blah" conditions before this line, but they are irelevat to the question):

    Sub CommandButton2_Click()

    TextBox3.value = internetauthorised & Textbox1.Value & "-" & protectivemarker & "-" & caveat & TextBox2.value & "-" & ListBox1.value

    end sub

    How do I get the subject line of my Outlook New Message to be the same as this textbox value?

    Sam

  2. #2
    Ideally, all you have to do is set the Subject property of the MailItem to the value of your textbox. However, obtaining the reference to that MailItem can be the tricky part depending on your userform's relationship with the New Message form. The simple way is to set a button on your userform to close the form and create the new MailItem once the subject line is compiled. The code would look something like this:

    [VBA]
    Sub CommandButton3_Click()
    Dim objNewMail as MailItem
    Set objNewMail = Application.CreateItem(olMailItem)
    objNewMail.Subject = TextBox3
    objNewMail.Display
    Me.Unload ' Disclaimer: I'm not familiar with userforms much,
    ' so I'm not 100% sure that works properly.
    End Sub
    [/VBA]

    Would this work, or were you planning a different way of loading the userform and/or New Message form?

  3. #3
    I would open a new message in Outlook, as per normal, but I put a new button on the new message toolbar which was linked to a macro and hence pull up the UserForm with all the questions on them. Once all the options and fields had been populated, I clicked a button on the userform to preview the information in the field and then I wanted it to go to the subject field of the message (and the userform dissappear-which I can do). I will give this a go...thank you very much for your reply...unfortunately all my Outlook VBA help files on my network are in French, which of course I do not know!

  4. #4
    Here is what Im trying to do. So far I have put a button on all new messages I open in Outlook 2000 which links to a macro which opens up a user form with all the questions/options/fields for the user to fill in. Upon clicking "ok" on this user form I currently get a textbox to preview the result (i.e. yyyymmdd-<protective marker>-<caveat>-<subject>-<author>). I want to get this text box value to then transfer to the subject line of the new message, which is already open. This (below) doesnt want to play.

    [VBA]Private Sub CommandButton2_Click() ? This is the "ok" button which currently compiles all the options/fields into a single string.
    Dim objNewMail as MailItem

    Set objNewMail = Application.CreateItem(olMailItem)
    ?~~~~~~All the Option button if?then questions that form the parts of the final string~~~~~~~

    Textbox3.value = ?~~~~~~~~subject string ~~~~~~

    objNewMail.Subject = Textbox3.value

    [/VBA]

    Thanks for the tip chocobochick, but I don't think I gave you the whole picture, sorry

  5. #5
    Okay. In that case, it gets a touch more complicated. Since the button you click to load the userform will always be from the active window, we can call the ActiveInspector of the Application object to give us our MailItem. The problem, however, is that you won't be referencing the object until a later procedure, so we'll have to store it in a module-level variable during the userform's initialize event in case the ActiveInspector changes later on.

    [VBA]
    ' Reference to MailItem
    Private objNewMail As MailItem

    ' Store reference to MailItem
    Private Sub UserForm_Initialize()
    Set objNewMail = Application.ActiveInspector.CurrentItem
    End Sub

    ' Compile user data and display new message subject
    Private Sub CommandButton2_Click()
    ' Insert subject compilation code here
    objNewMail.Subject = Textbox3
    End Sub
    [/VBA]

    I believe that should work.

  6. #6
    That works really well, thank you! Although it only does it once: I open Outlook, I open a new message, and run the code and it works. Send the message etc. Then when I try to send another new message, it doesn't seem to want to repopulate the new subject field. Is that because it is still running from the first time it was used? How could I fix that?

  7. #7
    It sounds like the form isn't unloading properly after each use. If the form still exists in memory, then the Initialize event won't occur the next time you try to load it.

    How are you calling the userform from the toolbar, and how are you closing it? When I tested this, I pointed my toolbar shortcut to a macro consisting of only the "Load UserForm1" and "UserForm1.Show" lines. On the UserForm itself, I assigned a close button with the code "Unload Me" (it turns out "Me.Unload" was improper syntax). When I switched this code to Me.Hide, I was able to duplicate your results.

    You can also use the terminate event to test if your form is properly unloading. Just add the following to your userform's code, and a message box should appear when you close the form.

    [VBA]
    Private Sub UserForm_Terminate()
    MsgBox "We have termination."
    End Sub
    [/VBA]

  8. #8
    Aha! I think you are onto something there! I have always just hidden my forms to get rid of them. I will try the unload action...never heard of that before, but then again, I am only a beginner! Thanks Chocobochick.

  9. #9
    yes it worked, thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •