How to copy mailItem.Body with line breaks to a website Textarea? - vba

I'm using objItem.Body to fill in a website text area.
With HTML emails it adds spaces for line breaks or something, how do I fix that? Here is an example e-mail.
Using ie.document.getElementById("message").Value = objItem.Body you can see that two extra spaces/returns are added. It also adds HYPERLINK="mailto:xxxxx".
Converting to plain text with something like:
objitem.BodyFormat = olFormatPlain
objItem.Body = Replace(objItem.Body, " " & vbCrLf, vbCr)
retains the extra spacings and ruins the original email (I want to keep screenshots etc).
How can I get the mailItem.Body to transfer correctly?

The only thing left is removing Hyperlinks! IE HYPERLINg "mailto:email"email and HYPERLINK"website"website
This is how to get the objitem.body to do correct spacings on html emails..
ie.document.getElementById("message").Value = Replace(objItem.Body, vbCrLf & vbCrLf, vbCrLf)

Related

Minify HTML code into one line

My program is generating HTML code, which is placed afterwards into some string variable. This HTML code is ready to be placed in CSV file, so the entire code is surrounded by quotes, as well as all inner double quotes have additional quotes for escape. The result is the user can see it nicely formatted in output.
However, I have to convert this code to one line as assuming that my excel having trouble with this 'well formatted' HTML code as there are line-breaks. Therefore, I want before I place into CSV to make this HTML code into one line. Can you tell me how to achieve that?
You can replace line breaks by nothing to get a single lined output:
Dim TestString As String
TestString = " <body>" & vbCrLf & " some html" & vbCrLf & " </body>"
Dim SingleLined As String
SingleLined = Replace(TestString, vbCrLf, "")

MailMessage removing consecutive spaces

I'm using an SmtpClient object to send a MailMessage from one MailAddress to another. I'm trying to format the body of the MailMessage to look centered using a monospace font, but the consecutive spaces are always being replaced by a single space.
So:
With smtpMessage
.IsBodyHtml = True
.Body = "<body style=""font-family:monospace"">"
.Body &= " ************************************************************<br>"
.Body &= " * This email was generated by an automated system. *<br>"
.Body &= " * Please do not reply to this email. *<br>"
.Body &= " ************************************************************<br>"
.Body &= "</body>"
End With
Is being received as:
************************************************************
* This email was generated by an automated system. *
* Please do not reply to this email. *
************************************************************
I've searched around but all I'm finding are topics about line breaks not displaying properly, nothing about the spaces themselves.
The spaces are removed not by MailMessage, but by the use of HTML. HTML standard is to ignore extra spaces. If you truly wish to keep the extra spaces, you should either add the "<pre>" tag, or use .
In HTML, multiple spaces in code are replaced by one space. You can try to force a space to appear.
OR you can try composing your message as plain text (IsHtml = false) (and remove any HTML from message body).

How can I remove the initial return when the multiple texts are added to the field?

I am asking the user to select a txt file from a specified folder on a server [This is in PowerPoint 2007], but I need to give them the option of selecting more than one, so I have a bit of conditional code to determine this.
One file selected uses this code:
oShape.TextFrame.TextRange.Text = Text
More than one file selected currently uses this:
oShape.TextFrame.TextRange.Text = oShape.TextFrame.TextRange.Text & vbCrLf & vbCrLf & Text
…but this results in an extra return space above it all in the field, which is a bit untidy.
Could anyone advise me on how I can modify this to only get the returns in between the two texts, but not at the beginning?
I am not entirely sure I got the problem, but I believe this will do:
oShape.TextFrame.TextRange.Text = iif(oShape.TextFrame.TextRange.Text <> "", oShape.TextFrame.TextRange.Text & vbCrLf & vbCrLf, "") & Text

AppendText won't append to the next line in a text file / vb

I'm making a program that lets you add student info to an existing CSV text file but whenever I use append text it adds the new info to part of the last line and then a new line.
I want it to do this:
John Doe,29,Male
John Doe,29,Male
It does this instead:
John Doe,29,MaleJo
hn Doe,29,Male
Note: there isn't actually an empty line between each set of info, I just wanted it to be easy to read when I posted it here.
Here is the code for that section:
Dim swVar As IO.StreamWriter = IO.File.AppendText(frmMain.fileName)
swVar.WriteLine(txtName.Text & "," & txtAge.Text & "," & gender)
swVar.Close()
Any help would be appreciated, thanks!
A handy tool in VS2010 (and others) is snippets - right click Insert Snippets... - lots of code patterns for typical tasks. In your case here is a modified snippet:
Sub AddToFile(textToAdd As String, filePath As String)
My.Computer.FileSystem.WriteAllText(filePath, textToAdd, True)
End Sub
You may want to check/add a vbNewLine to the text being added since one is not automatically added as with WriteLine.
Not a direct reply to your stated problem, but an alternate method.
See if something like this works better:
IO.File.AppendText(frmMain.fileName, vbNewLine & txtName.Text & "," & txtAge.Text & "," & gender & vbNewLine)
AppendText will open write and close all in one operation so there's no need for a separate streamwriter. It also doesn't add newlines so those must be added separately
Unless of course you are doing a series of writes to the same file then something like this would probably be more appropriate:
Dim swVar As New IO.StreamWriter(frmMain.fileName, True)
'write your lines here
swVar.WriteLine(txtName.Text & "," & txtAge.Text & "," & gender)
swVar.Close()

Multilines In Richtextbox

Alright, I have a richtextbox that contains this.
line1
line2
As a test I used the code below to confirm that my program can read the lines but it doesn't.
If RichTextBox1.lines.Contains("Line1" & vbcrlf & "Line2") Then
MsgBox("hi")
End If
I've tried vbcrlf, environment.newline, char(32), vbcrlf & _.
thinking that either lines or contains is the problem.
RichTextBox.Lines returns an array with one element for each line of text. Contains("Line1" & VbCrLf & "Line2") will look for an element in the array that matches that string, but your array has one element with "line1" and a second element with "line2", not a single element with both. By the way, "Line1" will not match "line1", as there is a case difference between the two strings.
If you want to read the lines of the RichtTextBox, you can loop through it:
For Each line As String In RichTextBox1.Lines
' Do something here
Next
RichTextBox.Lines Property
have you try this
If RichTextBox1.lines.Contains("Line1" & vblf & "Line2") Then
MsgBox("hi")
End If