Attachments collection bug ?

Status
Not open for further replies.
C

chauwel

Outlook 2K3/Exchange

I'm developping a macro to remove all embedded images from my company's mail

templates, but although the templates actually contains images, the

attachments collection remain empty at runtime. And the part that drive me

crazy: in debug mode, the collection is empty (Count=0, etc) until I expand

manually the "attachments" node of my mailitem object in the "Locals" window,

then the collection is populated.

The embbeded images are of type olOLE and there is no other attachment in

the templates. Is that a bug or am i missing something ?
 
Show the code you use.

"chauwel" <chauwel> wrote in message

news:9A547081-CABD-4989-BC2B-1E7DDF83E5FA@microsoft.com...
> Outlook 2K3/Exchange

> I'm developping a macro to remove all embedded images from my company's
> mail
> templates, but although the templates actually contains images, the
> attachments collection remain empty at runtime. And the part that drive me
> crazy: in debug mode, the collection is empty (Count=0, etc) until I
> expand
> manually the "attachments" node of my mailitem object in the "Locals"
> window,
> then the collection is populated.

> The embbeded images are of type olOLE and there is no other attachment in
> the templates. Is that a bug or am i missing something ?
 
Also, please explain just what you mean by "mail templates." That could be a

couple of different things in an Outlook context. And it might be helpful to

know why you're doing this -- doesn't that render the templates less

useful? -- and whether Word is your email editor.

Sue Mosher
<kenslovak@mvps.org> wrote in message

news:%23sVyUydbKHA.5976@TK2MSFTNGP05.phx.gbl...
> Show the code you use.

> >

>

> "chauwel" <chauwel> wrote in message
> news:9A547081-CABD-4989-BC2B-1E7DDF83E5FA@microsoft.com...
> > Outlook 2K3/Exchange
>

>> I'm developping a macro to remove all embedded images from my company's
> > mail
> > templates, but although the templates actually contains images, the
> > attachments collection remain empty at runtime. And the part that drive
> > me
> > crazy: in debug mode, the collection is empty (Count=0, etc) until I
> > expand
> > manually the "attachments" node of my mailitem object in the "Locals"
> > window,
> > then the collection is populated.
>

>> The embbeded images are of type olOLE and there is no other attachment in
> > the templates. Is that a bug or am i missing something ?

>
 
The email editor is Word 2003.

The company is undergoing a rebranding, so the logos and so on must be

removed (ideally replaced) and there is over 600 hundred .oft files to

process.

Public Sub Macro_RemoveImages()

Const PICTURE_TAG = "PICTURE (METAFILE)"

Dim olApp As New Outlook.Application

Dim mail As MailItem

Dim sourcePath As String

Dim targetPath As String

sourcePath = OpenFolderDialog(TEMPLATE_FOLDER, "Source folder:")

If sourcePath = "" Then Exit Sub

targetPath = OpenFolderDialog(TEMPLATE_FOLDER, "Target folder:")

If targetPath = "" Then Exit Sub

If UCase(targetPath) = UCase(sourcePath) Then

MsgBox "Source path and Target must be different", vbExclamation

Exit Sub

End If

Dim fso As Object, ofile As Object, folder As Object

Set fso = CreateFileSystemObject()

Dim i As Integer, c As Integer, savePath As String

Dim att As Attachment, atts() As Attachment

Set folder = fso.GetFolder(sourcePath)

For Each ofile In folder.Files

Debug.Print "*****************************"

Debug.Print "Loading " & ofile.name

Debug.Print "*****************************"

savePath = targetPath & "\" & ofile.name

Set mail = olApp.CreateItemFromTemplate(sourcePath & "\" & ofile.name)

If mail.Attachments.Count > 0 Then '<= Fails here

c = mail.Attachments.Count

ReDim atts(1 To c)

For i = 1 To c

Set att = mail.Attachments(i)

Debug.Print vbTab & "DisplayName: " & att.DisplayName

Debug.Print vbTab & "Type: " & att.Type

Debug.Print vbTab & "Position: " & att.Position

Debug.Print vbTab & "Index: " & att.Index

Debug.Print "---------------------------"

Set atts(i) = att

Next

For i = 1 To c

Set att = atts(i)

If UCase(att.DisplayName) = PICTURE_TAG Then

att.Delete

Debug.Print "Removed: " & i

End If

Next

End If

Debug.Print "Saving As " & savePath

mail.SaveAs savePath, OlSaveAsType.olTemplate

Set mail = Nothing

Next

End Sub

"Sue Mosher [MVP]" wrote:


> Also, please explain just what you mean by "mail templates." That could be a
> couple of different things in an Outlook context. And it might be helpful to
> know why you're doing this -- doesn't that render the templates less
> useful? -- and whether Word is your email editor.
> > Sue Mosher
> > >

> " - " <kenslovak@mvps.org> wrote in message
> news:%23sVyUydbKHA.5976@TK2MSFTNGP05.phx.gbl...
> > Show the code you use.
> > > >

> >

> > "chauwel" <chauwel> wrote in message
> > news:9A547081-CABD-4989-BC2B-1E7DDF83E5FA@microsoft.com...
> >> Outlook 2K3/Exchange
> >
> >> I'm developping a macro to remove all embedded images from my company's
> >> mail
> >> templates, but although the templates actually contains images, the
> >> attachments collection remain empty at runtime. And the part that drive
> >> me
> >> crazy: in debug mode, the collection is empty (Count=0, etc) until I
> >> expand
> >> manually the "attachments" node of my mailitem object in the "Locals"
> >> window,
> >> then the collection is populated.
> >
> >> The embbeded images are of type olOLE and there is no other attachment in
> >> the templates. Is that a bug or am i missing something ?

> >


> .
>
 
I have no idea why it's failing on this line:

If mail.Attachments.Count > 0 Then '<= Fails here

However, try instantiating an Attachments collection first:

Dim colAttach As Outlook.Attachments

Set colAttach = mail.Attachments

c = colAttach.Count

If c > 0 Then

"chauwel" <chauwel> wrote in message

news:4C033B21-64A2-4167-BB21-FAF62521F0EB@microsoft.com...
> The email editor is Word 2003.
> The company is undergoing a rebranding, so the logos and so on must be
> removed (ideally replaced) and there is over 600 hundred .oft files to
> process.

> Public Sub Macro_RemoveImages()
> Const PICTURE_TAG = "PICTURE (METAFILE)"
> Dim olApp As New Outlook.Application

> Dim mail As MailItem
> Dim sourcePath As String
> Dim targetPath As String

> sourcePath = OpenFolderDialog(TEMPLATE_FOLDER, "Source folder:")
> If sourcePath = "" Then Exit Sub

> targetPath = OpenFolderDialog(TEMPLATE_FOLDER, "Target folder:")
> If targetPath = "" Then Exit Sub

> If UCase(targetPath) = UCase(sourcePath) Then
> MsgBox "Source path and Target must be different", vbExclamation
> Exit Sub
> End If

> Dim fso As Object, ofile As Object, folder As Object
> Set fso = CreateFileSystemObject()

> Dim i As Integer, c As Integer, savePath As String
> Dim att As Attachment, atts() As Attachment
> Set folder = fso.GetFolder(sourcePath)

> For Each ofile In folder.Files
> Debug.Print "*****************************"
> Debug.Print "Loading " & ofile.name
> Debug.Print "*****************************"

> savePath = targetPath & "\" & ofile.name
> Set mail = olApp.CreateItemFromTemplate(sourcePath & "\" &
> ofile.name)

> If mail.Attachments.Count > 0 Then '<= Fails here
> c = mail.Attachments.Count
> ReDim atts(1 To c)

> For i = 1 To c
> Set att = mail.Attachments(i)
> Debug.Print vbTab & "DisplayName: " & att.DisplayName
> Debug.Print vbTab & "Type: " & att.Type
> Debug.Print vbTab & "Position: " & att.Position
> Debug.Print vbTab & "Index: " & att.Index
> Debug.Print "---------------------------"
> Set atts(i) = att
> Next

> For i = 1 To c
> Set att = atts(i)
> If UCase(att.DisplayName) = PICTURE_TAG Then
> att.Delete
> Debug.Print "Removed: " & i
> End If
> Next
> End If

> Debug.Print "Saving As " & savePath
> mail.SaveAs savePath, OlSaveAsType.olTemplate
> Set mail = Nothing
> Next
> End Sub
 
That does seem odd. You might want to try saving the item before attempting

to access its Attachments collection.

Alternatively, go through Inspector.WordEditor to work with the

Word.Document that forms the body of the message.

Sue Mosher

"chauwel" <chauwel> wrote in message

news:4C033B21-64A2-4167-BB21-FAF62521F0EB@microsoft.com...
> The email editor is Word 2003.
> The company is undergoing a rebranding, so the logos and so on must be
> removed (ideally replaced) and there is over 600 hundred .oft files to
> process.

> Public Sub Macro_RemoveImages()
> Const PICTURE_TAG = "PICTURE (METAFILE)"
> Dim olApp As New Outlook.Application

> Dim mail As MailItem
> Dim sourcePath As String
> Dim targetPath As String

> sourcePath = OpenFolderDialog(TEMPLATE_FOLDER, "Source folder:")
> If sourcePath = "" Then Exit Sub

> targetPath = OpenFolderDialog(TEMPLATE_FOLDER, "Target folder:")
> If targetPath = "" Then Exit Sub

> If UCase(targetPath) = UCase(sourcePath) Then
> MsgBox "Source path and Target must be different", vbExclamation
> Exit Sub
> End If

> Dim fso As Object, ofile As Object, folder As Object
> Set fso = CreateFileSystemObject()

> Dim i As Integer, c As Integer, savePath As String
> Dim att As Attachment, atts() As Attachment
> Set folder = fso.GetFolder(sourcePath)

> For Each ofile In folder.Files
> Debug.Print "*****************************"
> Debug.Print "Loading " & ofile.name
> Debug.Print "*****************************"

> savePath = targetPath & "\" & ofile.name
> Set mail = olApp.CreateItemFromTemplate(sourcePath & "\" &
> ofile.name)

> If mail.Attachments.Count > 0 Then '<= Fails here
> c = mail.Attachments.Count
> ReDim atts(1 To c)

> For i = 1 To c
> Set att = mail.Attachments(i)
> Debug.Print vbTab & "DisplayName: " & att.DisplayName
> Debug.Print vbTab & "Type: " & att.Type
> Debug.Print vbTab & "Position: " & att.Position
> Debug.Print vbTab & "Index: " & att.Index
> Debug.Print "---------------------------"
> Set atts(i) = att
> Next

> For i = 1 To c
> Set att = atts(i)
> If UCase(att.DisplayName) = PICTURE_TAG Then
> att.Delete
> Debug.Print "Removed: " & i
> End If
> Next
> End If

> Debug.Print "Saving As " & savePath
> mail.SaveAs savePath, OlSaveAsType.olTemplate
> Set mail = Nothing
> Next
> End Sub

> "Sue Mosher [MVP]" wrote:
>
> > Also, please explain just what you mean by "mail templates." That could
> > be a
> > couple of different things in an Outlook context. And it might be helpful
> > to
> > know why you're doing this -- doesn't that render the templates less
> > useful? -- and whether Word is your email editor.
> > > > Sue Mosher
> > >> >> >
>
>> " - " <kenslovak@mvps.org> wrote in message
> > news:%23sVyUydbKHA.5976@TK2MSFTNGP05.phx.gbl...
> > > Show the code you use.
> >> > > > >

> > >

> >>>>>>> > "chauwel" <chauwel> wrote in message
> > > news:9A547081-CABD-4989-BC2B-1E7DDF83E5FA@microsoft.com...
> > >> Outlook 2K3/Exchange
> > >
>> >> I'm developping a macro to remove all embedded images from my
> > >> company's
> > >> mail
> > >> templates, but although the templates actually contains images, the
> > >> attachments collection remain empty at runtime. And the part that
> > >> drive
> > >> me
> > >> crazy: in debug mode, the collection is empty (Count=0, etc) until I
> > >> expand
> > >> manually the "attachments" node of my mailitem object in the "Locals"
> > >> window,
> > >> then the collection is populated.
> > >
>> >> The embbeded images are of type olOLE and there is no other attachment
> > >> in
> > >> the templates. Is that a bug or am i missing something ?
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
P Iterate through collection of attachments Outlook VBA and Custom Forms 3
L Outlook attachments from OneDrive as links Using Outlook 0
G Print email attachments when hit subfolder Outlook VBA and Custom Forms 1
R Saving Emails and Attachments as .msg file Using Outlook 3
KurtLass Opening Graphics Attachments in Outlook 2021 Using Outlook 0
F Outlook 2016 Email with attachments not being received Using Outlook 2
Commodore PDF attachments started to open in Edge Using Outlook 0
T Outlook 2021 Cannot open attachments Outlook DeskTop 2021 Using Outlook 0
D ISOmacro to extract active mail senders name and email, CC, Subject line, and filename of attachments and import them into premade excel spread sheet Outlook VBA and Custom Forms 2
Witzker Outlook 2019 Macro to answer a mail with attachments Outlook VBA and Custom Forms 2
P Print attachments automatically and move the mail to an existing folder called "Ted" Outlook VBA and Custom Forms 4
G Schedule recurring email and attachments display Outlook VBA and Custom Forms 3
G Save and Rename Outlook Email Attachments Outlook VBA and Custom Forms 0
L Macro/VBA to Reply All, with the original attachments Outlook VBA and Custom Forms 2
A Attachments.Delete Outlook VBA and Custom Forms 3
M Deleting attachments does not reduce file size Using Outlook 0
L unblocking attachments before sending Office 365 Advanced Protection Using Outlook 0
I Saving attachments from multiple emails and updating file name Outlook VBA and Custom Forms 0
R Use an ItemAdd to Save Attachments on Arrival Outlook VBA and Custom Forms 0
shrydvd vba to secure zip attachments Outlook VBA and Custom Forms 3
R fetching blocked attachments Outlook VBA and Custom Forms 0
H Outlook 2016 sent over 30 copies of an e-mail with attachments Using Outlook 1
W Automatically open attachments without automatically printing them Using Outlook 0
O Save attachments using hotkey without changing attributes Outlook VBA and Custom Forms 1
T Outlook converts sent email to txt attachments when sync Using Outlook 0
Nadine Rule to move attachments with specific name Outlook VBA and Custom Forms 1
R Outlook is NOT removing attachments. Using Outlook 4
Dan_W Nested email attachments Outlook VBA and Custom Forms 4
M Print email and, attachments sent in hyperlinks in the email Outlook VBA and Custom Forms 2
W Save and rename outlook email attachments to include domain name & date received Outlook VBA and Custom Forms 4
S DRAGGING MAIL AND ATTACHMENTS TO CALENDAR Using Outlook 1
D Print attachments automatically and moves the mail to a new folder Outlook VBA and Custom Forms 9
G Download pdf attachments only if email subject has one of words Outlook VBA and Custom Forms 8
D Saving Selected Emails as PDF and saving Attachments Outlook VBA and Custom Forms 6
R Quick Access view in File Explorer when saving attachments Using Outlook 0
J Save E-mail attachments in a specific folder Outlook VBA and Custom Forms 0
broadbander Needing help with reply/reply all while keeping attachments and adding a new CC recipient. Outlook VBA and Custom Forms 5
I Forwarding attachments in email received Outlook VBA and Custom Forms 3
A Outlook - Send New 20 Attachments through Email Using Outlook 4
D Print Attachments only in selected emails using a macro Outlook VBA and Custom Forms 3
M Outlook 2016 msg attachments Using Outlook 0
N Saving And Deleting Outlook Attachments with Unknown Error Message Outlook VBA and Custom Forms 1
M Macro for attachments download adjustment Outlook VBA and Custom Forms 3
M VBA macro for Inbox's attachments search Outlook VBA and Custom Forms 0
C Auto save outlook attachments when email is received Outlook VBA and Custom Forms 1
S Outlook 2010 Cannot Open Attachments Using Outlook 14
I Print Automatically Attachments Outlook VBA and Custom Forms 3
Diane Poremsky Save Messages and Attachments to a New Folder Using Outlook 0
B Delete/replace old files and save new attachments Using Outlook 1
J Saving attachments from specific sender (phone number) to specific folder on hard drive Using Outlook 3

Similar threads

Back
Top