Outlook VBA Outlook 2003 SP3 Custom Form

Joined
Sep 8, 2011
Messages
4
Reaction score
0
Hi,

Really new at this VBA stuff. Having a heck of a time transferring custom field data to a Word Template. I have a custom form based on the Contacts form. The first tab is called "Build Sheet" and it has a text box on it called "usrName"

Sub cmdPrint_Click()
Set oWordApp = CreateObject("Word.Application")
If oWordApp Is Nothing Then
MsgBox "Couldn't start Word."
Else
Dim oWordApp
Dim oWordDoc
Dim bolPrintBackground

msgbox "Open Doc"
Set oDoc = oWordApp.Documents.Add("c:\Templates\BuildSheet.dot")

msgbox "Field 1: Find"
strusrName = Item.UserProperties.Find(usrName)


msgbox "Field 1: Result"
oDoc.FormFields("dName").Result = strusrName

msgbox "Field 2"
strusrCWID = Item.UserProperties.Find(usrCWID)
oDoc.FormFields("dCWID").Result = strusrCWID

msgbox "Begin Print"
bolPrintBackground = oWordApp.Options.PrintBackground
oWordApp.Options.PrintBackground = False
oDoc.PrintOut
oWordApp.Options.PrintBackground = bolPrintBackground
Const wdDoNotSaveChanges = 0
oDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
oWordApp.Quit
Set oDoc = Nothing
Set oWordApp = Nothing
End If
End Sub


So I got this code from a website and I've been trying different things to get the data moved over to the word template. I added the msgbox as a sort of debug thing. The script runs until the bolded part.then stops. If I replace Item.UserProperties.Find(usrName) with "Hello" the script runs past this point but encounters the same issue in the next Item.UserProperties command. If I replace that one also, the script completes and the DOT prints with the text I chose.

What am I missing? I have a sinking feeling it's ridiculously easy....
 
Joined
Sep 3, 2008
Messages
164
Reaction score
5
arch,

Here is syntax that will take a custom field from outlook and put the data in a Word document.

You will need to define the bookmark in Word with the matching name.

Are you limited to Word? Excel is a lot easier to dump data from Outlook fields. Word requires everywhere data is inserted has a defined bookmark.

I assume you will have to customize this code. The syntax works on dozens of custom forms I have.

strDetail 1 is the variable to hold the string
UserProperties ("Tile1") indicates the field name in Outlook is Tile1
objDoc1.Bookmarks("Tile1") is the name of the Word bookmark.

Code:
strDetail1 = Item.UserProperties("Tile1")
objDoc1.Bookmarks("Tile1").Range.InsertAfter strDetail1

I have quite a bit of code for integrating Outlook with MS applications.

For the benefit of regular users of the forum can you please post back what works for you?

Stoneboysteve
 
Joined
Sep 8, 2011
Messages
4
Reaction score
0
Hmm easier to dump outlook to excel you say? If I was going to do the exact same thing with an excel template... how would I go about doing it? I was only using word because several sites I read (which there wasn't many out there that had good descriptions) all used word... if you could teach me how to do the same thing in excel it would make my life much easier I believe.


For the record, the previous code I quoted was missing the quotations around the field name. It should have been:

strusrName = Item.UserProperties.Find("usrName")

Also note that code is having issues quitting word.
 
Joined
Sep 8, 2011
Messages
4
Reaction score
0
I am still searching for ways to do this with an Excel spreadsheet as you suggested. I'm having difficulty locating websites with information useful to me. I think i might be searching for the wrong terminology? Can you help point me in the right direction Steve?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top