如何从Notes富文本项提取图片?跨库复制图片代码故障求助
Question 1: How to Extract Images from a Notes RichTextItem?
Extracting images from a RichTextItem depends entirely on how the image is stored in the document—either as a file attachment or an embedded picture. Here’s how to handle both scenarios:
- For file attachments (images added via "Attach File"): Use the
RTELEM_TYPE_FILEATTACHMENTconstant with aNotesRichTextNavigatorto locate the attachment. You can then save it to your local system using theExtractFilemethod. - For embedded pictures (images inserted directly into rich text): Use
RTELEM_TYPE_PICTUREinstead. These are embedded directly in the rich text stream, so you’ll typically copy them to another rich text item (or use additional logic if you need to export them as standalone files).
Here’s a quick example that covers both cases:
Dim session As New NotesSession Dim db As NotesDatabase Dim doc As NotesDocument Dim rtItem As NotesRichTextItem Dim rtNav As NotesRichTextNavigator Dim elem As Variant Set db = session.CurrentDatabase Set doc = db.GetFirstDocument() Set rtItem = doc.GetFirstItem("YourRichTextFieldName") Set rtNav = rtItem.CreateNavigator() ' Check for embedded pictures first Set elem = rtNav.GetFirstElement(RTELEM_TYPE_PICTURE) While Not elem Is Nothing Print "Found an embedded picture in the rich text" ' You can copy this element to another rich text item here Set elem = rtNav.GetNextElement(RTELEM_TYPE_PICTURE) Wend ' Now check for image attachments Set elem = rtNav.GetFirstElement(RTELEM_TYPE_FILEATTACHMENT) While Not elem Is Nothing ' Extract the attachment to a local folder elem.ExtractFile("C:\LocalTemp\" & elem.Source) Print "Extracted image attachment: " & elem.Source Set elem = rtNav.GetNextElement(RTELEM_TYPE_FILEATTACHMENT) Wend
Question 2: Why Isn’t RTELEM_TYPE_FILEATTACHMENT Retrieving My Image?
The problem here is that your image isn’t stored as a file attachment—it’s an embedded picture in the rich text field. That’s why rtnav.GetFirstElement(RTELEM_TYPE_FILEATTACHMENT) returns Nothing—it’s looking for the wrong element type.
To fix this, switch to targeting RTELEM_TYPE_PICTURE instead. Here’s your modified code, plus the logic to copy the image to another document’s rich text item:
Dim rtnav As NotesRichTextNavigator Dim rti As NotesRichTextItem Dim CRMPhoto As Variant Dim targetDB As NotesDatabase Dim targetDoc As NotesDocument Dim targetRTI As NotesRichTextItem ' Initialize source document and rich text item Set rti = doccrm.GetFirstItem("Seller_Photo") Set rtnav = rti.CreateNavigator ' Look for embedded pictures (not attachments) Set CRMPhoto = rtnav.GetFirstElement(RTELEM_TYPE_PICTURE) If CRMPhoto Is Nothing Then Messagebox "Document contains no embedded image" Exit Function End If ' Set up your target database and document (adjust paths/names as needed) Set targetDB = New NotesDatabase("", "YourTargetDatabase.nsf") Set targetDoc = targetDB.CreateDocument() Set targetRTI = targetDoc.CreateRichTextItem("TargetPhotoField") ' Copy the image to the target rich text item While Not(CRMPhoto Is Nothing) Call targetRTI.AppendElement(CRMPhoto) Set CRMPhoto = rtnav.GetNextElement(RTELEM_TYPE_PICTURE) Wend ' Save the target document Call targetDoc.Save(True, False) Messagebox "Image copied to target document successfully!"
Quick Troubleshooting Tip:
If you still don’t find the image with RTELEM_TYPE_PICTURE, check if it’s stored as an OLE object (use RTELEM_TYPE_OLEOBJECT). This is rare for standard images, but possible if the image was inserted via an external app.
内容的提问来源于stack exchange,提问作者Babur Mansoor

