PDA

View Full Version : Solved: Copy first line and paste into subject Outlook



Shazam
10-18-2005, 03:43 PM
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 ?

fumei
10-18-2005, 05:17 PM
Yes. The first line can simply be captured as a string. Then use that string as the subject line in the Outlook code.

Shazam
10-18-2005, 06:13 PM
Do you have a sample code ?

fumei
10-18-2005, 09:03 PM
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.?

Shazam
10-19-2005, 05:05 AM
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:

LinesToPoints(1)

When I sent it out the Subject box says 12

Any Ideas ?

Killian
10-19-2005, 06:01 AM
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 ideaSub 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

Shazam
10-19-2005, 06:12 AM
Thank You so much Killian,
The code you provided is excellent and I got it to work with the code I have.




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

lwildernorva
10-19-2005, 08:26 AM
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):

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

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.

Killian
10-19-2005, 08:36 AM
@lwildernorva
Hi and welcome to VBAX :hi:

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

fumei
10-19-2005, 09:42 PM
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.