Button to detach an attachment not working - Please help!

I have an email with a button attached to detach an attachment to their notes directory.

Dim session As New NotesSession

Dim server As String

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim dbug As NotesLog

Dim rtitem As Variant

Dim fileCount As Integer

Dim it As NotesItem

server = session.GetEnvironmentString("Directory", True)

REM Specify the size limit for attachments

Const MaxSize = 5

fileCount = 0

If ( rtitem.Type = RICHTEXt ) Then

Forall o In rtitem.EmbeddedObjects

REM Note how many files we have processed

fileCount = fileCount + 1

dbug.LogAction(“file count:”+Cstr(fileCount))

If ( o.Type = EMBED_ATTACHMENT ) Then

Call o.ExtractFile( “c:\tmp\newfile” & Cstr( fileCount ) )

Call o.Remove

REM Note that we removed an attachment

dbug.LogAction(“attachment removed”)

REM Created a field noting that we removed an attachment

doc.stripped = “yes”

Call doc.Save( True, True )

End If

End Forall

End If

When I click the button I get an error of:

Variant does not contain an object

I’ve checked the Notes Help, which provided the same code as what I wrote. But it’s not working. Any suggestions?

Thanks

Edmound

Subject: Button to detach an attachment not working - Please help!

run it through debugger to see what line is throwing the error. I suspect it is this line but let us know for sure:

Forall o In rtitem.EmbeddedObjects

Subject: RE: Button to detach an attachment not working - Please help!

When I run the Debugger it’s pointing to this:

If ( rtitem.Type = RICHTEXT ) Then

Any thoughts?

Subject: RE: Button to detach an attachment not working - Please help!

The variant does not contain an object…

Doesn’t look like you set what rtitem is - so it fails with this logical error message saying that it knows that its a variant (as you dim’d it), but doesn’t know what it is…or what object it contains. Add a line to set the rtitem - the notes help will show you how…

Subject: RE: Button to detach an attachment not working - Please help!

Now I get Object Varible not set. this is what I was orginall getting when I was using the example from Notes Help.

This is how I set the rtitem:

Set rtitem = doc.GetFirstItem( “Body” )

Thanks

Edmound

Subject: RE: Button to detach an attachment not working - Please help!

Hi,You need to Dim and Set Everything.

I don’t have time to fix it all, but here is the code without that error at least. (You need to set what doc is as well).

Not sure its true in this case (not sure what help page you are looking at) but in general, in the notes help, it says “Set value of doc” - and that’s what’s required here.

To fix the next error that appears (a good learning process :slight_smile: Dim and set all the variables. If you are using it, it needs to be Dim and Set. Put Option Declare on that is helpful.

Sub Click(Source As Button)

Dim session As New NotesSession

Dim server As String

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim dbug As NotesLog

Dim rtitem As Variant

Dim fileCount As Integer

Dim it As NotesItem

Dim workspace As New NotesUIWorkspace

'Dim uidoc as a notesuidocument

Dim uidoc As NotesUIDocument

'Set the uidoc to the current open document

Set uidoc = workspace.CurrentDocument

'Set doc as the backend document

Set doc = uidoc.Document

server = session.GetEnvironmentString(“Directory”, True)

Set rtitem = doc.GetFirstItem( “Body” )

REM Specify the size limit for attachments

Const MaxSize = 5

fileCount = 0

If ( rtitem.Type = RICHTEXt ) Then

Forall o In rtitem.EmbeddedObjects

REM Note how many files we have processed

fileCount = fileCount + 1

'Presuming the doc has an attachment, now the below line

'has an error, probably because dbug isnt Dim and set

dbug.LogAction(“file count:”+Cstr(fileCount))

If ( o.Type = EMBED_ATTACHMENT ) Then

Call o.ExtractFile( “c:\tmp\newfile” & Cstr( fileCount ) )

Call o.Remove

REM Note that we removed an attachment

dbug.LogAction(“attachment removed”)

REM Created a field noting that we removed an attachment

doc.stripped = “yes”

Call doc.Save( True, True )

End If

End Forall

End If

End Sub

'Hopefully this is a help to you…

Subject: Got it working! Thanks for all your help

I was having a slight issue with the naming of the attached document, but figured it out…Thanks for your help with this I really appreciate it!