Write macro to attach PDF files in a folder as attachments to outlook email message. How?

  • Thread starter Thread starter tabonni
  • Start date Start date
T

tabonni

Hi All

I haven't touched Outlook/Word VBA before. So, I'm a newbie.

But, I need to write Outlook/Word Macro to do some works. I say
Outlook/Word Macro because I don't know I should write Outlook or Word
Macro if I want to attach files to Outlook email message using Word as
editor.

My situation is:

I download a number of PDF files from the Internet into a folder
called Temp. I want to write a Outlook/Word Macro to attach all the
PDF files in Temp folder to email message as attachments. Just like
press a button and all files will be attached in the email message.
After all PDF files are attached and sent, all PDF files in Temp
folder will be deleted. Is it possible?

And, can anyone show me some coding on implementing this?

Thank you very much
 
No special considerations for handling WordMail is required. This macro
should do what you want - just change the folder path, make sure the e-mail
is open and set a reference in the Outlook VBA Project to the Microsoft
Scripting Runtime library.

Sub AttachAllPDFFilesFromFolderToCurrentMessageAndDeleteWhenFinished()
On Error Resume Next

Dim objFS As New Scripting.FileSystemObject, objFolder As
Scripting.Folder, objFile As Scripting.File
Dim objMessage As Object, intX As Integer
Dim strFolderPath As String

If ActiveInspector Is Nothing Then Exit Sub

Set objMessage = ActiveInspector.CurrentItem
strFolderPath = "C:\Temp"

Set objFolder = objFS.GetFolder(strFolderPath)

For Each objFile In objFolder.Files
If Right(objFile.Name, 3) = "pdf" Then
objMessage.Attachments.Add (objFile.Path)
objFile.Delete (True)
End If
Next

MsgBox "Your PDF files have been attached, and the sources deleted.",
vbOKOnly + vbInformation, "Operation Complete"

Set objFS = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Set objMessage = Nothing
End Sub
 
Hello Eric

When I click Tools -> Macro ->
AttachAllPDFFilesFromFolderToCurrentMessageAndDeleteWhenFinished in
the email message body under Word editor, there is nothing happen. No
files attached. I already create a Temp folder under C: drive and put
a PDF file in the folder. Did I do something wrong?

What I did is:
I open my MS Outlook and reply a message. A email message with Word
editor pop up. Then, I click Tools (on the toolbar)-> Macro->Visual
Basic Editor.

Under VB Editor, I click Tools->References-> Check Microsoft Scripting
Runtime libary checkbox and keep the other default options. Then, I
insert a module and put the coding in the Sub AttachAll...When
Finished() End Sub. Am I doing the right thing?

Anything I haven't done that make it doesn't work?

Thank you
 
Check your macro security settings from the Tools | Macro| Security menu.
You may have to restart Outlook for any changes to take effect. Then try
running the macro again, making sure that you have a new message window open.

Also try setting a breakpoint on the first line of the procedure to verify
that it is firing at all.

You may also want to review the basics of Outlook VBA. See this link for
some great info:

Visual Basic and VBA Coding in Microsoft Outlook:
http://www.outlookcode.com/d/vb.htm

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/
 
Hi Eric

I check the macro security setting to Medium. I also restart my MS
Outlook. The macro doesn't work still.

I also try to set a breakpoint. I found the macro stop/exit the
subroutine at this statement "If ActionInspector Is Nothing Then Exit
Sub".

When I make the statement "If ActionInspector Is Nothing Then Exit
Sub" as comment, the macro is run. The file in C:\Temp deleted and the
MsgBox is popped up. HOWEVER, it can't attach attachments. I mean no
attachments attach in the email message body.

Is it because I am using MS Word as editor for Outlook message that
make it can't attach attachments? If so, which part of code I need to
change? Because I need to use MS Word as editor for Outlook message.

Thanks
 
If the code is exiting at the "If ActiveInspector Is Nothing Then Exit Sub"
line, then this is because you do not have a new e-mail message open. Using
Word as your editor has no effect on the code. Remove the "On Error Resume
Next" line to see exactly where the error is occuring. My guess is because
the objMessage variable evaluates to Nothing, which means you don't have a
message open.
 
Hi Eric

I want to distribute the outlook macro inside my department. I also
want to add a button on the toolbar. I'm using Visual Basic 6.0 COM
Add-in Designer to do this.
I have already make the references such as MS Office 10.0 Object
Library, MS Add-In Designer, MS Scripting Runtime, MS Outlook 10.0
Object Library and other default refernces.
I can create the add-in DLL. I also check the registry the Add-in DLL
is there. However, the command button on the toolbar does not show.

Please tell me what do you observe.

The following is my code:


Option Explicit

Dim objFS As New Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim objFile As Scripting.File

Dim objMessage As Object
Dim intX As Integer
Dim strFolderPath As String

Dim objWord As Object
Dim WithEvents myButton As Office.CommandBarButton

Private Sub AddinInstance_OnConnection(ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)

MsgBox "COM Add-in started in " & Application.Name

Set objWord = Application

Set myButton = objWord.CommandBars("Standard").Controls.Add(1)
With myButton
..Caption = "Attach Files"
..Style = msoButtonCaption
..Tag = "Attach Files"
..OnAction = "!<" & AddInInst.ProgId & ">"
..Visible = True
End With
End Sub

Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As _
AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)

Set objFS = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Set objMessage = Nothing
Set objWord = Nothing
Set myButton = Nothing
End Sub

Private Sub MyButton_Click(ByVal Ctrl As Office.CommandBarButton, _
CancelDefault As Boolean)

If ActiveInspector Is Nothing Then Exit Sub

Set objMessage = ActiveInspector.CurrentItem

strFolderPath = "C:\Temp"

Set objFolder = objFS.GetFolder(strFolderPath)

For Each objFile In objFolder.Files
objMessage.Attachments.Add (objFile.Path)
objFile.Delete (True)
Next

MsgBox "Your PDF Files have been attached, and the sources deleted.",
vbOKOnly + vbInformation, "Operation Complete"
End Sub



Thank you.
 
The previous problem is solved. After I restart my computer, the
button appears on the standard tool bar. Thanks

But, I got another question.

After I create the COM Add-In, is it I copy the DLL file and paste it
in the other machine as well as set the registry? If so, where should
I put the DLL file? And, how to set the registry?

Thanks
 
It doesn't matter where you copy the .dll, as long as you register it with
this from the command line:

regsvr32 <path to .dll>

However, you need registry keys like these to have it load with Outlook:

[HKEY_CURRENT_USER\Software\Microsoft\Office\Outlook\Addins\<ADD-IN
NAME>.Connect]
"FriendlyName"="<FRIENDLY ADD-IN NAME>."
"Description"="<DESCRIPTION>"
"LoadBehavior"=dword:00000003
"CommandLineSafe"=dword:00000000

Your code looks fine, although you don't need to set the OnAction property
if you declare the button WithEvents. However, you should check to see if
the button exists first; as it stands, you might be creating duplicate
buttons whenever the add-in loads. Lastly, for strict code readability, you
should rename your objWord variable, because it is storing a reference to an
Outlook object.

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/
 
Hi Eric

Yes. The button is duplicated. When I open the outlook and
create/reply a message everytime, the "Attach Files To Email" button
is created again.

How can I check to see the "Attach Files To Email" button is exist? If
it is exist, it won't create the button on the toolbar again.

Beside this, I want to put the "Attach Files To Email" Button into a
new Toolbar. How can I do that as well?

Set myButton = objOutlook.CommandBars("Standard").Controls.Add(1)

The above statement just create the button on the standard toolbar.
What I should change?

Cheers
Eric Legault said:
It doesn't matter where you copy the .dll, as long as you register it with
this from the command line:

regsvr32 <path to .dll>

However, you need registry keys like these to have it load with Outlook:

[HKEY_CURRENT_USER\Software\Microsoft\Office\Outlook\Addins\<ADD-IN
NAME>.Connect]
"FriendlyName"="<FRIENDLY ADD-IN NAME>."
"Description"="<DESCRIPTION>"
"LoadBehavior"=dword:00000003
"CommandLineSafe"=dword:00000000

Your code looks fine, although you don't need to set the OnAction property
if you declare the button WithEvents. However, you should check to see if
the button exists first; as it stands, you might be creating duplicate
buttons whenever the add-in loads. Lastly, for strict code readability, you
should rename your objWord variable, because it is storing a reference to an
Outlook object.

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/


tabonni said:
The previous problem is solved. After I restart my computer, the
button appears on the standard tool bar. Thanks

But, I got another question.

After I create the COM Add-In, is it I copy the DLL file and paste it
in the other machine as well as set the registry? If so, where should
I put the DLL file? And, how to set the registry?

Thanks
 
You can check to see if a button exists by calling its name from the controls
collection of the CommandBar where you expect it to exist:

Set objButton = objCommandBar.Controls("My Button Caption")

If objButton Is Nothing Then
'Create the button
End If

To add a new command bar, do this:

Set objCustomToolBar = ActiveExplorer.CommandBars.Add("My ToolBar")

Make sure that you declare your objToolBar publicly in a global module so
that it can later be deleted. It is best practice to delete any custom
toolbars when the Add-In is disconnected, as uninstalling your Add-In won't
delete any custom toolbars that were created. Delete the toolbar in the
OnDisconnection event:

Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode _
As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
On Error Resume Next

'Tear down the class
'IMPORTANT: This event will not fire when
'RemoveMode = ext_dm_HostShutdown
'It will fire when RemoveMode = ext_dm_UserClosed

If RemoveMode = ext_dm_UserClosed Then
'User shutdown removed COM Add-in
'Cleanup custom toolbars by deleting them
If Not objCustomToolBar Is Nothing Then objCustomToolBar.Delete
Else
'Host shutdown
End If
'DebugWrite "AddinInstance_OnDisconnection"
End Sub

The above code is taken from the template used to create a COM Add-In in
Visual Basic, which you can get here if you aren't already using it:

Outlook COM Add-in Template:
http://www.microeye.com/resources/template.htm

That site also has a great sample project that illustrates how to work with
custom toolbars:

Items Command Bar:
http://www.microeye.com/resources/itemsCB.htm

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/

tabonni said:
Hi Eric

Yes. The button is duplicated. When I open the outlook and
create/reply a message everytime, the "Attach Files To Email" button
is created again.

How can I check to see the "Attach Files To Email" button is exist? If
it is exist, it won't create the button on the toolbar again.

Beside this, I want to put the "Attach Files To Email" Button into a
new Toolbar. How can I do that as well?

Set myButton = objOutlook.CommandBars("Standard").Controls.Add(1)

The above statement just create the button on the standard toolbar.
What I should change?

Cheers
Eric Legault said:
It doesn't matter where you copy the .dll, as long as you register it with
this from the command line:

regsvr32 <path to .dll>

However, you need registry keys like these to have it load with Outlook:

[HKEY_CURRENT_USER\Software\Microsoft\Office\Outlook\Addins\<ADD-IN
NAME>.Connect]
"FriendlyName"="<FRIENDLY ADD-IN NAME>."
"Description"="<DESCRIPTION>"
"LoadBehavior"=dword:00000003
"CommandLineSafe"=dword:00000000

Your code looks fine, although you don't need to set the OnAction property
if you declare the button WithEvents. However, you should check to see if
the button exists first; as it stands, you might be creating duplicate
buttons whenever the add-in loads. Lastly, for strict code readability, you
should rename your objWord variable, because it is storing a reference to an
Outlook object.

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/


tabonni said:
The previous problem is solved. After I restart my computer, the
button appears on the standard tool bar. Thanks

But, I got another question.

After I create the COM Add-In, is it I copy the DLL file and paste it
in the other machine as well as set the registry? If so, where should
I put the DLL file? And, how to set the registry?

Thanks

OutlookCode.com is the best; otherwise, see MSDN:

Office Developer Center: Outlook:
http://msdn.microsoft.com/office/understanding/outlook/default.aspx

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/


:

Hi Eric

The macro is working now. Thank you so much.

By the way, do you know any websites are good for learning VBA.

I remember you recommended http://www.outlookcode.com/d/vb.htm last
time. Anymore sites?

Thanks again.

If the code is exiting at the "If ActiveInspector Is Nothing Then Exit Sub"
line, then this is because you do not have a new e-mail message open. Using
Word as your editor has no effect on the code. Remove the "On Error Resume
Next" line to see exactly where the error is occuring. My guess is because
the objMessage variable evaluates to Nothing, which means you don't have a
message open.

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/

:

Hi Eric

I check the macro security setting to Medium. I also restart my MS
Outlook. The macro doesn't work still.

I also try to set a breakpoint. I found the macro stop/exit the
subroutine at this statement "If ActionInspector Is Nothing Then Exit
Sub".

When I make the statement "If ActionInspector Is Nothing Then Exit
Sub" as comment, the macro is run. The file in C:\Temp deleted and the
MsgBox is popped up. HOWEVER, it can't attach attachments. I mean no
attachments attach in the email message body.

Is it because I am using MS Word as editor for Outlook message that
make it can't attach attachments? If so, which part of code I need to
change? Because I need to use MS Word as editor for Outlook message.

Thanks

Check your macro security settings from the Tools | Macro| Security menu.
You may have to restart Outlook for any changes to take effect. Then try
running the macro again, making sure that you have a new message window open.

Also try setting a breakpoint on the first line of the procedure to verify
that it is firing at all.

You may also want to review the basics of Outlook VBA. See this link for
some great info:

Visual Basic and VBA Coding in Microsoft Outlook:
http://www.outlookcode.com/d/vb.htm

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/


:

Hello Eric

When I click Tools -> Macro ->
AttachAllPDFFilesFromFolderToCurrentMessageAndDeleteWhenFinished in
the email message body under Word editor, there is nothing happen. No
files attached. I already create a Temp folder under C: drive and put
a PDF file in the folder. Did I do something wrong?

What I did is:
I open my MS Outlook and reply a message. A email message with Word
editor pop up. Then, I click Tools (on the toolbar)-> Macro->Visual
Basic Editor.

Under VB Editor, I click Tools->References-> Check Microsoft Scripting
Runtime libary checkbox and keep the other default options. Then, I
insert a module and put the coding in the Sub AttachAll...When
Finished() End Sub. Am I doing the right thing?

Anything I haven't done that make it doesn't work?

Thank you

No special considerations for handling WordMail is required. This macro
should do what you want - just change the folder path, make sure the e-mail
is open and set a reference in the Outlook VBA Project to the Microsoft
Scripting Runtime library.

Sub AttachAllPDFFilesFromFolderToCurrentMessageAndDeleteWhenFinished()
On Error Resume Next

Dim objFS As New Scripting.FileSystemObject, objFolder As
Scripting.Folder, objFile As Scripting.File
Dim objMessage As Object, intX As Integer
Dim strFolderPath As String

If ActiveInspector Is Nothing Then Exit Sub

Set objMessage = ActiveInspector.CurrentItem
strFolderPath = "C:\Temp"

Set objFolder = objFS.GetFolder(strFolderPath)

For Each objFile In objFolder.Files
If Right(objFile.Name, 3) = "pdf" Then
objMessage.Attachments.Add (objFile.Path)
objFile.Delete (True)
End If
Next

MsgBox "Your PDF files have been attached, and the sources deleted.",
vbOKOnly + vbInformation, "Operation Complete"

Set objFS = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Set objMessage = Nothing
End Sub

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/

:

Hi All

I haven't touched Outlook/Word VBA before. So, I'm a newbie.

But, I need to write Outlook/Word Macro to do some works. I say
Outlook/Word Macro because I don't know I should write Outlook or Word
Macro if I want to attach files to Outlook email message using Word as
editor.

My situation is:

I download a number of PDF files from the Internet into a folder
called Temp. I want to write a Outlook/Word Macro to attach all the
PDF files in Temp folder to email message as attachments. Just like
press a button and all files will be attached in the email message.
After all PDF files are attached and sent, all PDF files in Temp
folder will be deleted. Is it possible?

And, can anyone show me some coding on implementing this?

Thank you very much
 
Thank you very much

Eric Legault said:
You can check to see if a button exists by calling its name from the controls
collection of the CommandBar where you expect it to exist:

Set objButton = objCommandBar.Controls("My Button Caption")

If objButton Is Nothing Then
'Create the button
End If

To add a new command bar, do this:

Set objCustomToolBar = ActiveExplorer.CommandBars.Add("My ToolBar")

Make sure that you declare your objToolBar publicly in a global module so
that it can later be deleted. It is best practice to delete any custom
toolbars when the Add-In is disconnected, as uninstalling your Add-In won't
delete any custom toolbars that were created. Delete the toolbar in the
OnDisconnection event:

Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode _
As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
On Error Resume Next

'Tear down the class
'IMPORTANT: This event will not fire when
'RemoveMode = ext_dm_HostShutdown
'It will fire when RemoveMode = ext_dm_UserClosed

If RemoveMode = ext_dm_UserClosed Then
'User shutdown removed COM Add-in
'Cleanup custom toolbars by deleting them
If Not objCustomToolBar Is Nothing Then objCustomToolBar.Delete
Else
'Host shutdown
End If
'DebugWrite "AddinInstance_OnDisconnection"
End Sub

The above code is taken from the template used to create a COM Add-In in
Visual Basic, which you can get here if you aren't already using it:

Outlook COM Add-in Template:
http://www.microeye.com/resources/template.htm

That site also has a great sample project that illustrates how to work with
custom toolbars:

Items Command Bar:
http://www.microeye.com/resources/itemsCB.htm

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/

tabonni said:
Hi Eric

Yes. The button is duplicated. When I open the outlook and
create/reply a message everytime, the "Attach Files To Email" button
is created again.

How can I check to see the "Attach Files To Email" button is exist? If
it is exist, it won't create the button on the toolbar again.

Beside this, I want to put the "Attach Files To Email" Button into a
new Toolbar. How can I do that as well?

Set myButton = objOutlook.CommandBars("Standard").Controls.Add(1)

The above statement just create the button on the standard toolbar.
What I should change?

Cheers
Eric Legault said:
It doesn't matter where you copy the .dll, as long as you register it with
this from the command line:

regsvr32 <path to .dll>

However, you need registry keys like these to have it load with Outlook:

[HKEY_CURRENT_USER\Software\Microsoft\Office\Outlook\Addins\<ADD-IN
NAME>.Connect]
"FriendlyName"="<FRIENDLY ADD-IN NAME>."
"Description"="<DESCRIPTION>"
"LoadBehavior"=dword:00000003
"CommandLineSafe"=dword:00000000

Your code looks fine, although you don't need to set the OnAction property
if you declare the button WithEvents. However, you should check to see if
the button exists first; as it stands, you might be creating duplicate
buttons whenever the add-in loads. Lastly, for strict code readability, you
should rename your objWord variable, because it is storing a reference to an
Outlook object.

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/


:

The previous problem is solved. After I restart my computer, the
button appears on the standard tool bar. Thanks

But, I got another question.

After I create the COM Add-In, is it I copy the DLL file and paste it
in the other machine as well as set the registry? If so, where should
I put the DLL file? And, how to set the registry?

Thanks

OutlookCode.com is the best; otherwise, see MSDN:

Office Developer Center: Outlook:
http://msdn.microsoft.com/office/understanding/outlook/default.aspx

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/


:

Hi Eric

The macro is working now. Thank you so much.

By the way, do you know any websites are good for learning VBA.

I remember you recommended http://www.outlookcode.com/d/vb.htm last
time. Anymore sites?

Thanks again.

If the code is exiting at the "If ActiveInspector Is Nothing Then Exit Sub"
line, then this is because you do not have a new e-mail message open. Using
Word as your editor has no effect on the code. Remove the "On Error Resume
Next" line to see exactly where the error is occuring. My guess is because
the objMessage variable evaluates to Nothing, which means you don't have a
message open.

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/

:

Hi Eric

I check the macro security setting to Medium. I also restart my MS
Outlook. The macro doesn't work still.

I also try to set a breakpoint. I found the macro stop/exit the
subroutine at this statement "If ActionInspector Is Nothing Then Exit
Sub".

When I make the statement "If ActionInspector Is Nothing Then Exit
Sub" as comment, the macro is run. The file in C:\Temp deleted and the
MsgBox is popped up. HOWEVER, it can't attach attachments. I mean no
attachments attach in the email message body.

Is it because I am using MS Word as editor for Outlook message that
make it can't attach attachments? If so, which part of code I need to
change? Because I need to use MS Word as editor for Outlook message.

Thanks

Check your macro security settings from the Tools | Macro| Security menu.
You may have to restart Outlook for any changes to take effect. Then try
running the macro again, making sure that you have a new message window open.

Also try setting a breakpoint on the first line of the procedure to verify
that it is firing at all.

You may also want to review the basics of Outlook VBA. See this link for
some great info:

Visual Basic and VBA Coding in Microsoft Outlook:
http://www.outlookcode.com/d/vb.htm

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/


:

Hello Eric

When I click Tools -> Macro ->
AttachAllPDFFilesFromFolderToCurrentMessageAndDeleteWhenFinished in
the email message body under Word editor, there is nothing happen. No
files attached. I already create a Temp folder under C: drive and put
a PDF file in the folder. Did I do something wrong?

What I did is:
I open my MS Outlook and reply a message. A email message with Word
editor pop up. Then, I click Tools (on the toolbar)-> Macro->Visual
Basic Editor.

Under VB Editor, I click Tools->References-> Check Microsoft Scripting
Runtime libary checkbox and keep the other default options. Then, I
insert a module and put the coding in the Sub AttachAll...When
Finished() End Sub. Am I doing the right thing?

Anything I haven't done that make it doesn't work?

Thank you

No special considerations for handling WordMail is required. This macro
should do what you want - just change the folder path, make sure the e-mail
is open and set a reference in the Outlook VBA Project to the Microsoft
Scripting Runtime library.

Sub AttachAllPDFFilesFromFolderToCurrentMessageAndDeleteWhenFinished()
On Error Resume Next

Dim objFS As New Scripting.FileSystemObject, objFolder As
Scripting.Folder, objFile As Scripting.File
Dim objMessage As Object, intX As Integer
Dim strFolderPath As String

If ActiveInspector Is Nothing Then Exit Sub

Set objMessage = ActiveInspector.CurrentItem
strFolderPath = "C:\Temp"

Set objFolder = objFS.GetFolder(strFolderPath)

For Each objFile In objFolder.Files
If Right(objFile.Name, 3) = "pdf" Then
objMessage.Attachments.Add (objFile.Path)
objFile.Delete (True)
End If
Next

MsgBox "Your PDF files have been attached, and the sources deleted.",
vbOKOnly + vbInformation, "Operation Complete"

Set objFS = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Set objMessage = Nothing
End Sub

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/

:

Hi All

I haven't touched Outlook/Word VBA before. So, I'm a newbie.

But, I need to write Outlook/Word Macro to do some works. I say
Outlook/Word Macro because I don't know I should write Outlook or Word
Macro if I want to attach files to Outlook email message using Word as
editor.

My situation is:

I download a number of PDF files from the Internet into a folder
called Temp. I want to write a Outlook/Word Macro to attach all the
PDF files in Temp folder to email message as attachments. Just like
press a button and all files will be attached in the email message.
After all PDF files are attached and sent, all PDF files in Temp
folder will be deleted. Is it possible?

And, can anyone show me some coding on implementing this?

Thank you very much
 
Back
Top