Include signature in VBA created email Outlook 2013 - outlook

Here's what I've got. It works to create a message with HTML format, but I want it to include my signature and I'm not sure how to do that. I looked at some other answers and their solutions don't seem to work for Outlook 2013.
Sub CreateHTMLMail()
'Creates a new email item and modifies its properties.
Dim objMail As MailItem
'Create mail item
Set objMail = Application.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = "test"
.Display
End With
End Sub

There is no signature-specific property in the Outlook object model. You can detect the existing signature (if any) by reading the existing set of signatures (if any) in the following folders, so if you create a signature in Outlook it will save three files (HTM, TXT and RTF):
Vista and Windows 7/8/8.1/10:
C:\Users\<UserName>\AppData\Roaming\Microsoft\Signatures
Windows XP :
C:\Documents and Settings\<UserName>\Application Data\Microsoft\Signatures
Application Data and AppData are hidden folders, change the view in Windows explorer so it shows hidden files and folders if you want to see the files.
Outlook adds the signature to the new unmodified messages (you should not modify the body prior to that) when you call MailItem.Display (which causes the message to be displayed on the screen) or when you access the MailItem.GetInspector property - you do not have to do anything with the returned Inspector object, but Outlook will populate the message body with the signature.
Once the signature is added, read the HTMLBody property and merge it with the HTML string that you are trying to set. Note that you cannot simply concatenate two HTML strings - the strings need to be merged. E.g. if you want to insert your string at the top of the HTML body, look for the <body substring, then find the next occurrence of > (this takes care of the <body> element with attributes), then insert your HTML string after that >.

Related

Save complete email, body and header, as PDF

I want to save complete mail as PDF.
I found code below in stackoverflow 1. It saves the mailitem body and not the header (such as sender, recipient, subject).
I tried to manipulate the Word.Document to add the header info manually (in the code below I just use minimal changes for testing purposes) but it seems to be readonly. I also thought of "Print as PDF" using the Outlook print functionality, but found no way to get it triggered from my Outlook VSTO solution.
using Word = Microsoft.Office.Interop.Word;
private void SaveMailAsPDF(Outlook.MailItem _mailitem)
{
//source: https://stackoverflow.com/questions/26421252/save-outlook-mailitem-body-as-pdf
Outlook.MailItem mi = _mailitem;
mi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
string datetimeReceived = mi.ReceivedTime.ToString("yyyyMMdd-Hmmss");
string fullPath = #"C:\Users\al\Documents\OutlookMailsTest\" + datetimeReceived + "Test.pdf";
Word.Document doc = mi.GetInspector.WordEditor;
//doc.Paragraphs.Add();
//doc.Paragraphs.Add();
//Word.Range rng = doc.Range(0, 0);
//rng.Text = "New Text";
doc.SaveAs2(fullPath, FileFormat: Word.WdSaveFormat.wdFormatPDF);
}
Try to save in the MHTML format (it preserves the embedded pictures and includes the headers) using MailItem.SaveAs, then open the MHTML file using Word object model and save it as a PDF file.
The Outlook object model doesn't provide any print as pdf methods. The MailItem.PrintOut method prints the Outlook item using all default settings.The PrintOut method is the only Outlook method that can be used for printing.
You can use the Word object model for saving the message body in the format you need. For example, you may save the document which represents the message body using the open XML document (*.docx) and then open it for editing using Open XML SDK for adding message headers (if required). See Welcome to the Open XML SDK 2.5 for Office for more information.
You are free to use third-party components to save the document with required customizations using the PDF file format.

How to get content from field

I'm totally new to Base. I have different forms but in one named F_STRUCT of them I'm trying to make a macro which will allow the user to autofill another field when he select a zipcode.
so the database looks like this.
ID_ZIP ZIP CITY
1 97425 Les Avirons
2 82289 Toto
In my forms, I have a select which allows to select the ZIP code. It's label and name is ZipCode.
So I don't really know where to find the API reference for all the methods and chill methods, I followed examples from internet.
I tried this
Sub getZip
Dim Doc As Object
Dim DrawPage As Object
Dim Form As Object
Doc = StarDesktop.CurrentComponent
DrawPage = Doc.DrawPage
Form = DrawPage.Forms.GetByIndex(0)
Toto = Form.GetByName("ZipCode")
Print "hey"
End Sub
But it returns an error on the Toto = Form.GetByName("ZipCode") line.
The code works, so the problem must be how you created the form or control. Follow these instructions to set up a correct example:
Create Form in Design View
Use the List Box tool (is that what you mean by "select"?) and create a control.
Cancel out of the wizard if it pops up.
Right-click on the control and select Control Properties (not Name, which would modify the shape name instead of the control's name).
Set the Name to "ZipCode" (without quotes).
Save and close the form.
Open the form. In the window of that form (the CurrentComponent) go to Tools -> Macros -> Run Macro.
A list of documentation links for Base is at https://ask.libreoffice.org/en/question/80972/to-learn-libreoffice-base-are-there-introductions-or-tutorials/?answer=80973#post-id-80973.

How to automate outlook when there is more than one account configured?

In my microsoft outlook...I have more than one account configured.. When I try to automate the customised mailbox(account). It always points to the default inbox... How to change this to the customised inbox I want?
We tried to use the below code:
But always it points to the mailbox XXXXX#medtronic.com and Inbox folder (The 2nd mailbox)
But we need to point to the Inbox of the RS mailbox and search for the email through VBscript as highlighted below in yellow colour.
enter code here
Set objApp = CreateObject("Outlook.Application")`enter code here`
Set objNameSpace = objApp.GetNamespace("MAPI")
Set objSyncs = objNameSpace.SyncObjects
Set myFolder = objNameSpace.GetDefaultFolder(6)
Set ObjMails = myFolder.Items
Set objFilter = ObjMails[![enter image description here][1]][1]
What points to the default mailbox? Namespace.GetDefaultFolder? You really need to provide the relevant snippets of your code.
If you want to work with a delegate folder, use either Namespace.GetSharedDefaultFolder, or, if the store is already opened in the active profile, retrieve the Store object from Namespace.Stores collection and use Store.GetDefaultFolder.

Clicking on a Link in Outlook 2013 using VBScript

I wanted to click on a Link in my email body using VBScript? is this possible...
the email is in my Drafts folder. So far i have been able to navigate to my drafts folder.
olFolderDrafts = 16
Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objDrafts = objNamespace.GetDefaultFolder(olFolderDrafts)
MsgBox objDrafts.Items.Count
Use a for each loop to enumerate the items of your objDrafts, store the HTMLBody from the item to a variable and extract the link with a regular expression from that varable.
Clicking on it won't work since it's no GUI but you can open the link with something like this CreateObject("WScript.Shell").Run("http://www.google.com")

Word VBA Application Event fires more times when more documents opened

I have the following problem. I wrote simple macro which shows MsgBox before print dialog. Here is source:
Public WithEvents App As Application
Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
MsgBox "aaaaa"
End Sub
Private Sub Document_New()
Set App = Application
End Sub
When I open one document from template with this macro, everything is OK. But I have a problem when I open two documents from this template at same time. When I click to print button, MsgBox shown up twice. Any ideas?
Edit:
When I create document from this template and create another new document, which isnt't based on this template (both of this documents are opened at the same time) and I print from that new empty document, MsgBox shown up. This is also wrong right?
You have created application-level events that fire every time any document is being printed. They are triggered once for every document that has this code in it, so every time you print a document you will get the msgbox once for every open document that has the code in it, whether or not the document that's printing has the code in it.
So, the behaviors aren't wrong, although clearly they are not what you want.
You should put the Before_Print event in the ThisDocument module of your template. That way the event will only happen once, and only when the document being printed has the code in it.
You could put a check in your App_DocumentBeforePrint sub to check whether the instance of the application object which is firing the event is the instance that contains the active document:
If Me <> ActiveDocument Then Exit Sub

Resources