I'm trying to spell check and send an outlook e-mail using the following macro:
Sub SpellIt()
Dim oMail As Outlook.MailItem
Dim oDoc As Word.Document
Set oMail = Application.ActiveInspector.CurrentItem
Set oDoc = Application.ActiveInspector.WordEditor
oMail.Save
oDoc.Content.LanguageID = wdGerman
oDoc.CheckSpelling
oMail.Save
oMail.Send
End Sub
I need to be able to set the language explicitly to German and I found it can be done using Content.LanguageID, but this does not include the subject line. How can I set the language for the subject line? Of course it would be even better to be able to set it for the whole document, but I cannot apply LanguageID to .Document
Any ideas?
Thanks,
Reto
The wordeditor is used in the body of the email (in certain conditions it will be null as well). So you would not be able to work with it that way.
oMail.Subject will get you the subject text I guess you can copy that into a new document and spellcheck that then copy it back? or insert it into you wordeditor in a know range and then check and set the subject back delete the orginal range etc.
Related
We use Outlook calendar to keep track of room bookings at our local community centre. For each calendar appointment/room booking we insert an object from file (an excel spreadsheet). The spreadsheet and file path is the same for each booking. Screenshot
I'd like to simplify embedding this spreadsheet with a macro. Unfortunately Outlook doesn't allow me to record one, so I'm trying to get to grips with scripting it with VBA. I tried recorded the macro in Word to see if it would copy over to Outlook (it doesn't - object variable not set). The code generated by Word was
Selection.InlineShapes.AddOLEObject ClassType:= _
"Excel.SheetMacroEnabled.12", FileName:= _
"K:\OutlookCalendar.xlsm" _
, LinkToFile:=False, DisplayAsIcon:=False
I think I need to specify DIMs, but I'm getting out of my depth and I've been unable to find a Google solution so far. Could someone point me in the right direction please?
First set a reference to the "Microsoft Word x.0 Object Library" so Outlook can recognize the objects used by Word - Click Tools/References
Now In order to use your recorded macro in Outlook, you need to set a variable to the Selection object Item.
Example
Option Explicit
Public Sub UseWord()
Dim Inspector As Outlook.Inspector
Dim wdDoc As Word.Document
Dim Selection As Word.Selection
Set Inspector = Application.ActiveInspector()
Set wdDoc = Inspector.WordEditor
Set Selection = wdDoc.Application.Selection
'-> And insert your recorded macro here
End Sub
IDE Tools / References
I was automating an Outlook email from excel today when I noticed I forgot a line but somehow it still worked and I didn't get a compile error. Is there such as thing as a default library object?
Sub sendmail()
Dim olmsg As Outlook.MailItem
'This works
Set olmsg = Outlook.CreateItem(olMailItem)
'What it should be
Set olmsg = Outlook.Application.CreateItem
End Sub
When working in Outlook, it automatically recognizes Outlook types.
Similar to how Excel will recognize keywords such as ActiveSheet and ActiveCell without first expressing Excel.Application.
It is good practice to use the longhand expression, incase you ever need to port a similar code to Excel for working in Outlook, and because VBA does not handle overflows well.
I'm completely new to VBA, especially across different programs, but I have created myself a little real-world task that quickly got complicated, someone could give me some pointers or code snippets to try out to achieve the following:
I have an Excel file that's filled with names and numbers (see below) and I would like to transfer them individually to a Word document.
If I have highlighted cell A2 and click on [BUTTON], I want Word to open automagically and type out something like
--"Hi Mike, your current amount is $12.37 and you live in 23 One Street. Thanks."--
The amount should be printed in bold, and after that Word should save the file and close itself without further input needed.
Similarly, when I have selected A3, it should open another document, write the same text but with Julia's variables filled in, save it to a specified location and close.
A B C
1 Name Address Amount
2 Mike 23 One Way $12.37
3 Julia 3949 Street $39.23
[BUTTON]
So essentially, I guess, I'm trying to "remote-control" Word from within Excel and feed some variables from Excel into Word. I am at a complete loss how to do that, to be honest.
What I have found so far is this:
Dim wdApp As Word.Application, wdDoc As Word.Document
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
Set wdDoc = wdApp.Documents.Open("C:\temp\[NAME AFTER SELECTED FIELD A2].docx")
wdApp.Visible = True
...
Now I don't know what to do!
How to pass the standard text with the variables from the row of the selected field over to Word?
How to format them (bold, Arial, red, etc.)?
How to save under the specified filename?
Is this even possible to do? I know VBA is very powerful, so I hope you can help me out!
I'm using Office 2013, so any caveats related to macro programming or VBA language should take that into account.
Thank you so much!
Don't use get/create object. My macros are all coded as follows:
dim wdApp as New Word.Application 'Always use a new instance
Set wdDoc = wdApp.Documents.Open("C:\temp\[NAME AFTER SELECTED FIELD A2].docx")
wdApp.visible = true
From there you simply work as you would in word. Passing variables is pretty simple as the datatypes exist on both sides (double, integer, string etc are all included in the standard VBA libraries that are included by default so no need to add references). So if I want to tell word to add a paragraph:
wdDoc.paragraphs.add
wdDoc.paragraphs(wdDoc.paragraphs.count).range.text = "Hello World!"
Want to add text from a specific range?
dim xlRange as Excel.Range
dim wdApp as New Word.Application 'Always use a new instance
Set wdDoc = wdApp.Documents.Open("C:\temp\[NAME AFTER SELECTED FIELD A2].docx")
wdApp.visible = true
set xlRange = activesheet.range("A1") 'Just as an example
wdDoc.paragraphs.add
wdDoc.paragraphs(wdDoc.paragraphs.count).range.text = xlRange.value
Finally, say you want to use a variable name form excel to open a specified word document:
dim name as string
name = selection.cells(1).value 'Assuming they selected the cell
dim wdApp as New Word.Application 'Always use a new instance
Set wdDoc = wdApp.Documents.Open("C:\temp\" & name & ".docx")
wdApp.visible = true
I know this is a year late, but hey, hope it helps.
As a final note:
Use exit sub just prior to end sub in the word macro. Exit sub returns to caller where as end sub does not.
sub test
'do the word stuff
exit sub 'Stop it short of end sub
end sub
You could do this but it would be quite difficult.
Much easier is to use your spreadsheet from Work.
In Word (2013) go to the Design ribbon, and use the Start Mail Merge wizard. The source of addresses will be your spreadsheet. I think this will be much easier than what you are planning.
Cheers -
I use Outlook (MS Exchange) and have an individual as well as two group inboxes (I'm working logged in with the individual profile through which I also have access to the group inboxes).
When I send an email, I chose either my individual or one of the two group email addresses in the From field. When the email is sent, I want a copy saved in the inbox of myIndividualMailbox, groupAMailbox, or groupBMailbox depending on which From email address I used.
Example: If I send an email From groupA#myCompany.com, I want a copy of the email saved in the inbox of the groupAMailbox (and not in my individual inbox).
I have understood that this is not possible by setting up a rule in Outlook but that it could be done with a VBA macro. I don't now how to write the VBA macro and don't know if this is a just a short script or more complicated. In fact I have never written a macro in Outlook so I don't even know how to begin. Can anyone show how to do this?
I started looking for a solution with this question: Outlook send-rule that filter on the 'From' field
I made this for you as far as I can tell, it works. You should put this in the Microsoft Outlook Objects - ThisOutlookSession Module.
Note that the myolApp_ItemSend event will never trigger unless you run enableEvents first. And you will need to make sure it is enabled every time you close an re-open Outlook. This will take some customization, but it should give you the general idea.
Option Explicit
Public WithEvents myolApp As Outlook.Application
Sub enableEvents()
Set myolApp = Outlook.Application
End Sub
Private Sub myolApp_ItemSend(ByVal item As Object, Cancel As Boolean)
Dim items As MailItem
Dim copyFolder As Outlook.Folder
Dim sentWith As String
'Identify sender address
If item.Sender Is Nothing Then
sentWith = item.SendUsingAccount.SmtpAddress
Else
sentWith = item.Sender.Address
End If
'Determin copy folder based on sendAddress
Select Case sentWith
Case "groupA#myCompany.com"
'get groupAMailbox's inbox
Set copyFolder = Application.GetNamespace("MAPI").folders("groupAMailbox").folders("Inbox")
Case "myE-mailAddress"
'get My inbox
Set copyFolder = Application.GetNamespace("MAPI").folders("myE-mailAddress").folders("Inbox")
End Select
'copy the Item
Dim copy As Object
Set copy = item.copy
'move copy to folder
copy.Move copyFolder
End Sub
EDIT: It looks like they've actually built the event functionality into the Application object for Outlook directly now, but it from testing you still have to do what I outlined above.
Outlook stores all sent items in default sent items folders. however you can apply a patch to save sent items in its own folder.
http://support.microsoft.com/kb/2181579
I copied example at http://support.microsoft.com/kb/220595 to the VBA in Excel.
My code follows:
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Dim olAppt As Outlook.AppointmentItem
Set olAppt = olApp.CreateItem(olAppointmentItem)
I obtained the following error on the line Dim olAppt As Outlook.AppointmentItem:
"User-defined type not defined".
How should this be fixed?
I use MS Office 2003.
There are four prerequisite steps listed in that article. They are listed right before the code block.
You probably forgot to follow the step two, Adding a reference to Outlook object library.
The only difference is, in VBA the menu item is under Tools, not Project.
You need to refer to all constants by their value, so, as olAppointmentItem = 1 :
Set olAppt = olApp.CreateItem(1)
You can either look up values, for example http://msdn.microsoft.com/en-us/library/aa911356.aspx, or use Outlook's Object Browser to get the values.