File upload code not working - repost from May 21

I have an agent that is supposed to upload PDF files and someone (Matt) was helpful and suggested additional code but I am still not getting any files uploading in the “PDFFileAttachment” RT field. Previous to that the result was that some icon was appearing next to the field with the filename (not what I wanted).

One thing I noticed is that the TheAttachment variable is not getting any values passed to it (do I have too many parameters in it?):

Set TheAttachment = rtitem.EmbedObject(EMBED_ATTACHMENT,“”, pdfFilePath, pdfFile)

My apologies if this reposting may be breaking some of the rules here - the thread was getting a bit long. My LS skills are limited and would appreciate any assistance to let me know where this is going wrong.

Dan

Option Public

Option Declare

Sub Initialize

Dim session As New Notessession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim profiledoc As NotesDocument

Dim rtfBody As NotesRichTextItem

Dim v As NotesView

Dim TheAttachment As NotesEmbeddedObject

Dim rtitem As Variant



Dim pdfDirectory As String

Dim pdfPartial As String

Dim pdfFile As String

Dim pdfFileName As String

Dim pdfFilePath As String



Set db = session.CurrentDatabase

'Initialize agent log activity recording

Dim agentLog As New NotesLog( "Agent Log" )

Call agentLog.OpenNotesLog( "", "agentlog.nsf" )

’ LOG: number of documents to be processed

agentLog.LogAction("Number of documents processed: " & db.UnprocessedDocuments.Count)

'get the “(Database Configuration)” Profile document based on the current user and get items from it

Set profiledoc = db.GetProfileDocument("(Database Configuration)")



pdfDirectory = profiledoc.CSVFolder(0)



Set v =  db.getview("vImportedCSVRecords")

Set doc = v.GetFirstDocument

'Cycle through the vImportedCSVRecords view, and starting with the first document go through the contents

Do While Not( doc Is Nothing )

     ' LOG: nth document

	agentLog.LogAction("Processed document - Policy #: " & doc.Policy(0))

'Get a handle on the last four digits of the attachment file we are looking for by getting the value of the field called FourDig

	pdfPartial = Trim$(doc.FourDig(0))

	pdfFileName = FindPDFFile( pdfPartial, pdfDirectory )

	If pdfFileName <> "" Then

		pdfFile = pdfPartial

		

		Forall i In doc.Items

			If i.Name = "PDFFileAttachment" Then

				If Not Isempty(i.EmbeddedObjects) Then

					Forall o In i.EmbeddedObjects

						If rtItem.Type = RICHTEXT Then

							pdfFilePath = pdfDirectory & pdfFileName

							Set TheAttachment = rtitem.EmbedObject(EMBED_ATTACHMENT,"", pdfFilePath, pdfFile)

						End If   

					End Forall

				End If 

			End If

		End Forall 

		

		Call doc.Save( True, True )

		

	End If

	Set doc = v.GetNextDocument(doc)

Loop 

End Sub

Function FindPDFFile( fileNumber As String, pdfDirectory As String ) As String

Dim pdfDirFile As String



FindPDFFile = ""



pdfDirFile$ = Dir$( pdfDirectory & "*.pdf", 0 )



Do While pdfDirFile <> ""

	If pdfDirFile$ Like ("*" & fileNumber$ & ".pdf") Or pdfDirFile$ Like ("*" & fileNumber$ & ".PDF")Then

		FindPDFFile = pdfDirFile$

		Exit Function

	End If

	pdfDirFile = Dir$()

Loop

End Function

Subject: File upload code not working - repost from May 21

I don’t kno why it’s not working because we dont’ have the value of the profile document, but instead of your FindPDFFile your coule use this:

From Heinz Ulrich Krause,

dummy = ExistFileDirDrive(“FilePath”)

If Not(dummy > 0) Then

Doesn’t exist

else

'Exist

end if

Function ExistFileDirDrive(FilePathName As String) As Integer

Module = Getthreadinfo(1)

Dim nTest As String

nTest = Lcase$(FilePathName)

If Len(nTest) = 1 Then

Select Case Left$(nTest, 1)

Case “a” To “z”

nTest = FilePathName & ":"

End Select

Elseif Len(nTest) = 2 Then

Select Case Left$(nTest, 2)

Case “a:” To “z:”

nTest = FilePathName & ""

End Select

End If

On Error Resume Next

ExistFileDirDrive = Cint(Getattr(nTest) Or vbNormal _

Or vbHidden Or vbSystem Or vbArchive Or vbDirectory)

End Function

JYR

Subject: File upload code not working - repost from May 21

I did not test the code, but this seems to be the problem:

Forall i In doc.Items

If i.Name = "PDFFileAttachment" Then

	If Not Isempty(i.EmbeddedObjects) Then

		Forall o In i.EmbeddedObjects

			If rtItem.Type = RICHTEXT Then

				pdfFilePath = pdfDirectory & pdfFileName

				Set TheAttachment = rtitem.EmbedObject(EMBED_ATTACHMENT,"", pdfFilePath, pdfFile)

			End If   

		End Forall

	End If 

End If

End Forall

you did not declare the rtItem… (in your case it would be ‘i’)

Try that instead (without the forall and doc.save, between ‘pdfFile = pdfPartial’ and ‘End if’)

Change the declaration of the ‘rtitem’ from variant to NotesRichTextItem.

Dim bDoAtt as Boolean

bDoAtt = false

If doc.hasitem(“PDFFileAttachment”) then

set rtItem = doc.getfirstitem("PDFFileAttachment")

if not isArray( rtItem.EmbeddedObjects ) then bDoAtt = true

else

Set rtItem = New NotesRichTextItem ( doc, "PDFFileAttachment" )

bDoAtt = true

End If

if bDoAtt then

pdfFilePath =  pdfDirectory & pdfFileName

Set TheAttachment = rtItem.EmbedObject ( EMBED_ATTACHMENT, "", pdfFilePath )

call doc.save(true,false)

end if

In some cases it might be that the icon is only a grey document symbol. The only way I’ve found to resolve this is to use uiclasses to attach documents into a RTField

Subject: RE: File upload code not working - repost from May 21

Hi Daniel,I tried your code and the same thing happens. Also, the icon is only a grey document symbol.

You also said : “The only way I’ve found to resolve this is to use uiclasses to attach documents into a RTField” - how would someone proceed in that fashion when trying to apply this to all the documents in the view via a doc collection?

By the way what should the i be declared as and set at?

Here is the new code updated with yours:

Sub Initialize

Dim session As New Notessession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim profiledoc As NotesDocument

Dim v As NotesView

Dim TheAttachment As NotesEmbeddedObject

Dim rtitem As Variant

Dim success As Variant



Dim pdfDirectory As String

Dim pdfPartial As String

Dim pdfFile As String

Dim pdfFileName As String

Dim pdfFilePath As String



Set db = session.CurrentDatabase

'Initialize agent log activity recording

Dim agentLog As New NotesLog( "Agent Log" )

Call agentLog.OpenNotesLog( "", "agentlog.nsf" )

’ LOG: number of documents to be processed

agentLog.LogAction("Number of documents processed: " & db.UnprocessedDocuments.Count)

'get the “(Database Configuration)” Profile document based on the current user and get items from it

Set profiledoc = db.GetProfileDocument("(Database Configuration)")



pdfDirectory = profiledoc.CSVFolder(0)



Set v =  db.getview("vImportedCSVRecords")

Set doc = v.GetFirstDocument

'Cycle through the vImportedCSVRecords view, and starting with the first document go through the contents

Do While Not( doc Is Nothing )

     ' LOG: nth document

	agentLog.LogAction("Processed document - Policy #: " & doc.Policy(0))

'Get a handle on the last four digits of the attachment file we are looking for by getting the value of the field called FourDig

	pdfPartial = Trim$(doc.FourDig(0))

	pdfFileName = FindPDFFile( pdfPartial, pdfDirectory )

	If pdfFileName <> "" Then

		pdfFile = pdfPartial

		Set rtitem = doc.GetFirstItem( "PDFFileAttachment" )

		Forall i In doc.Items

			If i.Name = "PDFFileAttachment" Then

				If Isempty(i.EmbeddedObjects) Then

					Dim bDoAtt As Boolean

					bDoAtt = False

					

					If doc.hasitem("PDFFileAttachment") Then

						Set rtItem = doc.getfirstitem("PDFFileAttachment")

						If Not Isarray( rtItem.EmbeddedObjects ) Then bDoAtt = True

					Else

						Set rtItem = New NotesRichTextItem ( doc, "PDFFileAttachment" )

						bDoAtt = True

					End If

					

					If bDoAtt Then

						pdfFilePath = pdfDirectory & pdfFileName

						Set TheAttachment = rtItem.EmbedObject ( EMBED_ATTACHMENT, "", pdfFilePath )

						Call doc.save(True,False)

					End If

				End If 

			End If

		End Forall 

		

		success = doc.ComputeWithForm( False, False )

		If success Then

			Call doc.Save( True, True )

			Messagebox "Document was saved!"

		Else

			Messagebox "Document was NOT saved!"

		End If			

		'Call doc.Save( True, True )

		

	End If

	Set doc = v.GetNextDocument(doc)

Loop 

End Sub

Subject: RE: File upload code not working - repost from May 21

Dan,

I don’t understand why you are looping through all fields in the doc (Forall i In doc.Items…) when you know the name of the field (PDFFileAttachment) where you want to attach the PDF file.

Also, it makes no sense to me that you will only attach a PDF file if there are other PDF files already attached (Forall o In i.EmbeddedObjects…). How does the first PDF file ever get attached?

Although it looks like you only want to attach one PDF file, the sample I am providing below assumes the pdfDirectory variable points to a folder path containing one or more PDF files and that you want to attach all of them to the PDFFileAttachment field.

Dim attachedFile As NotesEmbeddedObject



' Store files found in pdfDirectory as attachments to the current doc.  

Set rtitem = doc.GetFirstItem("PDFFileAttachment")						' "New" bombs if rich text field already exists, so test for it this way.

If rtitem Is Nothing Then														' If rich text field does not exist in doc (it can exist in form)...

	Set rtitem = New NotesRichTextItem(doc, "PDFFileAttachment")	' Define NEW rich text field for holding file attachments.

End If



pdfFileName = Dir$(pdfDirectory & "*.pdf", 0)							' Obtain name of the first file that we will attach to the doc.



Do While pdfFileName <> ""

	Set attachedFile = doc.GetAttachment(pdfFileName)			' Check to see if a file of the same name is already attached to the doc.

	If Not attachedFile Is Nothing Then								' If so, we don't want duplicates...

		Call attachedFile.Remove										' Delete the earlier file attachment.

	End If



	Call rtitem.EmbedObject(EMBED_ATTACHMENT, "", pdfDirectory & pdfFileName)	' Attach current file found in folder to rich text field in doc.

	pdfFileName = Dir$()

Loop

Ken A Collins

Hoboken, NJ

Subject: RE: File upload code not working - repost from May 21

Hi Ken,thats GREAT stuff thanks. HOWEVER, it uploads ALL the PDF files found in the directory pointed to. I need the PDF filenames matching the last 4 digits of the policy number field (stored in the FourDig field) to be uploaded. In other words, if the FourDig field has a value of 1034 and the PDF files found are PSOT0111034.pdf and TSO111034.pdf they should both be uploaded. But if only TSO111034.pdf is found then thats the one to be uploaded otherwise do not upload anything and go on to the next document.

Thanks, Dan

Here is my updated code:

Sub Initialize

Dim session As New Notessession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim profiledoc As NotesDocument

Dim v As NotesView

Dim TheAttachment As NotesEmbeddedObject

Dim rtitem As Variant

Dim success As Variant



Dim pdfDirectory As String

Dim pdfPartial As String

Dim pdfFile As String

Dim pdfFileName As String

Dim pdfFilePath As String



Set db = session.CurrentDatabase

'Initialize agent log activity recording

Dim agentLog As New NotesLog( "Agent Log" )

Call agentLog.OpenNotesLog( "", "agentlog.nsf" )

’ LOG: number of documents to be processed

agentLog.LogAction("Number of documents processed: " & db.UnprocessedDocuments.Count)

'get the “(Database Configuration)” Profile document based on the current user and get items from it

Set profiledoc = db.GetProfileDocument("(Database Configuration)")



pdfDirectory = profiledoc.CSVFolder(0)



Set v =  db.getview("vImportedCSVRecords")

Set doc = v.GetFirstDocument

'Cycle through the vImportedCSVRecords view, and starting with the first document go through the contents

Do While Not( doc Is Nothing )

     ' LOG: nth document

	agentLog.LogAction("Processed document - Policy #: " & doc.Policy(0))

'Get a handle on the last four digits of the attachment file we are looking for by getting the value of the field called FourDig

	pdfPartial = Trim$(doc.FourDig(0))

	pdfFileName = Dir$(pdfDirectory & "*.pdf", 0) ' Obtain name of the first file that we will attach to the doc		

	

	pdfFile = pdfPartial

	

	Set rtitem = doc.GetFirstItem( "PDFFileAttachment" )

'If the PDFFileAttachment is empty then activate same field to accept attachment(s)

	If rtitem Is Nothing Then 

		Set rtitem = New NotesRichTextItem( doc, "PDFFileAttachment" ) ' Define NEW RTF for holding file attachment(s)

	End If

	Do While pdfFileName <> ""

		Set TheAttachment = doc.GetAttachment(pdfFileName) ' Check to see if a file of the same name is already attached to the doc.

		If Not TheAttachment Is Nothing Then ' If so, we don't want duplicates...

			Call TheAttachment.Remove ' Delete the earlier file attachment.

		End If

		

		Call rtitem.EmbedObject(EMBED_ATTACHMENT, "", pdfDirectory & pdfFileName) ' Attach current file found in folder to rich text field in doc.

		pdfFileName = Dir$()

	Loop

	

	Call doc.Save( True, True )

	

	Set doc = v.GetNextDocument(doc)

Loop 

End Sub

Subject: RE: File upload code not working - repost from May 21

Thanks Daniel but I did declare the ritem way up at the top of the code.

Subject: RE: File upload code not working - repost from May 21

‘declare’ was the wrong word… you did not initiate (set) it

Subject: RE: File upload code not working - repost from May 21

Hi Daniel,when putting the line :

Set rtitem = doc.GetFirstItem( “PDFFileAttachment” )

I get a type mismatch error message on the following line:

Forall o In i.EmbeddedObjects

Sub Initialize

Dim session As New Notessession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim profiledoc As NotesDocument

Dim rtfBody As NotesRichTextItem

Dim v As NotesView

Dim TheAttachment As NotesEmbeddedObject

’ Dim rtitem As Variant

Dim rtitem As NotesRichTextItem



Dim pdfDirectory As String

Dim pdfPartial As String

Dim pdfFile As String

Dim pdfFileName As String

Dim pdfFilePath As String



Set db = session.CurrentDatabase

'Initialize agent log activity recording

Dim agentLog As New NotesLog( "Agent Log" )

Call agentLog.OpenNotesLog( "", "agentlog.nsf" )

’ LOG: number of documents to be processed

agentLog.LogAction("Number of documents processed: " & db.UnprocessedDocuments.Count)

'get the “(Database Configuration)” Profile document based on the current user and get items from it

Set profiledoc = db.GetProfileDocument("(Database Configuration)")



pdfDirectory = profiledoc.CSVFolder(0)



Set v =  db.getview("vImportedCSVRecords")

Set doc = v.GetFirstDocument

'Cycle through the vImportedCSVRecords view, and starting with the first document go through the contents

Do While Not( doc Is Nothing )

     ' LOG: nth document

	agentLog.LogAction("Processed document - Policy #: " & doc.Policy(0))

'Get a handle on the last four digits of the attachment file we are looking for by getting the value of the field called FourDig

	pdfPartial = Trim$(doc.FourDig(0))

	'Messagebox "pdfPartial = " & pdfPartial

	pdfFileName = FindPDFFile( pdfPartial, pdfDirectory )

	If pdfFileName <> "" Then

		pdfFile = pdfPartial



		Set rtitem = doc.GetFirstItem( "PDFFileAttachment" )

		Forall i In doc.Items

			If i.Name = "PDFFileAttachment" Then

				If Isempty(i.EmbeddedObjects) Then

					Forall o In i.EmbeddedObjects

						If rtItem.Type = RICHTEXT Then

							pdfFilePath = pdfDirectory & pdfFileName

							Set TheAttachment = rtitem.EmbedObject(EMBED_ATTACHMENT,"", pdfFilePath, pdfFile)

						End If   

					End Forall

				End If 

			End If

		End Forall 

		Call doc.Save( True, True )

		

	End If

	Set doc = v.GetNextDocument(doc)

Loop 

End Sub

Subject: RE: File upload code not working - repost from May 21

Here is another twist …I returned to a previous code but the problem is that instead of putting the file in the “PDFFileAttachment” field it added a grey icon to the right side of the field. When looking at the code as the LS Debugger runs, I noticed that the value of the “PDFFileAttachment” field is " - and a couple of weird symbols":

Sub Initialize

Dim session As New Notessession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim profiledoc As NotesDocument

Dim rtfBody As NotesRichTextItem

Dim v As NotesView

Dim TheAttachment As NotesEmbeddedObject

Dim rtitem As Variant



Dim pdfDirectory As String

Dim pdfPartial As String

Dim pdfFile As String

Dim pdfFileName As String

Dim pdfFilePath As String



Set db = session.CurrentDatabase

'Initialize agent log activity recording

Dim agentLog As New NotesLog( "Agent Log" )

Call agentLog.OpenNotesLog( "", "agentlog.nsf" )

’ LOG: number of documents to be processed

agentLog.LogAction("Number of documents processed: " & db.UnprocessedDocuments.Count)

'get the “(Database Configuration)” Profile document based on the current user and get items from it

Set profiledoc = db.GetProfileDocument("(Database Configuration)")



pdfDirectory = profiledoc.CSVFolder(0)



Set v =  db.getview("vImportedCSVRecords")

Set doc = v.GetFirstDocument

'Cycle through the vImportedCSVRecords view, and starting with the first document go through the contents

Do While Not( doc Is Nothing )

     ' LOG: nth document

	agentLog.LogAction("Processed document - Policy #: " & doc.Policy(0))

'Get a handle on the last four digits of the attachment file we are looking for by getting the value of the field called FourDig

	pdfPartial = Trim$(doc.FourDig(0))

	'Messagebox "pdfPartial = " & pdfPartial

	pdfFileName = FindPDFFile( pdfPartial, pdfDirectory )

	If pdfFileName <> "" Then

		pdfFile = pdfPartial

		Set rtitem = doc.GetFirstItem( "PDFFileAttachment" )

'If the PDFFileAttachment is empty then activate same field to accept attachment(s)

		If rtitem Is Nothing Then Set rtitem = New NotesRichTextItem( doc, "PDFFileAttachment" )

		If rtItem.Type = RICHTEXT Then

			pdfFilePath = pdfDirectory & pdfFileName

			Set TheAttachment = rtitem.EmbedObject(EMBED_ATTACHMENT,"", pdfFilePath, pdfFile)

		End If   

		Call doc.Save( True, True )

		

	End If

	Set doc = v.GetNextDocument(doc)

Loop 

End Sub