![]() |
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Search this Thread | Display Modes |
#1
|
|||
|
|||
![]()
Make sure you have a reference set to Word in the VBA project references. In
the Outlook VBA only a reference to Outlook is set automatically. Tools, References. Set a reference to Word there. -- Ken Slovak [MVP - Outlook] http://www.slovaktech.com Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003 Reminder Manager, Extended Reminders, Attachment Options http://www.slovaktech.com/products.htm wrote in message ps.com... Hello all, I am attempting to automate a mail merge from MS Outlook 2003. I have found some sample code here. http://groups.google.ca/group/micros...13b1236c 846f But am getting an error when I try to run it. This is my first attempt in writing in Outlook and am feeling a little flustered so any thing that you can offer would be appreciated. The error that I am getting is "Compile error - user defined type not defined" and it is failing on this line Private Function fillbookmark(sBookmark As String, sValue As String, _ odoc As word.document) As Boolean I have only had to slightly modify it for my own uses - at least at this point - once it is working I am sure I will manipulate it a little more. Any help that anyone is willing to offer would be appreciated. Thanks Terry Public Sub WordBookmark() Dim oOutlook As Outlook.Application Dim oInspector As Outlook.Inspector Dim oItem As Object Dim oContact As Outlook.ContactItem Dim oWord As word.Application Dim odoc As word.document Dim sBkmName As String Dim sTemplateName As String Dim blnFill As Boolean Dim fillbookmark As Boolean 'Change this file name and location if necessary. sTemplateName = "C:\My Documents\Fax Template (blue)1.dot" 'Get an Outlook Application object On Error Resume Next Set oOutlook = GetObject(, "Outlook.Application") If oOutlook Is Nothing Then Set oOutlook = CreateObject("Outlook.Application") End If On Error GoTo WordBookmarkError Set oInspector = oOutlook.ActiveInspector 'Look for an open Inspector window. If oInspector Is Nothing Then MsgBox "There is no open item" Else Set oItem = oInspector.CurrentItem 'Make sure the open item is a ContactItem. If oItem.Class = olContact Then Set oContact = oItem 'Get a Word Application object On Error Resume Next Set oWord = GetObject(, "Word.Application") If oWord Is Nothing Then Set oWord = CreateObject("Word.Application") End If On Error GoTo WordBookmarkError 'Add a document based on our template. Set odoc = oWord.Documents.Add(sTemplateName) With oContact 'Fill each bookmark in turn. sBkmName = "FullName" blnFill = fillbookmark(sBkmName, .FullName, odoc) sBkmName = "fax" blnFill = fillbookmark(sBkmName, .business_fax, odoc) 'Repeat the function call for each bookmark. 'sBkmName = "StreetAddress" 'blnFill = FillBookmark(sBkmName, .BusinessAddressStreet, _ oDoc) 'sBkmName = "City" 'blnFill = FillBookmark(sBkmName, .BusinessAddressCity, _ oDoc) 'sBkmName = "State" 'blnFill = FillBookmark(sBkmName, .BusinessAddressState, _ oDoc) 'sBkmName = "PostalCode" 'blnFill = FillBookmark(sBkmName, .BusinessAddressPostalCode, _ oDoc) 'sBkmName = "FirstName" 'blnFill = FillBookmark(sBkmName, .FirstName, _ oDoc) 'End With 'Activate our new document. odoc.Activate 'Turn off the display of bookmarks. odoc.ActiveWindow.View.ShowBookmarks = False 'Move the cursor to the end of the document. oWord.Selection.EndKey Unit:=wdStory, Extend:=wdMove 'Make the document visible. oWord.Visible = True odoc.ActiveWindow.Visible = True Else MsgBox "This is not a Contact item" End If End If WordBookmarkExit: 'Set all objects to Nothing to prevent memory and 'resource leaks. This still leaves the new docment open. Set oItem = Nothing Set oContact = Nothing Set oInspector = Nothing Set oOutlook = Nothing Set odoc = Nothing Set oWord = Nothing Exit Sub WordBookmarkError: MsgBox "Error occurred: " & Err.Description GoTo WordBookmarkExit End Sub Private Function fillbookmark(sBookmark As String, sValue As String, _ odoc As word.document) As Boolean With odoc If .Bookmarks.Exists(sBookmark) Then .Bookmarks(sBookmark).Range.Text = sValue fillbookmark = True Else fillbookmark = False End If End With End Function |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
mail merge email - why so different in Office 03 from 2000 | Sono | Outlook - Using Contacts | 2 | March 4th 06 06:31 PM |
How do I use nicknames in an e-mail mail merge? | brent | Outlook - Using Contacts | 2 | February 8th 06 09:29 PM |
Mail Merge Problem | Donna in Idaho | Outlook - Using Contacts | 2 | February 7th 06 10:04 AM |
Contact Mail Merge | WHGeorge | Outlook - Using Contacts | 1 | January 18th 06 10:18 AM |
Office 2003 AND Office XP - aka Office 2002 | rrose selavy | Outlook - General Queries | 3 | January 14th 06 10:14 AM |