Lotusscripts Get rich text field and send Email - email

I try to coding is sending Rich text field via email , but I find an error that's I think this method for sending email, by following code
Sub Click(Source As Button)
Dim s As New NotesSession
Dim w As New NotesUIWorkspace
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim uidoc As NotesUIDocument
Set uidoc = w.CurrentDocument
Set s = New NotesSession
Set w = New NotesUIWorkspace
Set db = s.CurrentDatabase
Set doc = New NotesDocument (db)
doc.sendTo =s.UserName
doc.Subject = "Employee Information"
Dim rt As NotesRichTextItem
Set rt = New NotesRichTextItem ( doc, "Body" )
'Dim file As Variant 'if I use this code for declare for get value; Error : Type Mismatch
'Set file = doc.GetFirstItem("Body")
Dim rtitem As NotesRichTextItem 'if I use this code for declare for get value ; Error : Missing text object
Set rtitem = doc.GetFirstItem( "Body" )
Call rt.AppendRTItem(rtitem)
doc.Send(False)
End Sub

One thing I notice is that you don't set the form on the mail document you are creating.
You have some code you don't need, since you don't use uidoc anywhere, no need for that or declaring a NotesUIWorkspace object.
I also recommend that you use better variable names, and not to use extended notation when you set field values in a NotesDocument object.
I suggest that you take a look at the articles here:
http://blog.texasswede.com/how-to-write-better-code-in-notesdomino/
Below is the code that I cleaned up:
Option Public
Option Declare
Sub Click(Source As Button)
Dim session As New NotesSession
Dim ws As New NotesUIWorkspace
Dim db As NotesDatabase
Dim mailDoc As NotesDocument
Dim mailBody As NotesRichTextItem
Set db = session.CurrentDatabase
Set mailDoc = New NotesDocument(db)
Call mailDoc.ReplaceItemValue("Form","Memo")
Call mailDoc.ReplaceItemValue("SendTo",session.UserName)
Call mailDoc.ReplaceItemValue("Subject","Employee Information")
Set mailBody = New NotesRichTextItem(mailDoc,"Body" )
'Dim file As Variant 'if I use this code for declare for get value; Error : Type Mismatch
'Set file = doc.GetFirstItem("Body")
Dim rtitem As NotesRichTextItem 'if I use this code for declare for get value ; Error : Missing text object
Set rtitem = doc.GetFirstItem("Body")
Call mailBody.AppendRTItem(rtitem)
Call mailDoc.Send(False)
End Sub
The big question here is where you are getting the rich text field you want to send from? In your original code you are trying to read it from the newly created document (the one I call mailDoc). But that does not make any sense.
Your problem is simply that you are not reading the rich text from anywhere.
If your goal is to send an email, you can use my mail notification class:
http://blog.texasswede.com/updated-mailnotification-class-now-with-html-email-support-and-web-links/
Then your code would look something like this:
Dim session As New NotesSession
Dim mail As NotesMail
' *** Create a mail
Set mail = New NotesMail()
' Set receipient and subject
mail.MailTo = session.CommonUsername
mail.Subject = "Employee Information"
mail.Principal = "noreply#example.com"
' Create body content from rtitem.
' Yes, I should have added a method in the
' class to append RichtText to the mail body...
mail.body.AppendRTItem(rtitem)
Call mail.Send()
The only thing you have to do is to get the rtitem from somewhere. Since your original code declared a NotesUIWorkSpace object and a NotesUIDocument object, I am guessing you want to read it from the currently open document. Then you just add the following to the beginning of the code:
Dim ws As New NotesUIWorkspace
Dim thisdoc As NotesDocument
Dim rtitem as NotesRichTextItem
Set thisdoc = ws.CurrentDocument.Document
Set rtitem = thisdoc.GetFirstItem("Body")
Do you also see how much easier it is to read when you use descriptive variable names?

Hi you did not saved the document. Please be aware the Richtext is not available if the document is not saved.

Related

How to email multiple doclinks of those who are my direct reports in the view?

I have a question. For example there is one view and 10 documents are in that view. Out of all those documents, 8 of them I should be the recipient of the email (based on the field value which is my email address).
Now, what I want to happen is that I will be receiving only one email for all those 8 documents, and in that email I there will be 8 doclinks.
Is that possible?
'Cause currently I am getting 8 emails and for each email, there is one doclink. Thanks in advance for those who can help me.
Dim s As NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim rtitem As NotesRichTextItem
Dim i As Integer
Dim view As NotesView
Set s = New NotesSession
Set db = s.CurrentDatabase
Set view = db.GetView("View")
Set doc = New NotesDocument(db)
Dim addresses As NotesName
i=0
Set doc = view.GetFirstDocument
While Not(doc Is Nothing)
Set addresses = New NotesName(doc.Manager(0))
If addresses.abbreviated = "" Then
i = i + 1
Else
doc.SendTo = addresses.abbreviated
doc.Form = "Memo"
Set rtitem = New NotesRichTextItem(doc, "Body")
Call rtitem.AppendText("Balance")
Call rtitem.appenddoclink(doc, "Link")
doc.Send (True)
i = i + 1
End If
Set doc = view.GetNextDocument(doc)
Wend
As Thorsten said, it can be done. There are a couple of ways to handle this, depending on how flexible and future proof you want it, and how "clean" of a solution you want.
Let's say you have 10 documents, 8 with your email and 2 with a different user's email. I assume you want to send one mail to you with 8 doc links and one to the other person with 2 doc links.
The way I would do it is to create a class. That class would contain a list of NotesDocuments (and a method to add documents to the list):
Class DocData
Public docs List As NotesDocument
Public Sub New()
End Sub
Public Sub Add(doc as NotesDocument)
Set docs(doc.UniversalID) = doc
End Sub
End Class
In your main code, you have a list of DocData objects, one per mail recipient:
Dim docs List As DocData
You now loop through the view or the document collection you have. You check the email address for each document and if there isn't a list item for that address, you create it and add the document to it. If it already exists, just add the document:
email = doc.GetItemValue("EmailAddress")(0)
If !IsElement(docs(email)) Then
Set docs(email) = New DocData()
End If
Call docs(email).Add(doc)
When all documents have been processed, you should have a list with one item per mail recipient, and you can loop through the list, build one email per item and populate it with doc links for all the documents in the list in the object.
For performance reasons, if you plan to have this working on larger views with more documents, I woudl suggest that you put the email address in one of the columns, and then use view entries to loop though the documents, and ColumnValues() to read the email address.
I wrote about it here: http://blog.texasswede.com/re-using-lotusscript-lists-to-increase-performance/
Your code can be slightly modified to only send one mail:
Dim s As NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim rtitem As NotesRichTextItem
Dim i As Integer
Dim view As NotesView
Set s = New NotesSession
Set db = s.CurrentDatabase
Set view = db.GetView("View")
Set doc = New NotesDocument(db)
Dim addresses As NotesName
i=0
'- prepare mail
doc.Form = "Memo"
Set rtitem = New NotesRichTextItem(doc, "Body")
Call rtitem.AppendText("Balance")
Set doc = view.GetFirstDocument
While Not(doc Is Nothing)
Set addresses = New NotesName(doc.Manager(0))
If addresses.abbreviated = "" Then
i = i + 1
Else
'- Set recipient
If not doc.HasItem( "SendTo" ) then
doc.SendTo = addresses.abbreviated
End If
'- Append descriptive text, link and new line
Call rtitem.appendtext(doc.Subject(0) & " " )
Call rtitem.appenddoclink(doc, "Link")
Call rtitem.addnewline(1)
i = i + 1
End If
Set doc = view.GetNextDocument(doc)
Wend
'- send mail
Call doc.Send (True)
with that code the mail is first prepared, then a doclink is added for every document, and in the end the mail is sent.

How to generate email from Access form using contents of textbox

I have a main form with a 'help' button which opens a simple form with a textbox that a user can use to submit issues noted with the main form. I would like the contents of what the user types into the textbox to be emailed to myself and a co-worker using a 'send' button.
I found the following code on stackoverflow which works except I can't figure out how to have the body of the email include what the user types into the textbox instead of the static text that's currently in the code.
Here's how the code looks now:
Private Sub SendEmail_Click()
Dim olApp As Object
Dim objMail As Object
Dim Issue As String
strIssue = Me.ContactMessageBox
On Error Resume Next 'Keep going if there is an error
Set olApp = GetObject(, "Outlook.Application") 'See if Outlook is open
If Err Then 'Outlook is not open
Set olApp = CreateObject("Outlook.Application") 'Create a new instance
End If
'Create e-mail item
Set objMail = olApp.CreateItem(olMailItem)
With objMail
.To = "emailaddress.com"
.Subject = "Form issue"
.Body = "strIssue"
.send
End With
MsgBox "Operation completed successfully"
End Sub
Does anyone have ideas about how to do this?
Change
Dim Issue As String
strIssue = Me.ContactMessageBox
...
.Body = "strIssue"
to
Dim strIssue As String
strIssue = Me.ContactMessageBox
...
.Body = strIssue
If you put your variable between "" then it is read as a string instead of the variable.

sending outlook based email in VBScript - error (sfile as string) line?

im trying to send an email from a VBScript, it will eventually be added into a currently working script as an else if (if this is possible).
im getting an error at line 23 character 32?
Dim outobj, mailobj
Dim strFileText
Dim objFileToRead
Set outobj = CreateObject("Outlook.Application")
Set mailobj = outobj.CreateItem(0)
strFileText = GetText("C:\test\test 2.txt")
With mailobj
.To = "user#user.com"
.Subject = "Testmail"
.Body = strFileText
.Display
End With
Set outobj = Nothing
Set mailobj = Nothing
End Sub
Function GetText(sFile as String) As String
Dim nSourceFile As Integer, sText As String
nSourceFile = FreeFile
Open sFile For Input As #nSourceFile
sText = Input$(LOF(1), 1)
Close
GetText = sText
End Function
what do i need to add to get line 23 to work and the script to finally do what i need it to, i have copied most of this script from elsewhere due to a sincere lack of VBscripting knowledge?
Take a look at the Using Automation to Send a Microsoft Outlook Message article. It provides a sample code and describes all the required steps for sending emails.
Try this: remove the GetText function entirely, and replace the line
strFileText = GetText("C:\test\test 2.txt")
with
Set fso = CreateObject("Scripting.FileSystemObject")
strFileText = fso.OpenTextFile("C:\test\test 2.txt").ReadAll

Access VBA visible control form from another form

I have a table and form setup to control another form in my database.
I'm wanting to make a code that will take the title from my field and add it to my code as a variable to change the visibility options of my other form.
my form is set with all the of the names to all objects on the form I want to control.
LSE_FORM_ADMIN = The table with all the LSE_FORM_ALL names in it.
Table is setup with 3 columns key, names and a checkbox which I put into a form to make a continuous list.
here is my code on the form, but I keep getting and runtime 424: object required error:
Private Sub Form_Current()
Dim VARSET As Object
Dim VAR As String
VARSET = DLookup("TITLE", Table!LSE_FORM_ADMIN, "") 'keep getting error here
VAR = VARSET
If Me!CB = "-1" Then
Form_LSE_FORM_ALL!VAR.Visible = True
Else
Form_LSE_FORM_ALL!VAR.Visible = False
End If
End Sub
can someone help me fix this code so that it will grab the title field data and make it a variable to add to the rest of the code?
It's difficult to see exactly what you are trying to achieve, but your problems stem from using the variant variable type when you should be using an explicit Form or Control type. Using your last example.
RSTT.Visible = True 'getting Run-time error '424': object required
This is because you have declared RSTT as a variant. The line
RSTT = "Form_LSE_FORM_ALL" & "!" & (RST)
results in the variable RSTT containing a string, which does not have a property ".Visible"
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
These lines are redundant as you have the values that you need available on the form fields which are already bound to the table LSE_FORM_ADMIN.
As far as I understand, you have a continuous form (ADMIN?) bound to the table LSE_FORM_Admin. As you step through the records on this form, you want code to be fired which takes the value of the TITLE field/control and use it to set a control with the same name, on a separate form, Form_LSE_FORM_ALL, to be (in)visible, dependent on the value of the checkbox control name CB on the ADMIN form?
If you want the ADMIN form to make the changes "live" to the ALL form, you should consider using an event of the CB checkbox control. Using the current event of the form means that the changes you make will not be reflected in the ALL form until you step out of the record you have just edited, then back in, to fire the form's Current event on that record.
Example using AfterUpdate event of CB checkbox
Private Sub CB_AfterUpdate()
Dim strRST As String
Dim frmTarget as Form
Dim ctlRSTT As Control
Set strRST = Me!TITLE
Set frmTarget = Forms("Form_LSE_FORM_ALL")
Set ctlRSTT = frmTarget.Controls(strRST)
ctlRSTT.Visible = Me!CB 'getting Run-time error '424': object required
End Sub
really not sure how to do the syntax when doing a recordset to a table from the form, need some help with that.
here is my code and attempt at the record set:
Private Sub Form_Current()
Dim DB As Database
Dim RS As Recordset
Dim RST As String
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
Set RST = RS 'GETTING OBJECT REQUIRED ERROR ON "RST ="
Do Until RS.EOF
RST = Me!TITLE
RS.MoveNext
Loop
If Me!CB = "-1" Then
Form_LSE_FORM_ALL!RS.Visible = True
Else
Form_LSE_FORM_ALL!RS.Visible = False
End If
End Sub
I think I know what you are trying to do, but your descriptions / references are not matching up. Please look at the following comments and clarify:
1. You say "...make a code that will take the title from my field and ..." but your code is taking "Me.Title", "ME" is a reference to the Form - not a field.
2. Your code is in the "Form_Current" event, which means it will fire for every record you process. That will work, but I think you want to do this code only once to be more efficient.
3. You have no provision for processing more than one field. I think you need to loop through all fields in your table, setting visible to true or false.
The following is my suggestion, but I will update once you clarify the issues.
Option Compare Database
Option Explicit
Dim DB As DAO.Database
Dim RS As DAO.Recordset
'Dim RST As Variant
'Dim RSTT As Variant
Public Sub FORM_CURRENT()
Set DB = CurrentDb
Set RS = DB.OpenRecordset("LSE_FORM_ADMIN")
Do While Not RS.EOF ' Loop thru all field names for the form
If RS!HideYN = True Then ' Desire to hide the field?
Me(RS!ctlname).Visible = False ' Yes, hide the field.
Else
Me(RS!ctlname).Visible = True ' No, show the field
End If
RS.MoveNext ' Get next field name
Loop
RS.Close
Set RS = Nothing
Set DB = Nothing
'Set RST = Me!Title
'RSTT = "Form_LSE_FORM_ALL" & "!" & (RST)
'If Me!CB = "-1" Then
' RSTT.Visible = True 'getting Run-time error '424': object required
'Else
' RSTT.Visible = False
'End If
End Sub
Final code, thanks to Cheesenbranston.
Private Sub Form_AfterUpdate()
Dim strRST As String
Dim frmTarget As Form
Dim ctlRSTT As Control
strRST = Me!TITLE
Set frmTarget = Forms("LSE_FORM_ALL")
Set ctlRSTT = frmTarget.Controls(strRST)
If Me!CB = "-1" Then
ctlRSTT.Visible = True
Else
ctlRSTT.Visible = False
End If
End Sub
#Cheesenbranston: Your original code was more like a toggle of on and off so if my object was not visible then my trigger checkbox would make it visible when checked, more of a quality of life for my own needs, none the less worked. Also strRST doesn't need SET since its just a String. Thanks again =D very happy day!

Lotus Notes 7 - copy / move docs. ( parent & response docs ) without changing the UNID ?

I have 2 databases : let say dbA and dbB. Actually, dbB is a ''child'' database of dbA, because the forms/views/frameset/etc that it contains they all are also in dbA.
I want now, to copy from a view ( let say vwA ) from dbA some 8K docs to the same view ( vwA ) from dbB. THese 8k contains both parent and child docs, which in dbA are listing OK, with #Text(#UniqueDocumentID). I just made a test, copy one parent doc and its response, and pasted in the 2nd database, but unfortunately the connection between the 2 docs isn't made... I guess the UNID had changed...
Is there any solutions? Thanks for your time.
Yes, copying a document to another database always creates a new UniversalIDs for document in target database.
To avoid this, your LotusScript should work like this:
create new document in target database
CopyAllItems from source document to target document
set same UniversalID to target document targetDoc.UniversalID = sourceDoc.UniversalID
save the target document
This way the target document has the same UniversalID like the source document and links between document should work in target database too.
This is an example for an agent working on selected documents:
Dim session As New NotesSession
Dim dbSource As NotesDatabase
Dim dbTarget As NotesDatabase
Dim col As NotesDocumentCollection
Dim docSource As NotesDocument
Dim docTarget As NotesDocument
Set dbSource = session.Currentdatabase
Set dbTarget = session.Getdatabase(dbSource.Server, "YourTargetDatabase.nsf", false)
Set col = dbSource.Unprocesseddocuments
Set docSource = col.Getfirstdocument()
While Not docSource Is Nothing
Set docTarget = dbTarget.Createdocument()
Call docSource.Copyallitems(docTarget, true)
docTarget.UniversalID = docSource.UniversalID
Call docTarget.save(True, False)
Set docSource = col.Getnextdocument(docSource)
Wend
Or, you could let replication handle this for you by setting a replication formula, assuming that dbA and dbB are replicas.
I think the easiest way is creating a script and copiing the documents using the CopyToDatabase method of NotesDocument class.
The CopyToDatabase method retains the UniversalID of documents (for perfomance reasons). This is true at least for Versions up to R7.
More information can be found here:
IBM Technote: Documents copied using CopyToDatabase method reuse same UNID
A sample Script (copied and modified from Knut) would be:
Dim session As New NotesSession
Dim dbSource As NotesDatabase
Dim dbTarget As NotesDatabase
Dim col As NotesDocumentCollection
Dim docSource As NotesDocument
Dim docTarget As NotesDocument
Set dbSource = session.Currentdatabase
Set dbTarget = session.Getdatabase(dbSource.Server, "YourTargetDatabase.nsf", false)
Set col = dbSource.Unprocesseddocuments
Set docSource = col.Getfirstdocument()
While Not docSource Is Nothing
Set docTarget = docSource.Copytodatabase(dbTarget)
Set docSource = col.Getnextdocument(docSource)
Wend