HELP! New To Macros

J

jburgess

Hi all,
I am trying to get my MSWord Macro to look in specific places to find
the records in my mail merge to print. Ideally I would like a .txt file
with records numbers in and the macro to look at what it is being told
to print and change the number in the code to correspond with this...
Here is the code I have...

Sub Merge()
'
' Merge Macro
' Macro created 28/02/2006 by A Computer
'

With ActiveDocument.MailMerge
SendKeys "{enter}"
.Destination = wdSendToPrinter
.DataSource.FirstRecord = 1
.DataSource.LastRecord = 1
.Execute

SendKeys "{enter}"
.Destination = wdSendToPrinter
.DataSource.FirstRecord = 3
.DataSource.LastRecord = 3
.Execute
End With
End Sub

P.S.
Also I know using SendKeys isn't exactly recommended, any replacment
commands???
 
D

Doug Robbins - Word MVP

If you run the following macro when the mail merge main document is active,
it will display the file open dialog box so that you can open a Word
document in which you have the record numbers that you want to print as
individual numbers in separate paragraphs in the document, such as

1
3
5

It will then execute the merge for those record numbers:

Dim maindoc As Document
Dim Source As Document
Dim recrange As Range
Dim records As Variant
Dim i As Long
Set maindoc = ActiveDocument
With Dialogs(wdDialogFileOpen)
.Show
End With
Set Source = ActiveDocument
Set recrange = Source.Range
recrange.End = recrange.End - 1
records = Split(recrange.Text, vbCr)
For i = 0 To UBound(records)
MsgBox records(i)
With maindoc.MailMerge
.DataSource.FirstRecord = records(i)
.DataSource.LastRecord = records(i)
.Destination = wdSendToNewDocument
.Execute
End With
Next i

If you wanted an input box to be displayed into which you could enter the
numbers of the records, then use

Dim maindoc As Document
Dim records As Variant
Dim i As Long
Set maindoc = ActiveDocument
records = Split(InputBox("Enter the numbers of the records that you want
to print, with each one separated by a comma (no spaces)", "Record
Selector"), ",")
For i = 0 To UBound(records)
MsgBox records(i)
With maindoc.MailMerge
.DataSource.FirstRecord = records(i)
.DataSource.LastRecord = records(i)
.Destination = wdSendToNewDocument
.Execute
End With
Next i

In you macro, the following needs to be all one line of code

records = Split(InputBox("Enter the numbers of the records that you want
to print, with each one separated by a comma (no spaces)", "Record
Selector"), ",")


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

Sorry, the line

.Destination = wdSendToNewDocument

should have been

.Destination = wdSendToPrinter

for your purposes.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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

Similar Threads


Top