Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Take text from email, open doc ( from a template) and, insert text

Status
Not open for further replies.

JoshuaVaughn1

Technical User
Dec 1, 2011
3
US
Hi all,

Here's my use case. When I get an email, I'd like to be able to hit a macro/run VBA from Outlook that:

1. Opens a new instance of a Word doc based off a specific Word template.

2. Pulls text from fields in that Outlook email (subject line, tables, or form fields in the body, whatever).

3. Then inserts the text pulled from the email into specific fields (text boxes) found in the word doc.

Thanks for taking the time to read this,
God bless,

Joshua
 
Here's what you'll need to do:
1. Set a reference to Microsoft Word ## Object Library
2. In your code, create objects for the Message, and the Word Document. If the text boxes are embedded within the document itself, rather than in a separate form, then you'll just need to reference the text boxes in the code... So basically as simple as:
MyWordDocObject.TextBox1 = MyMailItem.Subject
MyWordDocObject.TextBox1 = MyMailItem.Body

and so on... (of course I'm just making up names here for the object names..

As far as having the the code run from Outlook upon receiving a new mail item, you can do it a couple of ways. Either way, you'll want to put the code into a Standard module. Then how you run it would be 1 of 2 ways:
1. Have a rule reference it (tell the rule to "run a script" based on whatever rule critieria you want to have.
2. In the VB Editor, in the ThisOutlookSession module, put a new procedure, if not already there:
Code:
Private Sub Application_NewMail()
   [green]'Call your other code here by putting the procedure name[/green]
End Sub

I would also suggest you put some conditions in your code so that it exits as quick as possible (perhaps just using the rules will be enough, but maybe not) to make sure that it's an email you WANT to put to Word. Otherwise, it'll really slow down your email operations, and you'll spend more time waiting than doing anything... possibly.

Once you get started on the project, and run into issues, put as much detail as you can for each problem here, and we can help step through it all.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top