How to count attachments before save document in lotusscript? - lotus-notes

I would like to check whether attach an attachment in RichText field before save(it is a new document) the form in lotusscript. There are a RichText field naming "Attachs" and a button to run the below code. It shows "Please attach an attachment" when I click the button without attaching any attachment, and then it is still shows the same message when I have attached it. How can I solve this problem? Thanks a lot.
If uidoc.Document.Attachs(0) = "" Then
Msgbox "Please attach an attachment",,"Attention"
Else
Msgbox "Pass, you have attached an attachment"
End If

Unfortunately you cannot access attachments in a NotesRichtextItem with LotusScript before a document is saved.
BUT: Formula language "#AttachmentNames" can. You can leverage that:
Dim attachNames as Variant
attachNames = Evaluate( "#AttachmentName", uidoc.Document )
If attachNames(0) = "" then
Msgbox "Please attach an attachment",,"Attention"
Else
Msgbox "Pass, you have attached an attachment"
End If

Related

How to send multiple hyperlinks with mail in Excel VBA, depending on the cell value

There is and light order managment system where you can order a few items. When you have chosen your order, you click a button, and the rows that is order and number of items is copied to a new confirmation sheet. This sheet is then supposed to be sent to a chosen reciever by oMail. This works fine.
But in one of the columns there is a certificate on PDF(hyperlink), that is linked to a server(local file on my computer:))
I'm trying to send this PDF's as multiple hyperlinks in the mail, but that is no sucess :p. The thing I want to do is check if the cell is empty, if not, attach the hyperlink(s) in my stringbody. And send the mail.
Here is my code:
I use a function also to make the range to html format:
It's more useful to edit e-mail body via GetInstector
For example:
Dim myInspector As Object'inspector object
Dim wdDoc As Object''document
Dim myitem As Object'mailitem
Set myitem = CreateObject("Outlook.Application").CreateItem(0)'creating mailitem from Excel
Set myInspector = myitem.GetInspector'set inspector to edit mailitem
Set wdDoc = myInspector.WordEditor'set MS Word to edit mailbody
With myitem'here you are setting the e-mail properties
.To = Email'fill to e-mail
.Subject = Subja'Subject
.Display'display mailitem etc.
End With
'here edit the body with MS Word methods
wdDoc.Range.Paragraphs.Add
wdDoc.Range.Paragraphs.item(1).Range.Text = "Something you want in 1st paragraph" & vbCr
wdDoc.Range.Paragraphs.Add
wdDoc.Range.Paragraphs.item(2).Range.Text = "Something else you want in 2nd paragraph " & vbCr
So, actually you edit e-mail programmatically the same as you do it in Word. It makes useless long and complicated string with HTML-tags. You add MS Word objects and forms.

Passing String From Excel to Word

I am trying to pass a string from Excel userform using VBA to a Word document. In the Word document I have created a field>doc variable and called it bookingRef. The code is as follows:
Dim objWord As New Word.Application
Dim doc As Word.Document
Dim bkmk As Word.Bookmark
Set doc = objWord.Documents.Open("test.docx")
objWord.ActiveDocument.variables("bookingRef").Value = Me.txtRef.Text
objWord.ActiveDocument.Fields.Update
objWord.Documents.Save
It doesn't have any errors, however when I open the document up, I have to right click and update field (I thought objWord.ActiveDocument.Fields.Update did this?). Also, it keeps locking the document so it cannot be opened again. Is there a way to unlock after save?
The document is locked because you didn't close it with the Document.Close method, so the document is still opened and therefore cannot be opened again.
Also avoid using ActiveDocument the document that was opened is set to doc
Set doc = objWord.Documents.Open("test.docx")
and can therefore be referenced with doc.
Dim objWord As New Word.Application
Dim doc As Word.Document
'Dim bkmk As Word.Bookmark
Set doc = objWord.Documents.Open("test.docx")
doc.variables("bookingRef").Value = Me.txtRef.Text
doc.Fields.Update
doc.Save
doc.Close
Also don't forget to quit your Word application after you are done.
objWord.Quit
Otherwise the instance of Word will be open until you shut down your computer.
The Fields.Update method should update the fields, but it might be unsuccessful because of an error. Check it for errors:
If doc.Fields.Update = 0 Then
MsgBox "Update Successful"
Else
MsgBox "Field " & doc.Fields.Update & " has an error"
End If
What I did (a test according comments below this answer):
(following steps according How to store and retrieve variables in Word documents)
Created a file C:\Temp\test.docx
To use the DocVariable field, follow these steps:
On the Insert menu, click Field.
In the Categories box, select Document Automation.
In the Field names list, select DocVariable.
In the New Name box, under Field properties, type the name of the document variable bookingRef.
Click OK.
Note you will see nothing in the document yet but that's ok because the variable bookingRef does not exist yet.
Save file and close Word.
Run the following code in Excel
Option Explicit
Public Sub Test()
Dim objWord As New Word.Application
On Error GoTo CLOSE_WORD_APP 'error handling to ensure there will not be any orphaned and invisible Word application left
Dim doc As Word.Document
Set doc = objWord.Documents.Open("C:\Temp\test.docx")
doc.Variables("bookingRef").Value = "This is the updated Value: " & Time
doc.Fields.Update
doc.Save
doc.Close
CLOSE_WORD_APP:
objWord.Quit SaveChanges:=False
If Err.Number <> 0 Then
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End If
End Sub
Open the Word document C:\Temp\test.docx and see that everything is updated:

How to take text from a cell and input it into VBA?

What I am trying to do is basically you click a button, it brings up the Excel MailEnvelope to send an email, and you can then send it off to the relevant button email address'.
However one of the email addresses needs to be modifiable by the end user.
So I want a drop down where you select said email, and it then inputs that into the VBA code.
I know basically nothing about VBA and I could not find a way of doing this by searching around the web.
I figured I need some way of setting a variable to read a cell (the drop down cell), and then input that into the MailEnvelope Item.CC but I was struggling.
Any help would be appreciated.
This is what I have so far;
Sub Send_Range_Email()
' Select the range of cells on the active worksheet.
ActiveSheet.Range("B6:D302").Select
' Show the envelope on the ActiveWorkbook.
ActiveWorkbook.EnvelopeVisible = True
' Set the optional introduction field thats adds
' some header text to the email body. It also sets
' the To, CC and Subject lines.
With ActiveSheet.MailEnvelope
.Introduction = ""
.Item.To = "Email 0"
.Item.Subject = "Email Tracker Results"
.Item.CC = "Email 1" & text input here & "Email 2"
End With
End Sub
When using formulas, if you want to put a variable in there, just break it apart and add in the variable. As commented,
.Item.CC = "email 1" & "," & Range("A1").Value & ", " & "Email 2"
So to make super clear, say we want to add A1's value in this string: str = The man lives in STATE all the time by doing str = "The man lives in " & Range("A1").Value & " all the time"

How to insert hyperlink into email body

I created a macro in Excel to send emails to various users every time a specific file is updated.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim answer As String
answer = MsgBox("Would you like to save the changes?", vbYesNo, "Save Document")
If answer = vbNo Then Cancel = True
If answer = vbYes Then
'open outlook type stuff
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'add recipients
'newmsg.Recipients.Add ("Name1")
newmsg.Recipients.Add ("email#xxx.com")
'newmsg.Recipients.Add ("Name2")
newmsg.Recipients.Add ("email#xxx.com")
'add subject
newmsg.Subject = "Notification - Update file"
'add body
newmsg.Body = "This is an automated notification." & vbNewLine & vbNewLine & _
"The XXX file has been recently updated" & vbNewLine & vbNewLine & _
"Please do not reply to this email."
newmsg.Display 'display
newmsg.Send 'send message
'give conformation of sent message
MsgBox "Your document has successfully been saved", , "Confirmation"
End If
'save the document
'Me.Worksheets.Save
End Sub
I would like to add a hyperlink to the body text where it says "The XXX file has been recently updated" so that XXX file is a clickable link to a website.
The Outlook object model supports three main ways of customizing the message body:
The Body property returns or sets a string representing the clear-text body of the Outlook item.
The HTMLBody property of the MailItem class returns or sets a string representing the HTML body of the specified item. Setting the HTMLBody property will always update the Body property immediately. For example:
Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties.
Dim objMail As Outlook.MailItem
'Create e-mail item
Set objMail = Application.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><BODY>Enter the message text here. </BODY></HTML>"
.Display
End With
End Sub
The Word object model can be used for dealing with message bodies. See Chapter 17: Working with Item Bodies for more information.
Note, the MailItem.BodyFormat property allows you to programmatically change the editor that is used for the body of an item.
The last two supports creating a hyperlink in the message body. It is up to you which way is to choose.
If you want to do that, you'll have to write HTML instead of plain text.
This line:
newmsg.Body = "The XXX file has been recently updated"
... would become something like:
newMsg.HTMLBody = "The XXX file has been recently updated".
This is because in Outlook emails with formatting you write HTML text, and a link in HTML is expressed as follows:
your Hyper-text

Excel macro send rich mail using LotusNotes

I'm working on a small macro to send mail from excel 2007 using my Lotus Notes session.
The sending mail part is working fine.
Now I need to send in the body part, a part of a stylesheet (for instance the area from A1:B20). This area has colors, bold font.
To send my email here is the code:
Set oSess = CreateObject("Notes.NotesSession")
Set oDB = oSess.GETDATABASE("", "")
Call oDB.OPENMAIL
flag = True
If Not (oDB.IsOpen) Then flag = oDB.Open("", "")
If Not flag Then
MsgBox "Can't open mail file: " & oDB.SERVER & " " & oDB.FILEPATH
End If
On Error GoTo err_handler
'Building Message
Set oDoc = oDB.CREATEDOCUMENT
Set oItem = oDoc.CREATERICHTEXTITEM("BODY")
oDoc.Form = "Memo"
'mail subject
oDoc.Subject = "subject"
'mail body
oDoc.sendto = "toto#myserver.com"
oDoc.body = "my text"
oDoc.postdate = Date
oDoc.SaveMessageOnSend = True
oDoc.visable = True
'Sending Message
oDoc.SEND False
Does anybody has an idea about how to send a stylesheet ?
You can try the solution described in this SO question : Sending formatted Lotus Notes rich text email from Excel VBA
Regards,
Max

Resources