Consulting

Results 1 to 10 of 10

Thread: Solved: Copy first line and paste into subject Outlook

  1. #1
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location

    Solved: Copy first line and paste into subject Outlook

    Hi Everyone,


    I have a Word Document that I email daily, is there a code that it will copy the first line of the document and paste it in the Subject: box ?

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yes. The first line can simply be captured as a string. Then use that string as the subject line in the Outlook code.

  3. #3
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Do you have a sample code ?

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Check the KnowledgeBase - that is what it is there for. There are a number of listungs that cover sending mail.

    As for getting the string of the first line...do you know how to declare variables, etc.?

  5. #5
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Not really I'm just a youngster in VBA I've been doing this a year already. If I see a code I try my best to modify it or put then together to get it to work. It takes me a long time though.

    I put this code in the Subject for the Outlook code:

    [VBA] LinesToPoints(1)[/VBA]

    When I sent it out the Subject box says 12

    Any Ideas ?

  6. #6
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Well LinesToPoints isn't going to help, is it?
    You'll need sub or function that gets the first line text of the document and assigns it to a variable. Then in your code for sending the mail (which I assume you have already), assign the varaible to the mailobject's Subject property
    Here's something that does that and displays the result, so you get the idea[VBA]Sub GetFirstLine()

    'set a constant for the maximum number of characters you want to display
    'I'm not sure, but I think the Subject field in Outlook
    'will only show 256 chars anyway
    Const MAX_LENGTH As Integer = 72
    Dim strFirstLine As String

    'assign the text of the first sentence to a variable
    strFirstLine = ActiveDocument.Sentences(1).Text

    'if the first line is too long, truncate it
    If Len(strFirstLine) > MAX_LENGTH Then
    strFirstLine = Left(strFirstLine, MAX_LENGTH - 3) & "..."
    End If

    'display the value of the variable
    MsgBox strFirstLine

    End Sub[/VBA]
    K :-)

  7. #7
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Thank You so much Killian,
    The code you provided is excellent and I got it to work with the code I have.



    [VBA]
    Sub MyOutlook()
    Dim AppOutlook As Object
    Set AppOutlook = CreateObject("Outlook.application")
    With AppOutlook.CreateItem(olMailItem)
    .To = "Anyone@home.com"
    .Subject = ActiveDocument.Sentences(1).Text
    .Send
    End With
    Set AppOutlook = Nothing
    End Sub
    [/VBA]

  8. #8
    Referring regularly to this forum has increased my knowledge of VBA, especially in my use of Word. I combine Word with Dragon NaturallySpeaking Legal Solutions to automate much of my work. I want to thank all the contributors here for the useful information they post.

    Since I took the code from this thread Killian posted and modified it to apply to another context, saving letters that I regularly dictate, I thought it might be useful to post the code with my modifications here. Each letter has a standard first line, a file number of fixed length, and is saved to the same network directory. This task cried out for automation, and I recognized immediately from this thread that I could do it (even if I didn't quite "immediately" figure out how to do it):

    [VBA]Sub SaveLetter()

    'set a constant for the maximum number of characters you want to display

    Const MAX_LENGTH As Integer = 9

    'declare variables
    Dim strFirstLine As String
    Dim ThisDoc As Document
    Dim myDocName As String

    Set ThisDoc = ActiveDocument

    'assign the text of the first sentence to a variable
    strFirstLine = ThisDoc.Sentences(1).Text

    'since my first line contains a paragraph mark that
    'will screw up the save, truncate it to eliminate the
    'paragraph mark
    If Len(strFirstLine) > MAX_LENGTH Then
    strFirstLine = Left(strFirstLine, MAX_LENGTH)
    End If

    'save document as letter to my work directory--some
    'of my dictation may use the same file number but not
    'be a letter

    myDocName = strFirstLine & " Letter"
    Application.ChangeFileOpenDirectory "G:\LEW work"
    ThisDoc.SaveAs FileName:="G:\LEW work\" & myDocName

    'exit document

    ThisDoc.Close

    'release memory assigned to this procedure

    Set ThisDoc = Nothing

    End Sub[/VBA]

    Having hooked this macro to a NaturallySpeaking macro so that I can call it in dictation, I figure that this code saves me at least five steps every time I write a letter: clicking save, changing the file directory, entering the file name, clicking the save button in the dialog box, and exiting the document. Plus, it's just fun to know, "I did that."

    Thanks again.

  9. #9
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    @lwildernorva
    Hi and welcome to VBAX

    Well spotted and thanks for posting - it's nice to have plenty of "real world" examples on the board.
    K :-)

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Shazam, you may want to add Option Explicit to your code so that variables must be explicitly declared. It may well save you grief later on when you get into more advanced coding.

Posting Permissions

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