How do you convert a variant to a string in lotus script?

I’ve been looking for this but must not be searching for the right words!

CSTR does not work as far as I can tell.

Subject: how do you convert a variant to a string in lotus script?

Post your code. Cstr() should convert a variant to a string just like str() should convert an integer to a string.

Subject: how do you convert a variant to a string in lotus script?

If you have a value in a variant, and you want to make sure that it is a string value, even though a variant can hold a string, Cstr is the right way to go.

Cstr(VariableName)

What kind of issues are you running into with this? Can you post some code you are using that doesn’t seem to be working, and also tell why you think it isn’t working?

Subject: RE: how do you convert a variant to a string in lotus script?

Jerry,

I am getting a type mismatch on the line that has:

commentItem = Cstr(tempComment) '<============ type mismatch here

I’ve been looking at this for 3 days now and have to get this resolved. This is not code that I wrote, but rather inherited very suddenly. It’s been awhile since I’ve done lotus script so I’m really in a pinch now. Have run it with debigger several times and am not sure what’s going on.

’ Start of code

Class Product

Public type As String

Public name As String

Public mktcompany As String

Public newapprovalstate List As String

Public apprcomments List As String

Public unid As String

Public myRef As Product ’ Reference to an object of this class

End Class

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim dc As NotesDocumentCollection	

Dim doc As NotesDocument

Dim maildoc As NotesDocument

Dim searchString As String, lastmonth As String

Dim rtStyle1 As NotesRichTextStyle

Dim rtStyle2 As NotesRichTextStyle

Dim rtStyle3 As NotesRichTextStyle

Dim rtStyle4 As NotesRichTextStyle

Dim rtStyle5 As NotesRichTextStyle

Dim rtStyle6 As NotesRichTextStyle

Dim arr_States As Variant

Dim arr_PMStateAppr

Redim arr_PMStateAppr(0)

arr_PMStateAppr(0) = ""

Dim arr_PMStateApprDates

Redim arr_PMStateApprDates(0)

arr_PMStateApprDates(0) = ""

Dim tmp As Variant

Dim apprDate As NotesDateTime

Dim eDate As NotesDateTime

Dim apprExcept As String

Dim tempComment As Variant

Dim arr_Comments() As String 

Redim Preserve arr_Comments(0) As String

arr_Comments(0) = ""

tempComment = ""

Dim	commentItem As String

Dim strApprDate As String	

Dim PMBegDate As NotesDateTime

Dim PMEndDate As NotesDateTime

Dim item As NotesItem, itemE As NotesItem, rtitem As NotesRichTextItem

Dim AnyProductApproved As Integer, AnyStateApproved As Integer, i As Integer, Yr As String, statecnt As Integer

Dim doccount As Integer, prodcount As Integer

Dim prod List As Product  'user defined class

Dim endingdate As String, themonth As String

Dim blnUseEDate As Integer

Dim j As String



Set session = New NotesSession     

Set db = session.currentdatabase



'get StateApproval documents

searchString = { Form="StateApprovals" }



Set dc=db.search( searchString, Nothing, 0)

If dc.Count=0 Then Exit Sub



arr_States = Evaluate( { @DbColumn("":""; @DbName; "(States)"; 1) } )  'load all of th estates into the array



tmp=Evaluate( { @Adjust(@Date(@Year(@now);@Month(@now); @Day(@now);0;0;0); 0;0;-30;0;0;0) }) 'Beginning of last month

Set PMBegDate = New NotesDateTime( tmp(0) )

tmp=Evaluate( { @Adjust(@Date(@Year(@now);@Month(@now); @Day(@Now);0;0;0); 0;0;0;0;0;-1) }) 'Last second of last month

Set PMEndDate = New NotesDateTime( tmp(0) )



'Set up styles ... we like colors!

'1=Black unbolded text

Set rtStyle1=session.CreateRichTextStyle

rtStyle1.Bold=False

rtStyle1.NotesColor=0

'2=Green UnBolded Text

Set rtStyle2=session.CreateRichTextStyle

rtStyle2.Bold=False

rtStyle2.NotesColor=9

'3=Blue unbolded text

Set rtStyle3=session.CreateRichTextStyle

rtStyle3.Bold=False

rtStyle3.NotesColor=4

'4=Black bolded text

Set rtStyle4=session.CreateRichTextStyle

rtStyle4.Bold=True

rtStyle4.NotesColor=0

'5=Red bolded text

Set rtStyle5=session.CreateRichTextStyle

rtStyle5.Bold=True

rtStyle5.NotesColor=2

'6=Green Bolded Text

Set rtStyle6=session.CreateRichTextStyle

rtStyle6.Bold=True

rtStyle6.NotesColor=9



Set maildoc = New NotesDocument(db)

maildoc.form = "Memo"         

Set rtitem = New NotesRichTextItem( maildoc, "Body")

Call rtitem.AppendText("The following products were approved in the past week:" )



AnyProductApproved = False

doccount = -1

Set doc=dc.GetFirstDocument



While Not(doc Is Nothing)

	AnyStateApproved = False  'State Within this Product document

	For i=0 To Ubound(arr_States)

		Set item = doc.GetFirstItem( "Cdate_" & Cstr(i) )

		Set tempComment = doc.GetFirstItem("Comments_" & Cstr(i) )

		commentItem = Cstr(tempComment)										'<============ type mismatch here

		

		

		If item Is Nothing Then Goto nextState

		Set apprDate = item.DateTimeValue

		Redim Preserve arr_Comments(i)

		tempComment = Cstr(commentItem)

		arr_Comments(i) = tempComment

		

		If Not apprDate Is Nothing Then

			

			'We will use the Entered date to determine what gets captured in last week's approvals, if it exists.

			blnUseEDate = False				

			If (Not itemE Is Nothing) Then

				Set eDate = itemE.DateTimeValue

				If eDate Is Nothing Then 

					Set eDate = apprDate

				Else

					'There is a valid Entered Date

					blnUseEDate = True

				End If						

			Else

				Set eDate = apprDate

			End If

			

			If (eDate.LSLocalTime>=PMBegDate.LSLocalTime) And (eDate.LSLocalTime<=PMEndDate.LSLocalTime) Then

				If arr_PMStateAppr(0) <> "" Then 

					Redim Preserve arr_PMStateAppr( Ubound(arr_PMStateAppr) + 1)

					Redim Preserve arr_PMStateApprDates( Ubound(arr_PMStateAppr) + 1)

					Redim Preserve arr_comments(Ubound(arr_PMStateAppr) + 1)

				End If

				arr_PMStateAppr(Ubound(arr_PMStateAppr)) = arr_States(i)

				' But now we save the actual approval date, not the entered date

				If blnUseEDate = True Then

					arr_PMStateApprDates(Ubound(arr_PMStateAppr)) = apprDate.DateOnly & "  [Date Entered: " & eDate.DateOnly & "]"

				Else

					arr_PMStateApprDates(Ubound(arr_PMStateAppr)) = apprDate.DateOnly

				End If	

				AnyStateApproved = True

				AnyProductApproved = True					

			End If

		End If

nextState:

	Next

	If AnyStateApproved = False Then Goto SkiptoNextDoc		'No States Approved Last Week

	

'States were approved, so process this doc. Products are built into a list of prod objects which can then be sorted by Type, then listed.	

	doccount = doccount + 1

	

	Set prod(doccount) = New Product

	'set name

	If ( Trim(doc.Name(0)) <> "") Then

		prod(doccount).name = doc.Name(0)

	Else

		prod(doccount).name = doc.Name(0)		

	End If

	

	'set state approvals (states and dates)

	For i=0 To Ubound( arr_PMStateAppr )

		prod(doccount).newapprovalstate(i)= arr_PMStateApprDates( i )  & "  " & arr_PMStateAppr( i )

		prod(doccount).apprcomments(i) = arr_Comments(i)        ' get a type mismatch on this line

	Next

	'set date complete

	

	'set UNID

	prod(doccount).unid = doc.UniversalID

SkiptoNextDoc:

	Set doc = dc.GetNextDocument(doc)

	'Reset State Arrays		

	Redim arr_PMStateAppr(0)

	arr_PMStateAppr(0) = ""

	Redim arr_PMStateApprDates(0)

	arr_PMStateApprDates(0) = ""

	'reset comments field array

	Redim Preserve arr_Comments(0) As String

	arr_Comments(0) = ""

Wend



If AnyProductApproved = False Then Exit Sub  'No approvals



'Now sort - Get sorted list of types

Dim typelist

Redim typelist(0)

typelist(0) = ""

Dim ptype As String

Forall p In prod

	ptype = p.type

	Forall t In typelist

		If t = ptype Then Goto foundtype

	End Forall

	If typelist(0) <> "" Then Redim Preserve typelist( Ubound(typelist) + 1)

	typelist( Ubound(typelist) ) = ptype

foundtype:

End Forall

Call QuickSort( typelist )



'Now Categorize and print products by typelist

Forall thistype In typelist							'don't need to use Product Type for this list 10/31/06

	Call rtitem.AddNewLine( 2 )

	Forall thisprod In prod

		prodcount = prodcount + 1

		If thisprod.type<>thistype Then Goto nextprod

		Call rtitem.AppendStyle(rtStyle6)

		Call rtitem.AppendText( thisprod.name )

		Call rtitem.AddNewLine( 1 )

		

		Forall thisstate In thisprod.newapprovalstate

			Call rtitem.AppendStyle(rtStyle3)

			Call rtitem.AddTab( 1 )

			Call rtitem.AppendText(thisstate)

’

’ Forall thiscomment In thisprod.apprcomments

			Call rtitem.AddNewLine( 2 )

			Call rtitem.AddTab( 1 )

			Call rtitem.AppendStyle(rtStyle2)

			Call rtitem.AppendText( "Comments:  ")

			Call rtitem.AppendStyle(rtStyle3)

’ Call rtitem.AppendRTItem(apprcomments(i))

			Call rtitem.AddNewLine( 2 )

’ Call rtitem.AppendText(arr_Comments(i))

’ End Forall

		End Forall

		

		

		Call rtitem.AddNewLine( 1 )

		Call rtitem.AddTab( 1 )

		Call rtitem.AppendStyle(rtStyle1)

		Call rtitem.AppendText("Click on this link to access the document ==>")

		Set doc=db.GetDocumentByUNID( thisprod.unid )

		Call rtitem.AppendDocLink(doc,"Click on this link to view all of the State approvals for this product:  ")

		Call rtitem.AddNewLine( 2 )

		

		'< a href="URL" target="_blank">Click Here< /a>

nextprod:

	End Forall

End Forall



'Last month

yr=Cstr(Year(PMEndDate.LSLocalTime))

endingdate=PMEndDate.DateOnly

Select Case Month(PMEndDate.LSLocalTime)

Case 0 : themonth="December"

Case 1 : themonth="January"

Case 2 : themonth="February"

Case 3 : themonth="March"

Case 4 : themonth="April"

Case 5 : themonth="May"

Case 6 : themonth="June"

Case 7 : themonth="July"

Case 8 : themonth="August"

Case 9 : themonth="September"

Case 10 : themonth="October"

Case 11 : themonth="November"	

Case 12 : themonth="December"

End Select



With maildoc

	'Notification group		

’ .SendTo = “ProdDev State Approval Notification”

	.SendTo = "Mary Van Pelt"

’ .BlindCopyTo = “Steve Senneff”

’ If Instr(Lcase(db.server), “n01notes15”)>0 Then

	.Subject = "New State Approvals Summary for Week Ending " & endingdate  & " - Development Testing"

’ Else

’ .Subject = "New State Approvals Summary for Week Ending " & endingdate

’ End If

	.Month = themonth

	.MonthNumber = Cstr(Month(PMEndDate.LSLocalTime))

	.Year = Yr

	.EndingDate = endingdate

	.ProdCount = prodcount

End With

Call maildoc.send(False)

Call maildoc.save(False,False) 'will save this doc in db

End Sub

’ end of code

Subject: RE: how do you convert a variant to a string in lotus script?

Well, you are dimming tempComment as a Variant, but you then assign something to it using GetFirstItem, so it is really an item from the doc.

Set tempComment = doc.GetFirstItem(“Comments_” & Cstr(i) )

commentItem = Cstr(tempComment) '<============ type mismatch here

Try dimming tempComment as a NotesItem, then use the item.Text property to get the textual value from the item.

Subject: RE: how do you convert a variant to a string in lotus script?

Thank you for your help everyone.

This worked!

Subject: RE: how do you convert a variant to a string in lotus script?

Try

commentItem = Cstr(tempComment(0))

hth - regards