[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

Moving $File Attachments

I need to move each $File attachments from one document to multiple documents. I am currently using the code below to get hold of the attachment, but can't find a way to save it to the other document. Help in solving this problem will be greatly appreciated.

     Dim sesCurrent As New NotesSession
     Dim dbCurrent As NotesDatabase
     Dim docCurrent As NotesDocument
     Dim docRDdoc As NotesDocument
     Dim item As NotesItem
     Dim emb As Variant
     Set dbCurrent = sesCurrent.CurrentDatabase
     Set docCurrent = sesCurrent.DocumentContext
     Set docRDdoc  = dbCurrent.Createdocument
     Forall i In docCurrent.Items
          If i.type = Attachment Then
               Set emb = docCurrent.GetAttachment(i.values(0))
                 "HOW DO I STORE THE OBJECT to docRDdoc"              
          End If
     End Forall
     
0
pcorreya
Asked:
pcorreya
  • 5
  • 4
  • 3
  • +1
1 Solution
 
CRAKCommented:
Can't you detach the attachments automatically (using NotesEmbeddedObject.ExtractFile(path)), re-attach them in those other documents (Set notesEmbeddedObject = notesRichTextItem.EmbedObject( type%, class$, source$, [ name$ ] ) - see Help) and delete the detached files?
On a server, as an agent, you would need unrestricted access to do this!
0
 
IceMountainCommented:
I did what CRAK suggested before. It worked.
0
 
zvonkoCommented:
Hello pcorreya,

do it with a trick! <|;-)
Simply copy all items, and then delete or replace this items you like to have diferent.

Here your code for this:

Sub Initialize
  Dim sesCurrent As New NotesSession
  Dim dbCurrent As NotesDatabase
  Dim docCurrent As NotesDocument
  Dim docRDdoc As NotesDocument

  Set dbCurrent = sesCurrent.CurrentDatabase
  Set docCurrent = sesCurrent.DocumentContext
  Set docRDdoc  = dbCurrent.Createdocument
  Call docCurrent.CopyAllItems( docRDdoc, True )
  docRDdoc.Subject = "Only a copy"
  Call docRDdoc.RemoveItem( "AnyUnNeededField" )
  Call docRDdoc.Save(True, False)
End Sub

Regards,
zvonko
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CRAKCommented:
That's dirty!
Love it!
0
 
pcorreyaAuthor Commented:
CRAK,

These are not embedded attachments in richtext fields. They were attached from the web using the file upload control. The notesembeddedobject doesn't work for it.

I also tried the following code and get a type mismatch error.

Forall o in doc.embeddedobjects
   print o.name
End forall

Thanks
Patrick
0
 
zvonkoCommented:
Hey Patrick,

have you tried my trick? It works in any case, 'cose it's the way replication is working...

Regards,
zvonko
0
 
pcorreyaAuthor Commented:
zvonko

Thanks for your comments, I would have used your trick if I was copying all attachments to another document. But I have to copy each $file attachment to seperate documents, that is the problem. Also, the document contains about 150 fields on it.

Thanks
Patrick
0
 
CRAKCommented:
This type mismatch that you when testing

Forall o in doc.embeddedobjects
  print o.name
End forall

Where does it occur?
If in the Print-line: was o aready DIMmed?
Is the "found" embedded object an attachment?
You have noticed that the property is not available on all platforms?
Or did you trigger it from an unsaved UIDoc?

What if you store @AttachmentNames in a Computed field and read that field in script: you could try:
Set notesEmbeddedObject = notesDocument.GetAttachment( fileName$ )
0
 
zvonkoCommented:
Hello Patrick,

I would say, this an extra 300 points answer ;-))

Use this:
'CopyAttachments:
Option Public
Option Declare

Sub Initialize
  Dim sesCurrent As New NotesSession
  Dim dbCurrent As NotesDatabase
  Dim docCurrent As NotesDocument
  Dim docRDdoc As NotesDocument
  Dim srcItem As NotesItem
 
  Set dbCurrent = sesCurrent.CurrentDatabase
  Set docCurrent = sesCurrent.DocumentContext
  Set docRDdoc  = dbCurrent.Createdocument
  Call docCurrent.CopyAllItems( docRDdoc, True )
  Forall  item In docCurrent.Items
    If ( item.Name <> "$FILE" ) Then
      Call docRDdoc.RemoveItem( item.Name )
    End If
  End Forall
  docRDdoc.Subject = "CopyOfAttachmentsOnly"
  Call docRDdoc.Save(True, False)
End Sub

One small note: the subject title is not totaly true. The reason is I do not realy check for item.Type == ATTACHMENT. So you get also OLE items with name $FILE copied. If this is also a problem for you, then open a NEW question for this; OK. <|;-)

I hope you are helpped now... ;-)
zvonko
0
 
pcorreyaAuthor Commented:
Hi zvonko

You did not ready my previous comment properly, I need to copy each attachment to a seperate document. If I wanted to copy all attachment to another document they your suggestion would work.

Thanks
Patrick

0
 
zvonkoCommented:
Hello Patrick,

this is realy the last try to get my points ;-)

So take this and give me an A <|;-)

Sub Initialize
  Dim sesCurrent As New NotesSession
  Dim dbCurrent As NotesDatabase
  Dim docCurrent As NotesDocument
  Dim docRDdoc As NotesDocument
  Dim docMOREdoc As NotesDocument
  Dim FileCount As Integer
  Dim FileNames List As String
 
  Set dbCurrent = sesCurrent.CurrentDatabase
  Set docCurrent = sesCurrent.DocumentContext
  Set docRDdoc  = dbCurrent.Createdocument
  '//now create a master copy
  FileCount = 0
  Call docCurrent.CopyAllItems( docRDdoc, True )
  Forall  item In docRDdoc.Items
    If ( item.Name <> "$FILE" ) Then
      Call docRDdoc.RemoveItem( item.Name )
    Else
      FileCount = FileCount + 1
      FileNames( FileCount ) = item.Values(0)
    End If
  End Forall
  '//now make copies and reduce to one attachment
  If (FileCount = 1) Then
    docRDdoc.Subject = "CopyOfAttachment: " & FileNames(1)
    Call docRDdoc.Save(True, False)  
  Elseif (FileCount > 1) Then
    Forall attname In FileNames
      Set docMOREdoc  = dbCurrent.Createdocument
      Call docRDdoc.CopyAllItems( docMOREdoc, True )
      Forall  item In docMOREdoc.Items
        If ( item.Values(0)  <> attname ) Then Call item.Remove
      End Forall
      docMOREdoc.Subject = "CopyOfAttachment: " & attname
      Call docMOREdoc.Save(True, False)  
    End Forall
    Delete docRDdoc '//not realy necessary
  End If
  '//job done
End Sub

Good luck,
zvonko
0
 
pcorreyaAuthor Commented:
zvonko

Quite a nifty solution, it works. I give you full credit for this. Thanks for your perseverance, people like you who give it a 100% to find a solution is a reason why this site is so popular. It is a pity that Notes doesn't make it easy to do a simple operation like this.

Regards
Patrick
0
 
zvonkoCommented:

Thank you Patrick  for the points and for the feedback <|;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now