This macro to auto-fill all e-mail fields worked in 2003; not in 2

C

Cheryl1325

Can anyone please help; 2007 says REQUIRES OBJECT.
Is there something I cann add to this macro to have it work in Outlook 2007?

Many thanks from a real desperado.


Sub Macro 55()
'
'
Documents.Open FileName:="c:\EmailText.txt", ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto,
XMLTransform:="", _
Encoding:=1252
Selection.WholeStory
Selection.Copy
ActiveDocument.Close
Selection.PasteAndFormat (wdPasteDefault)
Documents.Open FileName:="c:\EmailText.txt", ConfirmConversions:=False,
ReadOnly:= _
False, AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:= _
"", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto, XMLTransform:="", Encoding:=1252
Selection.WholeStory
Selection.Copy
ActiveDocument.Close
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeBackspace
Selection.InsertFile FileName:="c:\EmailText.pdf", Range:="",
ConfirmConversions:= _
False, Link:=False, Attachment:=True
Selection.HomeKey Unit:=wdStory
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=2, Extend:=wdExtend
End Sub
 
S

Sue Mosher [MVP-Outlook]

This is a Word macro, not an Outlook macro. Are you running to run it in Word or Outlook? What is it that you're actually trying to accomplish with this code?
 
C

Cheryl1325

Hello Sue,

You are correct; it IS a Word macro, one that I played inside of Outlook
2003, which obviously saw itself as a Word window on my system.

Basically it opened-copied-closed two text files and pasted their contents
into the e-mail text area. It then inserted the PDF attachment, went up to
the top of the e-mail text box, tried to copy the partial line of text to
place into the Subject field and...stopped (I could not get it to
tab-back-upward to the Subject field as that area is outside the scope of
recognition when recording macros in Word.

An imperfect but workable solution for a non-programmer.

Now if I could get hold of a book with a variety of finished code, I'd love
to begin my VBA education. Until then where can I see lots on-screen to do
something like this?:

Set sender account;
Set filename attachment;
Copy files's text content to Subject field;
Copy files's text content to e-mail text box;

Then, all I need to do is place the recipient address and press send.

I might even get to eat lunch daily.

Many thanks.
 
C

Cheryl1325

Hello Sue,

You are correct; it IS a Word macro, one that I played inside of Outlook
2003, which obviously saw itself as a Word window on my system.

Basically it opened-copied-closed two text files and pasted their contents
into the e-mail text area. It then inserted the PDF attachment, went up to
the top of the e-mail text box, tried to copy the partial line of text to
place into the Subject field and...stopped (I could not get it to
tab-back-upward to the Subject field as that area is outside the scope of
recognition when recording macros in Word.

An imperfect but workable solution for a non-programmer.

Now if I could get hold of a book with a variety of finished code, I'd love
to begin my VBA education. Until then where can I see lots on-screen to do
something like this?:

Set sender account;
Set filename attachment;
Copy files's text content to Subject field;
Copy files's text content to e-mail text box;

Then, all I need to do is place the recipient address and press send.

I might even get to eat lunch daily.

Many thanks.
 
S

Sue Mosher [MVP-Outlook]

That sounds like a great project! Your first stop should be the object browser -- F2 in VBA. Switch from said:
Copy files's text content to e-mail text box;

The easiest approach would probably be to use Word methods to open the file. To start, use Tools | References to add a reference to the Microsoft Word library. Your code will need to instantiate a Word Application object, then use its Documents.Open method, which you already know, to open and return the text file. Finally, use the technique shown at http://www.outlookcode.com/codedetail.aspx?id=1333 to send the file as an Outlook message. (That sample shows how to instantiate a Word Application object, BTW.) Notice that the itm object in the sample code is an Outlook MailItem. That brings us to the other tasks on your list:
Copy files's text content to Subject field;

Use the MailItem.Subject property.
Set sender account;

Use the MailItem.SendUsingAccount property.
Set filename attachment;

Use the MailItem.Attachments.Add method.

That's just an outline. Take it a step at a time and let us know if you get stumped.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
C

Cheryl1325

Hello Sue,

Here's my code, so far, which I can appreciate does not not work (yet).
Though I may have taken a leap towards familiarity of VB code, I cannot yet
appreciate knowing the simple meaning of some of the lines that now follow:

Sub Sue()
' In honor of you
Dim myOlApp As New Outlook.Application
Dim myItem As Outlook.MailItem
Dim oAccount As Outlook.Account
Set myItem = myOlApp.CreateItem(olMailItem)
oMail.SendUsingAccount = "(e-mail address removed)"
Dim myAttachments As Outlook.Attachments
myAttachments.Add "C:\attachment.pdf"
myItem.Subject = "Per your request earlier, here is the form in PDF"
myItem.Display
Documents.Open FileName:="c:\FileWithText.doc"
Selection.WholeStory
Selection.Copy
ActiveDocument.Close
Selection.PasteAndFormat (wdPasteDefault)
End Sub

"Sub or Function not defined" is the (first) error coming up.

I'd also like to end with the cursor in the To: / Recipient address field,
so I can enter it and click send, or even better, type Alt-S. Would the
following line of code LEAVE me there?

Set myRecipient = myItem.Recipients.Add ("xxx")

Many thanks.
 
S

Sue Mosher [MVP-Outlook]

I'm a little disappointed that you didn't use the technique I suggested -- opening a document and using it as the body of an email message. Copy and paste isn't as simple. Was there something about that method that you didn't understand? I'd suggest that you just try running the code sample as is, without trying to modify it yet, so that you can see what it actually does. We can come back to it once it makes sense to you.

In the meantime, let's clean up some of the other issues in your code, starting with this statement:

Dim myOlApp As New Outlook.Application

That statement should never appear in Outlook VBA code. Instead, use the intrinsic Application object, e.g.:

Dim myOlApp As Outlook.Application
Set myOlApp = Application

Or simply replace myOlApp with Application everywhere it appears in your code.

I see a tendency in your code to lose track of what variable you're using, e.g.:

Dim myItem As Outlook.MailItem
' <snip>
Set myItem = myOlApp.CreateItem(olMailItem)
oMail.SendUsingAccount = "(e-mail address removed)"

You have both myItem and oMail! A good way to help avoid this issue is to add this statement to the very beginning of your code module:

Option Explicit

That will force you to declare every variable. In the case above, it would have warned you that oMail was undeclared, so you might have more easily seen that you already had a MailItem (myItem).

Another issue related to variables is that you need to instantiate them. You can't just declare them. For example, look at these two statements:

Dim myAttachments As Outlook.Attachments
myAttachments.Add "C:\attachment.pdf"

The first statement tells Outlook that you want to use an Attachments variable. But there is no statement that tells Outlook where that Attachments variable is supposed to come from. Is it related to the message you just created? Or to some other message? You need an additional statement that makes that explicit instantiation:

Dim myAttachments As Outlook.Attachments
Set myAttachments = myItem.Attachments
myAttachments.Add "C:\attachment.pdf"

For an object variable, you always use a Set statement to instantiate it. An object has properties and methods. It's not a simple value like a string or number. And speaking of objects and strings ...

Regarding SendUsingAccount, an email address is a string, not an Account object. You declare an oAccount object, but your code never actually instantiates it. TIP: It's always a good idea to look up properties and methods that are new to you. Do this with the object browser. In the VBA environment in Outlook, press F2. Switch from <All Libraries> to Outlook to browse all Outlook objects and their properties, methods, and events. Select any object or member, then press F1 to see its Help topic. If you looked up SendUsingAccount, you'd see this sample code for SendUsingAccount:

Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("(e-mail address removed)")
oMail.Recipients.ResolveAll
oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub

This shows that you have to get the account from the Application.Session.Accounts collection. The sample does it by iterating the collection. Another way is to use the name of the account, so you could have:

Set oAccount = Application.Session.Accounts("Name of your account")

The name may or may not be the same as the the email address. Use the name that appears in the accounts list.

That's enough for now. Let us know how you get along.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
M

Michael Bauer [MVP - Outlook]

Sue, if you like to see what's already been discussed on this:

Topic: Macro for mailing same PDF from same account regularly...
in this group

http://groups.google.de/group/micro...iling+same+pdf"&rnum=1&hl=de#a8e7c17aa6514f06

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Organize Outlook email:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>


Am Wed, 20 Jun 2007 09:43:50 -0400 schrieb Sue Mosher [MVP-Outlook]:
I'm a little disappointed that you didn't use the technique I suggested --
opening a document and using it as the body of an email message. Copy and
paste isn't as simple. Was there something about that method that you didn't
understand? I'd suggest that you just try running the code sample as is,
without trying to modify it yet, so that you can see what it actually does.
We can come back to it once it makes sense to you.
In the meantime, let's clean up some of the other issues in your code, starting with this statement:

Dim myOlApp As New Outlook.Application

That statement should never appear in Outlook VBA code. Instead, use the
intrinsic Application object, e.g.:
Dim myOlApp As Outlook.Application
Set myOlApp = Application

Or simply replace myOlApp with Application everywhere it appears in your code.

I see a tendency in your code to lose track of what variable you're using, e.g.:

Dim myItem As Outlook.MailItem
' <snip>
Set myItem = myOlApp.CreateItem(olMailItem)
oMail.SendUsingAccount = "(e-mail address removed)"

You have both myItem and oMail! A good way to help avoid this issue is to
add this statement to the very beginning of your code module:
Option Explicit

That will force you to declare every variable. In the case above, it would
have warned you that oMail was undeclared, so you might have more easily
seen that you already had a MailItem (myItem).
Another issue related to variables is that you need to instantiate them.
You can't just declare them. For example, look at these two statements:
Dim myAttachments As Outlook.Attachments
myAttachments.Add "C:\attachment.pdf"

The first statement tells Outlook that you want to use an Attachments
variable. But there is no statement that tells Outlook where that
Attachments variable is supposed to come from. Is it related to the message
you just created? Or to some other message? You need an additional statement
that makes that explicit instantiation:
Dim myAttachments As Outlook.Attachments
Set myAttachments = myItem.Attachments
myAttachments.Add "C:\attachment.pdf"

For an object variable, you always use a Set statement to instantiate it.
An object has properties and methods. It's not a simple value like a string
or number. And speaking of objects and strings ...
Regarding SendUsingAccount, an email address is a string, not an Account
object. You declare an oAccount object, but your code never actually
instantiates it. TIP: It's always a good idea to look up properties and
methods that are new to you. Do this with the object browser. In the VBA
environment in Outlook, press F2. Switch from <All Libraries> to Outlook to
browse all Outlook objects and their properties, methods, and events. Select
any object or member, then press F1 to see its Help topic. If you looked up
SendUsingAccount, you'd see this sample code for SendUsingAccount:
Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("(e-mail address removed)")
oMail.Recipients.ResolveAll
oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub

This shows that you have to get the account from the
Application.Session.Accounts collection. The sample does it by iterating the
collection. Another way is to use the name of the account, so you could
have:
Set oAccount = Application.Session.Accounts("Name of your account")

The name may or may not be the same as the the email address. Use the name
that appears in the accounts list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top