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.
Related
I have a procedure that creates a PDF file according to an ms word template and its data is retrieved from a database.
It works fine, creates a PDF file perfectly , no run time errors. The problem is that whenever I shut off the computer, ms word prevents the shutdown and if I press cancel ms word shows a message;
The code goes like this;
Dim wordApp As Word.Application
Dim templateBookmarks As Word.Bookmarks
Dim templateName As String
Dim template As Word.Document
'Some other variables for computations
wordApp = CreateObject("Word.Application")
sourceTable = New DataTable
'Some other procs to fill the data table
templateName = "Foo Template.docx"
template = wordApp.Documents.Add(templatePath & templateName)
templateBookmarks = template.Bookmarks
templateBookmarks.Item("sample bookmark").Range.Text = "foo"
'Then fills the table in the template like...
template.Tables(1).Cell(1, 1).Range.Text = dataSource.Rows(0).Item(0)
'Then saves the document as a pdf
Dim saveName As String = "sample file"
template.SaveAs2(savePath & saveName, Word.WdSaveFormat.wdFormatPDF)
I have tried to force garbage collection for the word COM resources, as well as changing the template from an actual document i.e. docx to a word template .dotx. I also tried the method Quit() but it only shows the ms word message much earlier. This is the first time I needed to use interop so pardon if I don't have much idea about it.
The files I needed are saved, the only problem is the ms word message and unsaved and unnecessary files e.g. Document1,Document2,Document3 that seems to be created aside from the required PDF
Use the Document.Close method which closes the specified document after saving files using the PDF file format. It allows specifying the SaveChanges parameter which specifies the save action for the document. Can be one of the following WdSaveOptions constants: wdDoNotSaveChanges, wdPromptToSaveChanges, or wdSaveChanges.
On Error GoTo errorHandler
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
errorHandler:
If Err = 4198 Then MsgBox "Document was not closed"
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)
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 VS2010 Vb.net program that creates a Word 2007 file.
My Normal.dot file is customised to give me a new Tab with Buttons in that do specific things via VBA in the Normal.dot program when those Buttons are pressed.
This all works fine, however, I now want to add some functionality whereas as soon as the new Word document is created, it edits a Task in Outlook.
I have edited the 2 "This Document" Procedures and you can see my Normal.Dot file in the attached Screenshot.
When I run my VB.Net program to create a brand new Word 2007 document, the program does NOT stop on either of the message boxes, it just continues and opens the Word document as before, my code is below, what am I doing wrong ?!?
'Open or Create Word document for Editing
myNewsLetter = myFolder + myLeague + "News" + mySession + ".doc"
If File.Exists(myNewsLetter) Then
'do nothing
Else
myTemplate = myTempFolder + "NL Skeleton.doc"
File.Copy(myTemplate, myNewsLetter)
Create_Blank_Newsletter()
End If
'Open Word Newsletter, or switch to it if it's already open
Dim myFileOpen As Boolean
myFileOpen = IsFileOpen(myNewsLetter)
If myFileOpen = False Then
MSDoc = MSWord.Documents.Open(myNewsLetter)
End If
MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
MSWord.Visible = True
MSWord.ActiveDocument.Bookmarks("\StartOfDoc").Select()
OK, sorted this, the full discussion can be found here ... http://www.vbaexpress.com/forum/showthread.php?p=286771#post286771
Basically, I'm not creating a NEW document, I am creating a new document via a Copy and then opening that existing document !!!
I am writing an outlook sub procedure that takes the currently selected email, parses it, and creates a new email message. The parsing is simple enough: Extract the email addresses from the first line of the message and then the rest of the body is the regular email body in the new message.
I am using this basic code for setting the body of the new message:
Set newMsg = Outlook.Application.CreateItem(olMailItem)
With newMsg
.BodyFormat = olFormatHTML
.Body = newBody
'... set subject,etc
.Display
The problem is that the new message that is created loses all of the HTML formatting that the message I was copying the information from possessed ( and various font stylings). I tried setting the new message's body format to HTML (see code above), but that did not do the trick. Currently, the new message contains all of the textual data, but instead of the table, each cell's data is tab-delimited and the entire message body is in the same font.
BodyFormat does not behave as expected. Instead, HTMLBody can be used as below to properly display the body in HTML format:
With newMsg
.HTMLBody = newBody
'... set subject,etc
.Display