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)
Related
I am trying to use a VBA script to pull info from the emails in Inbox into an excel spreadsheet:
'Subject
'To Address
'From Address
'CC Addresses
it fails for senders who have already left the organization and they're no longer in O365.
This is the code bit:
Function X400toSMTP(strAdr As String) As String
Dim olkRcp As Outlook.Recipient, olkUsr As Outlook.ExchangeUser
Set olkRcp = Session.CreateRecipient(strAdr)
If olkRcp.AddressEntry = Empty Then
X400toSMTP = strAdr
ElseIf olkRcp.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then
olkRcp.Resolve
Set olkUsr = olkRcp.AddressEntry.GetExchangeUser
X400toSMTP = olkUsr.PrimarySmtpAddress
End If
Set olkRcp = Nothing
Set olkUsr = Nothing
End Function
I ran the debug and it stops at AddressEntry : The attempted operation failed. An object could not be found
I'm trying to find a way to make the script leave the address field empty for those senders who cannot be found on O365 anymore and further process the rest of the items in the Inbox.
I have tried the below:
If IsNull(olkRcp.AddressEntry) Then
X400toSMTP = strAdr
but am still getting the same error for AddressEntry.
I am just a VBA noob so would very much appreciate your advice.
Many thanks!
Make sure the recipient is resolved before accessing the AddressEntry property - call olkRcp.Resolve.
Am facing a problem while saving the mail as a text file through vb.net. i had done the following code as per my task:-
public sub rearmail()
Dim pop3Client As Pop3Client
If (Session("Pop3Client") Is Nothing) Then
pop3Client = New Pop3Client
pop3Client.Connect("pop.gmail.com", 995, True)
pop3Client.Authenticate("mymail#gmail.com", "password")
Session("Pop3Client") = pop3Client
Else
pop3Client = CType(Session("Pop3Client"), Pop3Client)
End If 'connect to the inbox with username and pass word authentication
Dim message As Message = pop3Client.GetMessage(0)
dim frm as string ' to store from address
dim subj as string ' to store the subject
dim mdate as date ' to store the date and time
frm=message.Headers.From.Address
subj= message.Headers.Subject
mdate=message.Headers.DateSent
'**** no i need to read the mail message and save it as a notepad file ****
end sub
i had tried a lot to read the mail using :-
Dim message As Message = pop3Client.GetMessage(0)
as follows:-
Dim str As MailMessageEventArgs
dim strmsg as string
strmsg=str..Message.Body.ToString 'error null error exception at run time
strmsg=message.MessagePart.Body ' nothing will return
like wise i do a lot with the message object but i fails to achieve the goal, i hope that the experts in this community can help me to overcome the problem,
thanks in advance.....♥
thanks for the one who make a try to solve the problem, i got the solution now i wish to share it with you. here is the code segment to read the mail body content;-
Dim messagePart As MessagePart = message.MessagePart.MessageParts(0)
MsgBox(messagePart.BodyEncoding.GetString(messagePart.Body))
I'm using vb.net to send email via .MSG file, I've tried with .OFT, both using createitemfromtemplate.
The emails will send, no problem. Works great. The only issue I have is anytime I restart, I have to resave the .msg or .oft files as the same file names in order for them to send again, otherwise it won't work anymore.
Any ideas as to why this is or how to fix this?
Example:
Dim omsg As Object
omsg = Outl.CreateItemfromtemplate("Custom Two.msg")
omsg.To = (TextBox1.Text)
omsg.Subject = (TextBox2.Text)
omsg.Display(False) 'will display message to user
Someone suggested adding the files into memory before the application loads to correct this.. but I'm not 100% sure how to do this, other than it goes in the load events.. Any ideas?
The answer I found to be easiest for this is:
Dim filelist() As String = Directory.GetFiles(Application.StartupPath)
For Each File In filelist
If File.Contains(".oft") Or File.Contains(".msg") Then
Dim temp1 As String = File.Replace(Application.StartupPath & "\", String.Empty)
If File.Contains(".oft") Then
ComboBox1.Items.Add(temp1)
ElseIf File.Contains(".msg") Then
ComboBox1.Items.Add(temp1)
End If
End If
Instead of trying to read them into a list one by one with names, linking them dynamically seems to fit better and execute without an issue.
I have a written a program which downloads a webpage's source but now I want to search the source for a particular link I know the link is written like this:
<b>Geographical Survey Work</b>
Is there anyway of using "Geographical Survey Work" as criteria to retrieve the link? The code I am using to download the source to a string is this:
Dim sourcecode As String = ((New Net.WebClient).DownloadString("http://examplesite.com"))
So just to clarify I want to type into an input box "Geographical Survey Work" for instance and "/internet/A2" to popup in a messagebox? I think it can be done using a regex, but that's a bit beyond me. Any help would be great.
With HTMLAgilityPack:
Dim vsPageHTML As String = "<html>... your webpage HTML code ...</html>"
Dim voHTMLDoc.LoadHtml(vsPageHTML) : vsPageHTML = ""
Dim vsURI As String = ""
Dim voNodes As HtmlAgilityPack.HtmlNodeCollection = voHTMLDoc.SelectNodes("//a[#href]")
If Not IsNothing(voNodes) Then
For Each voNode As HtmlAgilityPack.HtmlNode In voNodes
If voNode.innerHTML.toLower() = "<b>geographical survey work</b>" Then
vsURI = voNode.GetAttributeValue("href", "")
Exit For
End If
Next
End If
voNodes = Nothing : voHTMLDoc = Nothing
Do whatever you want with vsURI.
You might need to tweak the code a bit as I'm writing free-hand.
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.