PDA

View Full Version : Using Word form with Outlook Contacts



Kathy
03-01-2005, 02:49 PM
I need to create a form that will pull up the address of a particular client. We already have names and addresses listed in the Outlook Contacts folder. Is it possible to use that for the source of the names and addresses? If so, how do I make it work, and how do I allow the users to find and pick the client they would like to use on the form? I have used Access database to do this, but it seems very different using Word and Outlook....HELP!:help

Kathy

Killian
03-02-2005, 05:13 AM
Hi Kathy,

This should get you started...
In your Word VBA Project, set a reference to the Outlook object model in Tools|References then add a UserForm with a ComboBox and put this code in the UserForm's Initialize event:
Dim objOL As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim objFolder As MAPIFolder
Dim c As Outlook.ContactItem

Set objOL = New Outlook.Application
Set objNS = objOL.GetNameSpace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderContacts)
For Each c In objFolder.Items
ComboBox1.AddItem c.FullName & " - " & c.Email1Address
Next
ComboBox1.ListIndex = 0

Set objOL = Nothing

Kathy
03-02-2005, 07:32 AM
Hi Killian,

Thanks for the fast and thorough help. But now I think my problem is more than just code...I only have MS Office Word 2003. When I tried to find the Tools>References like you instructed, it wasn't there. Then I started doing a search on VBA Project, and learned that apparently VBA Project is something completely different than Word...(I think). Is this an add-in for Word that I can get from the MS website?

OK, I've been poking around Word, and found the VB toolbar....now it all makes sense. I'm definately a newbie with Word VB.
Thanks :hi:

Killian
03-02-2005, 12:39 PM
Well if you've found the VB toolbar you're on the right track :yes
The best way to get started is to open the VB Editor (you can also do this with Alt-F11 in any MS Office app) and have a look around. The Help files there have decent examples and overviews to get started.
Then you should use the VB toolbar to record a marco in Word and then go back to the VB Editor and take a look at what it does in code.
You'll soon get the idea and there's always plenty of advice on this forum!

Kathy
03-02-2005, 02:58 PM
Thanks for the step-by-step help. I'm leaving the office now, but will pick up with this tomorrow. Nice to know you're willing to help a beginner. Tomorrow...recording a macro.

Thanks!

Kathy
03-07-2005, 02:42 PM
Ok, having a problem. When I right-click the form, click "view code" and insert the code that you gave me, then try to run it, I get a compile error "invalid outside procedure". What am I doing wrong? I was wondering if there was a book or CD tutorial that I could learn the Visual Basics in a step-by-step manner. Recently I purchased "Mastering Word Made Easy" from TeachUcomp, Inc hoping it would cover VBE, but it doesn't. I really would like to learn to use this. Could you recomend something?

Thanks
Kathy

Rollin_Again
03-07-2005, 02:59 PM
You need to name your sub and include the "End Sub" line at the end

Public sub Contact()

Dim objOL As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim objFolder As MAPIFolder
Dim c As Outlook.ContactItem

Set objOL = New Outlook.Application
Set objNS = objOL.GetNameSpace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderContacts)
For Each c In objFolder.Items
ComboBox1.AddItem c.FullName & " - " & c.Email1Address
Next
ComboBox1.ListIndex = 0

Set objOL = Nothing

End Sub



Rollin

Killian
03-07-2005, 06:05 PM
Hi Kathy.

Rollin is right there, I think the code wasn't in a procedure but I get the idea you won't learn too much copying this example if you're not clear on how it all fits together... and a book covering the basics is definately the best place to start. That's how I got started with just about everything to do with PCs and programming and I can now hold down a job as a lead developer so there must be something to it! I just had a quick look at Amazon (search "VBA beginners") and there's a few to choose from.
I think the ...for Dummies programming books are excellent and would recommend one if you're starting from scratch because when it comes to writing code, if you don't get the basics right, it will always be a struggle.
Also, don't worry too much about whether it's specifically Word because it's important to get the principles of programming in VBA down first... working with the Word, Excel or whatever objects will soon fall into place once you get going.
There's also a lot of stuff in the knowledge base here with full descriptions and instructions for use that you could go through as you pick it up.
I've been thinking about writing up an introduction to VBA to post up here but I haven't written anything down yet so don't wait... buy a book and get started! :whip
The 'Resources' link here also has a number of sites that have tutorials that are worth a visit.
Most important of all, if you're writing some code and you get stuck, come and ask! I've only been a member of this forum a few months but I've noticed a friendly knowledge-sharing vibe going on here that should keep you moving up the learning curve. If you give it a good shot within a few months you'll be answering some questions yourself :thumb

(I've attached a working example from your original question, just in case you can't wait 'til you get to a bookshop...)

Kathy
03-08-2005, 11:39 AM
Thanks Rollin! I'll give it a try. Appreciate your time.

Many thanks,
Kathy

Hi Killian,
I really do appreciate your time explaining this all to me, and appreciate your patience with a beginner. I am truely a beginner with this, but understand the power of this, and really want to learn how to do the things I want with my programs. I will definately look for those books you recommend..."VBE for Dummies" is definately my kind of book. Hopefully I can get it used online somewhere. It's also reassuring to know that you recommend this book, sounds like what I need. Thanks also for the working example!....can't wait to take a look at it.

Thanks for your fast reply!
Kathy :friends:

mdmackillop
03-08-2005, 01:18 PM
HI Kathy,
Just a small point, but the VBA help is not installed during a "standard" installation, so you may need to add these from your CD.
MD

Kathy
03-09-2005, 07:31 AM
Thanks for bringing that to my attention, no wonder I was not getting any helpful information in trying to research from the program.

Many thanks!
Kathy