473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Mail Merge help

164 100+
Hi there, I am using office 2007. I know a little vba, but not too much. I have a pretty simple database that has a couple tables and one form. My goal is to be able to hit a button on the form and have all that information merge into a word doc.

I have accomlished this by using the mail merge wizard with access, but I was hoping for something a bit quicker for day to day use.

Also the only way I could find to do it was merging to word from a table and not the form record.


I have been searching and found this may be able to be done with a word template that includes bookmarks??
I was hoping for some advise on how to accomplish this with the button in the form that kind of does all of the work for me. The template will never change and neither will the fields in my access form.


any information or pointer would be amazing

Thanks guys!
Nov 10 '07 #1
2 1561
mbatestblrock
164 100+
Anyone???


I have a somewhat update..

I was checking into an access book...

I can get this to work using this..

Expand|Select|Wrap|Line Numbers
  1. Public Sub MergetoWord()
  2. ' This method creates a new document in
  3. ' MS Word using Automation
  4.   Dim rsCust As New ADODB.Recordset
  5.   Dim sSQL As String
  6.   Dim WordObj As Word.Application
  7.   Dim iTemp As Integer
  8.  
  9. On Error Resume Next
  10.  
  11.   sSQL = "SELECT * FROM Customers " _
  12.     & "WHERE CustomerNumber = " _
  13.     & Forms!Orders![CustomerNumber]
  14.  
  15.   rsCust.Open sSQL, CurrentProject.Connection
  16.  
  17.   If rsCust.EOF Then
  18.     MsgBox "Invalid customer", vbOKOnly
  19.     Exit Sub
  20.   End If
  21.  
  22.   DoCmd.Hourglass True
  23.  
  24.   'Try to get a running instance of Word:
  25.   Set WordObj = GetObject(, "Word.Application")
  26.   If Err.Number <> 0 Then
  27.     'An error is thrown if Word is not running,
  28.     'so use CreateObject to start up Word:
  29.     Set WordObj = CreateObject("Word.Application")
  30.   End If
  31.  
  32.   'Make sure the user can see Word:
  33.   WordObj.Visible = True
  34.  
  35.   'Warning:
  36.   'Specify the correct drive and path to the
  37.   'file named Thanks.dotx in the line below.
  38.  
  39.   WordObj.Documents.Add _
  40.   Template:="C:\Thanks.dotx", NewTemplate:=False
  41.  
  42.   With WordObj.Selection
  43.     .GoTo what:=wdGoToBookmark, Name:="FullName"
  44.     .TypeText rsCust![ContactName]
  45.  
  46.     .GoTo what:=wdGoToBookmark, Name:="CompanyName"
  47.     .TypeText rsCust![CompanyName]
  48.  
  49.     .GoTo what:=wdGoToBookmark, Name:="Address1"
  50.     .TypeText rsCust![Address1]
  51.  
  52.     .GoTo what:=wdGoToBookmark, Name:="Address2"
  53.     If IsNull(rsCust![Address2]) Then
  54.       .TypeText ""
  55.     Else
  56.       .TypeText rsCust![Address2]
  57.     End If
  58.  
  59.     .GoTo what:=wdGoToBookmark, Name:="City"
  60.     .TypeText rsCust![City]
  61.  
  62.     .GoTo what:=wdGoToBookmark, Name:="State"
  63.     .TypeText rsCust![State]
  64.  
  65.     .GoTo what:=wdGoToBookmark, Name:="Zipcode"
  66.     .TypeText rsCust![Zipcode]
  67.  
  68.     .GoTo what:=wdGoToBookmark, Name:="PhoneNumber"
  69.     .TypeText rsCust![PhoneNumber]
  70.  
  71.     .GoTo what:=wdGoToBookmark, Name:="NumOrdered"
  72.     .TypeText Forms!Orders![Quantity]
  73.  
  74.     .GoTo what:=wdGoToBookmark, Name:="ProductOrdered"
  75.     If Forms!Orders![Quantity] > 1 Then
  76.       WordObj.Selection.TypeText Forms!Orders![Item] & "s"
  77.     Else
  78.       WordObj.Selection.TypeText Forms!Orders![Item]
  79.     End If
  80.  
  81.     .GoTo what:=wdGoToBookmark, Name:="FName"
  82.  
  83.     iTemp = InStr(rsCust![ContactName], " ")
  84.  
  85.     If iTemp > 0 Then
  86.       .TypeText Left$(rsCust![ContactName], iTemp - 1)
  87.     End If
  88.     .GoTo what:=wdGoToBookmark, Name:="LetterName"
  89.     .TypeText rsCust![ContactName]
  90.  
  91.     DoEvents
  92.     WordObj.Activate
  93.     .MoveUp wdLine, 6
  94.   End With
  95.  
  96.   'Set the Word Object to Nothing to free resources:
  97.   Set WordObj = Nothing
  98.   DoCmd.Hourglass False
  99.  
  100. End Sub
  101.  
  102.  
  103.  
  104.  
  105. Private Sub Command12_Click()
  106.  
  107. End Sub
  108.  

I have to open the Module and run it...

I having one heck of time trying to figure out how to make this happen with a button on the form.. I did an on click, and pasted that same code in there, and it wont do ANYTHING I made sure all the references were checked as well.

I am stumped! This is for someone else, if it the DB were for me Id be fine running the module manually, but I don't think this person will want to that.

How can I get that button to run that module???
Nov 11 '07 #2
NeoPa
32,556 Expert Mod 16PB
Can you explain what happened a bit more precisely.
I can't help much with the logic of the MS Word stuff, but hopefully I can throw some light on why it's not running the code. I will need a bit more information as to what exactly is happening though.
Nov 12 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: William Wisnieski | last post by:
Hi Everyone, Access 2000 I have some code behind a button that performs a word merge with a query data source. The merge works fine. But what I'd like to do somehow is after the merge is...
2
by: Aaron | last post by:
hello, i am perfoming a mail merge with the following code. Public Function MergeIt() Dim objWord As Object Set objWord = GetObject("C:\MyMerge.doc", "Word.Document") ' Make Word visible....
8
by: Squirrel | last post by:
Hi everyone, I've created a mail merge Word doc. (using Office XP) , the data source is an Access query. Functionality I'm attempting to set up is: User sets a boolean field to true for...
3
by: Andy Davis | last post by:
I have set up a mail merge document in Word 2003 which gets its data from my Access 2000 database. I want to set up a button on a form that: 1. runs the query to provide the dat for the merge...
0
by: Phil C. | last post by:
Hi, I'm using Access 2000. I have a Select Query that uses the MID function to separate the actual text of articles from the title of the articles. The articles are enterd into the...
1
by: ckirby | last post by:
I've inherited an application for creating mail merges from an Access 2003 ADP that needs an overhaul. To generate the mail merge , the app uses a shell command: result = Shell(str, vbHide) ...
8
by: Ron B | last post by:
Help!!! What am I doing wrong? I am working with Office 2003 and am trying to create a command button on an Access form that will create a mail merge in Word from an Access table. I want to...
6
by: crealesmith | last post by:
Firstly, I have no problem with mail merging to Word, VB code for that works perfectly. On one mail merge I need to merge 15 fields of data that are from 3 seperate records. The 3 records are all...
1
by: mr k | last post by:
Hi, I wanted to use mail merge with forms but Text form fields are not retained during mail merge in Word, I got the code from Microsoft but it doesn't remember the text form field options such as...
1
by: kayberrie | last post by:
I want to write a VBA mail merge code. I want to link the code/macro/dohicky to a nifty little button so it makes life easy. I think I can handle the button part, the code part - not so much. I know...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.