Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

Mail Merge VBA Query

  • Please log in to reply



    New Member

  • Member
  • Pip
  • 1 posts
Hi people,
Hope you can help me with a problem...

Ok a problem with defining a select query for a mail merge.
I want to have a pop-up box and then use this value an the query data for a mail merge and have written the following quick macro:

Sub test_mail_merge()
' test_mail_merge Macro
' Macro by JPC
Dim payno As Integer
payno = InputBox("payroll number", "Payroll Selection", 0)

ActiveDocument.MailMerge.DataSource.QueryString = _
"SELECT * FROM C:\Documents and Settings\client\Desktop\test merge data.csv WHERE ((Payroll = payno))"

With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
End With
End Sub

If i replace the "payno" variable in the SELECT query with a real variable the merge works fine and only merges one record, i'm obviopusly using the defined variable "payno" incorectly, have tried removing brackets ect but no luck, any ideas.
  • 0


Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP