How to add text to body of e-mail? - vba

I searched StackOverflow and others with no good results.
I have to send e-mails through Access VBA. I'm using Selenium Basic library.
I can, access the e-mail web page, click on the "new mail" button, fill fields "To", "subject", "Copy to" and "blind copy to" if such parameters are given, and click the "send e-mail" button.
I cannot, edit the rich text body of the e-mail. I can send e-mails, but they're blank.
Public Sub sendEmail( _
emailPage As String, _
sendTo As String, _
subject As String, _
emailText As String, _
Optional copyTo As String, _
Optional blindCopyTo As String)
'Parameters above: emailPage is the intranet, corporative e-mail site from which the e-mails must be
'sent; sendTo is the e-mail address to which the e-mail will be sent; subject is the subject of the
'e-mail; emailText is the text that I want to go in the body of the e-mail.
'Variables below: preparing variables to be used, like a webDriver. I tried to mingle Selenium and
'Windows HTML objects to no vail, as one can infer from the commented variable
Dim browser As WebDriver
Dim webEle As WebElement
Dim theKeys As selenium.Keys
Dim por As By
'Dim CurrentWindow As HTMLWindowProxy
'choosing the web browser driver. I chose firefox since it went farther than chrome*:
Set browser = New FirefoxDriver
'with the browser driver, I go to the e-mail webpage:
With browser
.Get emailPage
'Below I find the elements with the XPath to interact with
.SwitchToFrame "s_MainFrame"
.FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[2]/div[10]/table[1]/tbody/tr/td[2]/table/tbody/tr/td[1]/span").Click
.FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[5]/table[1]/tbody/tr[3]/td[3]/div/textarea").SendKeys sendTo
If IsMissing(copyTo) = False Then
.FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[5]/table[1]/tbody/tr[4]/td[3]/div/textarea").SendKeys copyTo
End If
If IsMissing(blindCopyTo) - False Then
.FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[5]/table[1]/tbody/tr[5]/td[3]/div/textarea").SendKeys blindCopyTo
End If
.FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[5]/table[1]/tbody/tr[6]/td[3]/div/textarea").SendKeys subject
'And here is the problem: I cannot access the e-mail body text to change it. SendKeys won't work. I also
'tried inserting HTML texts like <HTLM><BODY>email text goes here</BODY></HTML> and
'<HTLM><H1>email text goes here</BODY></HTML>, to no vail.
.FindElementByXPath("/html/body/div/div").SendKeys emailText
'.SwitchToFrame "s_MainFrame"
'After that, I click the button to send the e-mail, close the browser driver, and set it to nothing.
.FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[2]/div[14]/table[1]/tbody/tr/td[1]/span").Click
End With
browser.Close
Set browser = Nothing
End Sub
These are the elements that I get when inspecting the body text. In the first line of the e-mail body text, which is clean, the element is
<div><br></div>
And its XPath is:
/html/body/div/div
But using it as the path to access is of no use in my tests. Below is the rich text body text element main values:
<body role="main" id="e-$new-0-bodyrich-editor" class="s-mailbody-edit s-disable-inotesstyle">
And it's XPath:
/html/body
In Chrome, I get an error, and it doesn't open the email page. I have the Chrome 87 version and the appropriate driver.

Sorry to bother everyone!
I did some more testing and discovered I had to reference the XPath for the iframe:
.FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[2]/div/div/div[1]/iframe").SendKeys emailText
I also updated the function with a While DoEvents to close the browser driver only after the e-mail page writes in the referenced HTML element that the e-mail was sent succesfully:
Do While InStr(1, browser.FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[2]/div[1]/div/div[2]/div/table[1]/tbody/tr/td[2]/span").text, "Email sent successfully") = 0
DoEvents
Loop
Also, some declared variables were unused and were removed, like the webEle. Only the browser variable is declared in the final code.

Related

How to post image on facebook by using selenium chrome webdriver and vba?

I am very new to Selenium and I'm trying to create an application, which gets some information from an excel sheet, logs into a specific facebook page (not a person's profile), and creates a post which contains an image.
So far I've managed to log into the page account but I have no clue on how to upload a certain image due to the fact that it seems that the webdriver cannot interact with the filedialog that appears if I (programmatically) click on the "upload image" button. Also I cannot find any "upload" id or whatsoever.
Posting here the code I need help with:
' After having logged in, this creates a post
Dim ele As WebElement
Dim drv As New WebDriver
Set ele = drv.FindElementByXPath("//*[#aria-label='Crea un post']") ' This selects the "create a new post" button
ele.Click
Application.Wait (100)
If Not IsEmpty(Cells(24, 2)) Then ' Adds picture if there is one specified in the excel sheet
Set ele = drv.FindElementByXPath("//*[#aria-label='Foto/video']") ' This clicks the "add Picture" button, but then the FileDialog appears and I cannot interact with it
ele.Click
ele.SendKeys "C:\mytestimage.png"
End If
Set ele = drv.FindElementByXPath("//*[#aria-label='Scrivi qualcosa a mypage']") ' Italian for "write something to mypage"
ele.SendKeys "Whatever I want to be written in the post"

Trouble clicking on a load more button using vba

I'm trying to click on the Load More button located at the bottom of the left window of this webpage using vba in combination with selenium but the script always throws timeout error pointing at this .Get Url line. Although it seems I've defined an accurate xpath to locate the element, I can't think further as to what I should do now to achieve the same.
How can I click on that Load More button?
Sub ClickOnLoadMore()
Const Url$ = "http://www.ratemyprofessors.com/search.jsp?queryoption=TEACHER&queryBy=schoolDetails&schoolID=457&schoolName=James+Madison+University&dept=select"
Dim driver As New ChromeDriver, post As Object
With driver
.Get Url
Set post = .FindElementByXPath("//div[contains(.,'Load More')]")
.ExecuteScript "arguments[0].scrollIntoView();", post
post.Click
End With
End Sub
I see two "Load More" buttons. Both are matched by "//div[contains(.,'Load More')]". The first one is hidden. You need to handle second one.
Try this XPath
"//div[#class='content' and . = 'Load More']"
At least for me there were a couple of banners to dismiss as well as scrolling. There was no problem with the get line
Option Explicit
Public Sub ClickOnLoadMore()
Const Url$ = "http://www.ratemyprofessors.com/search.jsp?queryoption=TEACHER&queryBy=schoolDetails&schoolID=457&schoolName=James+Madison+University&dept=select"
Dim driver As New ChromeDriver, post As Object
With driver
.get Url
If .FindElementsByCss(".close-notice.close-this").Count > 0 Then
.FindElementByCss(".close-notice.close-this").Click
End If
.SwitchToFrame .FindElementByCss("[id^='spout-unit-iframe']")
With .FindElementByCss("#spout-ads #spout-header-close")
.ScrollIntoView
.Click
End With
.SwitchToDefaultContent
.ExecuteScript "document.querySelector('.result-list [onclick*=LoadMore]').scrollIntoView(true);" & _
"window.scrollBy(0, -(window.innerHeight - this.clientHeight) / 2);"
.FindElementByCss(".result-list [onclick*=LoadMore]").Click
Stop '<== Delete me later
'other code
.Quit
End With
End Sub

Open an url in a certain outlook mail

I am writing a vba script to open a particular mail from outlook and then open a link in that mail.
So far I have only achieved the first part. How do I get vba to test which part of the body is a hyper link and to open it?
Dim stringsearch As String
stringsearch = oMsg.Body
So now the searchstring contains the body text of the mail.
I know that the link will always begin with the phrase "SearchID" and will always be the last line of the mail.
Any help/suggestions/links are very appreciated.
This will find the "SearchId" within the stringsearch and extract everything after it into "link".
dim linkLoc as Integer
linkLoc = instr(1, stringsearch, "SearchID")
dim link as string
link = mid(stringsearch, linkLoc + 8)

VBA Code to change font and size in an email from access

I have an access form that runs a query. There is a command button on the form that I want to send an email with the query results attached in an excel spreadsheet. Using the code below I am able to attach the results and send the email ok. I would like to be able to format the email body so that it is more noticeable. I'm not really sure how to go about doing this and still have my query attached. I have also created an email template that I would use but I haven't been able to figure out how to use the template and attach the query results. I'm open for any suggestions. Any help would be greatly appreciated.
DoCmd.SendObject acQuery, "BoxOrder", "ExcelWorkbook(*.xlsx)", "me#home.com", _
"John#hishome.com", "", "BOX ORDER", _
"ALL BOXES STITCHED" & vbCrLf & "Questions: Please Call Me" & _
vbCrLf & "555-555-5555 x 66654", True, True
You could follow the Article from MS.
A proportion of the code is as follows:
Set ola1 = New Outlook.Application
Set mai1 = ola1.CreateItem(olMailItem)
mai1.To = strTo
mai1.Subject = strSubj
If bolHTML = True Then
mai1.HTMLBody = strBody
Else
mai1.Body = strBody
End If
mai1.Display
If you use the HTML (set bolHTML = True) version you can either have an RTF control on a Form and pass the formatted text over or hardcode your HTML with the formatting you need. Just set the "strBody" to the message you want.
Then you need to look into the Attachments.Add (MS Article) if you want to use the above code with your original purpose.
There's a full 599CD Email Seminar you could follow if you're going to be doing a lot with Email in Access.

How to convert the body of an email into a .pdf

I have to convert an email into .pdf without the head which includes the information about the date, the receivers, cc, etc.
Does anyone how to do that the easy way?
My other solution would be to copy the whole body of the mail into a new word-document and save it as a .pdf, but I don't know how to copy the whole body via VBA either.
[EDIT JMax from comments]
Here is the code I've tried:
sBody = oMail.HTMLBody
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
wrdApp.Documents.Add "C:\asd\Releasemail.dotx"
wrdApp.Documents("Dokument1").Bookmarks().Item("Releaseinhalt").Range.Text = sBody
I get my whole HTML printed in the .doc, but I want the body of the mail as it's shown in Outlook, not the markup, that creates that look. For example, if I press Ctrl + a and Ctrl + c in Outlook and press Ctrl + v in Word, I get the text with all its styling copied to Word.
How to do that in VBA?
When you want to get only the body of a mail, you have to use this kind of statement:
Dim Msg As Outlook.MailItem
Body = Msg.HTMLBody
You can find another example on this blog and on VBA Express
Have a try and come back when you will have an issue on some code.
[EDIT]
To get the body content instead of HTML, you can use : Msg.Body but you will then probably loose the formatting of the message.