Help!

Selecting Only One Record In A Merge

 
  

Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> MailMerge Fields RSS
Next:  Macro-copy  
Author Message
Michael Koerner
External


Since: Mar 13, 2004
Posts: 2080



PostPosted: Thu Sep 17, 2009 8:30 am    Post subject: Selecting Only One Record In A Merge
Archived from groups: microsoft>public>word>mailmerge>fields (more info?)

I sometimes only need to print one envelope from my mail merge list. Thanks
to Peter Jamieson who a wrote who a while ago wrote the following macro for
me which allows me to select a starting letter for the merge. I was
wondering if the macro (I have a hard time spelling macro) could be edited
so that if I inserted a telephone number only that record would pop up in
the envelope.

Sub OneMergePerInitialLetterAskStart()
' error trapping to be added
Dim bDone As Boolean
Dim iLetter As Integer
Dim objMMMD As Word.Document
Dim strColumnName As String
Dim strSheetName As String
Dim strStartLetter As String
bDone = False
Do
strStartLetter = InputBox("Enter the starting letter," & _
" or blank to quit", "Starting letter", "a")
strStartLetter = UCase(Trim(strStartLetter))
If Len(strStartLetter) = 0 Then
bDone = True
Else
If Len(strStartLetter) > 1 Then
MsgBox "Enter a single letter" & _
" (from A to Z or a to z)," & _
" or blank to quit", vbOKOnly
Else
If strStartLetter < "A" _
Or strStartLetter > "Z" Then
MsgBox "Enter a (single) letter" & _
" from A to Z or a to z," & _
" or blank to quit", vbOKOnly
Else
bDone = True
End If
End If
End If
Loop Until bDone

If strStartLetter <> "" Then
' Set this to the name of the worksheet or to the range name
strSheetName = "Nominal Roll$"
' Set this to the exact name of the column containing the name
' (upper/lower case is probably significant
strColumnName = "LastName"

Set objMMMD = ActiveDocument
With objMMMD
For iLetter = Asc(strStartLetter) To Asc("Z")
.MailMerge.DataSource.QueryString = _
" SELECT * FROM [" & strSheetName & "]" & _
" WHERE ucase(" & strColumnName & ") like '" & _
Chr(iLetter) & "%'"
With .MailMerge
' remove or change this as necessary
.Destination = wdSendToNewDocument
On Error GoTo norecords
.Execute Pause:=False
End With
If MsgBox("Completed Letter " & Chr(iLetter) & _
". Do the next letter?", vbOKCancel) = vbCancel Then
Exit For
End If
norecords:
If Err.Number = 5631 Then
' Assume it is because there were no records for this letter
' Not necessarily true - could be just a badly formed query
Err.Clear
On Error GoTo 0
Resume atloop
Else
' just stop
On Error GoTo 0
End If
atloop:
Next
End With
Set objMMMD = Nothing
End If
End Sub


--

Regards
Michael Koerner
Back to top
Peter Jamieson
External


Since: Jan 15, 2009
Posts: 94



PostPosted: Fri Sep 18, 2009 7:10 am    Post subject: Re: Selecting Only One Record In A Merge [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Can the phone number only appear in one column in your data source, or
could it be in several? Do your numbers have non-numeric characters,
e.g. extension numbers introduced with "x", international "+" at the
beginning of the number, spaces, etc.?

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv

Michael Koerner wrote:
> I sometimes only need to print one envelope from my mail merge list. Thanks
> to Peter Jamieson who a wrote who a while ago wrote the following macro for
> me which allows me to select a starting letter for the merge. I was
> wondering if the macro (I have a hard time spelling macro) could be edited
> so that if I inserted a telephone number only that record would pop up in
> the envelope.
>
> Sub OneMergePerInitialLetterAskStart()
> ' error trapping to be added
> Dim bDone As Boolean
> Dim iLetter As Integer
> Dim objMMMD As Word.Document
> Dim strColumnName As String
> Dim strSheetName As String
> Dim strStartLetter As String
> bDone = False
> Do
> strStartLetter = InputBox("Enter the starting letter," & _
> " or blank to quit", "Starting letter", "a")
> strStartLetter = UCase(Trim(strStartLetter))
> If Len(strStartLetter) = 0 Then
> bDone = True
> Else
> If Len(strStartLetter) > 1 Then
> MsgBox "Enter a single letter" & _
> " (from A to Z or a to z)," & _
> " or blank to quit", vbOKOnly
> Else
> If strStartLetter < "A" _
> Or strStartLetter > "Z" Then
> MsgBox "Enter a (single) letter" & _
> " from A to Z or a to z," & _
> " or blank to quit", vbOKOnly
> Else
> bDone = True
> End If
> End If
> End If
> Loop Until bDone
>
> If strStartLetter <> "" Then
> ' Set this to the name of the worksheet or to the range name
> strSheetName = "Nominal Roll$"
> ' Set this to the exact name of the column containing the name
> ' (upper/lower case is probably significant
> strColumnName = "LastName"
>
> Set objMMMD = ActiveDocument
> With objMMMD
> For iLetter = Asc(strStartLetter) To Asc("Z")
> .MailMerge.DataSource.QueryString = _
> " SELECT * FROM [" & strSheetName & "]" & _
> " WHERE ucase(" & strColumnName & ") like '" & _
> Chr(iLetter) & "%'"
> With .MailMerge
> ' remove or change this as necessary
> .Destination = wdSendToNewDocument
> On Error GoTo norecords
> .Execute Pause:=False
> End With
> If MsgBox("Completed Letter " & Chr(iLetter) & _
> ". Do the next letter?", vbOKCancel) = vbCancel Then
> Exit For
> End If
> norecords:
> If Err.Number = 5631 Then
> ' Assume it is because there were no records for this letter
> ' Not necessarily true - could be just a badly formed query
> Err.Clear
> On Error GoTo 0
> Resume atloop
> Else
> ' just stop
> On Error GoTo 0
> End If
> atloop:
> Next
> End With
> Set objMMMD = Nothing
> End If
> End Sub
>
>
Back to top
Michael Koerner
External


Since: Mar 13, 2004
Posts: 2080



PostPosted: Fri Sep 18, 2009 9:58 am    Post subject: Re: Selecting Only One Record In A Merge [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Peter;

The phone number is only in one data source which is in this case is Excel
Col " O" with a header row called PhoneNumber and appear as xxx-xxx-xxxx

--

Regards
Michael Koerner


"Peter Jamieson" <pjj RemoveThis @KillMAPSpjjnet.demon.co.uk> wrote in message
news:OQ4RwnEOKHA.3412@TK2MSFTNGP06.phx.gbl...
Can the phone number only appear in one column in your data source, or
could it be in several? Do your numbers have non-numeric characters,
e.g. extension numbers introduced with "x", international "+" at the
beginning of the number, spaces, etc.?

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv

Michael Koerner wrote:
> I sometimes only need to print one envelope from my mail merge list.
> Thanks
> to Peter Jamieson who a wrote who a while ago wrote the following macro
> for
> me which allows me to select a starting letter for the merge. I was
> wondering if the macro (I have a hard time spelling macro) could be edited
> so that if I inserted a telephone number only that record would pop up in
> the envelope.
>
> Sub OneMergePerInitialLetterAskStart()
> ' error trapping to be added
> Dim bDone As Boolean
> Dim iLetter As Integer
> Dim objMMMD As Word.Document
> Dim strColumnName As String
> Dim strSheetName As String
> Dim strStartLetter As String
> bDone = False
> Do
> strStartLetter = InputBox("Enter the starting letter," & _
> " or blank to quit", "Starting letter", "a")
> strStartLetter = UCase(Trim(strStartLetter))
> If Len(strStartLetter) = 0 Then
> bDone = True
> Else
> If Len(strStartLetter) > 1 Then
> MsgBox "Enter a single letter" & _
> " (from A to Z or a to z)," & _
> " or blank to quit", vbOKOnly
> Else
> If strStartLetter < "A" _
> Or strStartLetter > "Z" Then
> MsgBox "Enter a (single) letter" & _
> " from A to Z or a to z," & _
> " or blank to quit", vbOKOnly
> Else
> bDone = True
> End If
> End If
> End If
> Loop Until bDone
>
> If strStartLetter <> "" Then
> ' Set this to the name of the worksheet or to the range name
> strSheetName = "Nominal Roll$"
> ' Set this to the exact name of the column containing the name
> ' (upper/lower case is probably significant
> strColumnName = "LastName"
>
> Set objMMMD = ActiveDocument
> With objMMMD
> For iLetter = Asc(strStartLetter) To Asc("Z")
> .MailMerge.DataSource.QueryString = _
> " SELECT * FROM [" & strSheetName & "]" & _
> " WHERE ucase(" & strColumnName & ") like '" & _
> Chr(iLetter) & "%'"
> With .MailMerge
> ' remove or change this as necessary
> .Destination = wdSendToNewDocument
> On Error GoTo norecords
> .Execute Pause:=False
> End With
> If MsgBox("Completed Letter " & Chr(iLetter) & _
> ". Do the next letter?", vbOKCancel) = vbCancel Then
> Exit For
> End If
> norecords:
> If Err.Number = 5631 Then
> ' Assume it is because there were no records for this letter
> ' Not necessarily true - could be just a badly formed query
> Err.Clear
> On Error GoTo 0
> Resume atloop
> Else
> ' just stop
> On Error GoTo 0
> End If
> atloop:
> Next
> End With
> Set objMMMD = Nothing
> End If
> End Sub
>
>
Back to top
Michael Koerner
External


Since: Mar 13, 2004
Posts: 2080



PostPosted: Sat Sep 19, 2009 11:47 am    Post subject: Re: Selecting Only One Record In A Merge [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Peter;

I could introduce a 5 digit (xxxxx) record number in Col A if that would
make things easier. That way it would cover any international addresses that
may pop up in the future.

--

Regards
Michael Koerner


"Peter Jamieson" <pjj.RemoveThis@KillMAPSpjjnet.demon.co.uk> wrote in message
news:OQ4RwnEOKHA.3412@TK2MSFTNGP06.phx.gbl...
Can the phone number only appear in one column in your data source, or
could it be in several? Do your numbers have non-numeric characters,
e.g. extension numbers introduced with "x", international "+" at the
beginning of the number, spaces, etc.?

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv

Michael Koerner wrote:
> I sometimes only need to print one envelope from my mail merge list.
> Thanks
> to Peter Jamieson who a wrote who a while ago wrote the following macro
> for
> me which allows me to select a starting letter for the merge. I was
> wondering if the macro (I have a hard time spelling macro) could be edited
> so that if I inserted a telephone number only that record would pop up in
> the envelope.
>
> Sub OneMergePerInitialLetterAskStart()
> ' error trapping to be added
> Dim bDone As Boolean
> Dim iLetter As Integer
> Dim objMMMD As Word.Document
> Dim strColumnName As String
> Dim strSheetName As String
> Dim strStartLetter As String
> bDone = False
> Do
> strStartLetter = InputBox("Enter the starting letter," & _
> " or blank to quit", "Starting letter", "a")
> strStartLetter = UCase(Trim(strStartLetter))
> If Len(strStartLetter) = 0 Then
> bDone = True
> Else
> If Len(strStartLetter) > 1 Then
> MsgBox "Enter a single letter" & _
> " (from A to Z or a to z)," & _
> " or blank to quit", vbOKOnly
> Else
> If strStartLetter < "A" _
> Or strStartLetter > "Z" Then
> MsgBox "Enter a (single) letter" & _
> " from A to Z or a to z," & _
> " or blank to quit", vbOKOnly
> Else
> bDone = True
> End If
> End If
> End If
> Loop Until bDone
>
> If strStartLetter <> "" Then
> ' Set this to the name of the worksheet or to the range name
> strSheetName = "Nominal Roll$"
> ' Set this to the exact name of the column containing the name
> ' (upper/lower case is probably significant
> strColumnName = "LastName"
>
> Set objMMMD = ActiveDocument
> With objMMMD
> For iLetter = Asc(strStartLetter) To Asc("Z")
> .MailMerge.DataSource.QueryString = _
> " SELECT * FROM [" & strSheetName & "]" & _
> " WHERE ucase(" & strColumnName & ") like '" & _
> Chr(iLetter) & "%'"
> With .MailMerge
> ' remove or change this as necessary
> .Destination = wdSendToNewDocument
> On Error GoTo norecords
> .Execute Pause:=False
> End With
> If MsgBox("Completed Letter " & Chr(iLetter) & _
> ". Do the next letter?", vbOKCancel) = vbCancel Then
> Exit For
> End If
> norecords:
> If Err.Number = 5631 Then
> ' Assume it is because there were no records for this letter
> ' Not necessarily true - could be just a badly formed query
> Err.Clear
> On Error GoTo 0
> Resume atloop
> Else
> ' just stop
> On Error GoTo 0
> End If
> atloop:
> Next
> End With
> Set objMMMD = Nothing
> End If
> End Sub
>
>
Back to top
Michael Koerner
External


Since: Mar 13, 2004
Posts: 2080



PostPosted: Sat Sep 19, 2009 2:36 pm    Post subject: Re: Selecting Only One Record In A Merge [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Plus a 5 digit number is easier to insert than a 10 digit telephone number
<g>

--

Regards
Michael Koerner


"Michael Koerner" <iamnot.DeleteThis@home.com> wrote in message
news:u9HJOCUOKHA.5488@TK2MSFTNGP02.phx.gbl...
Peter;

I could introduce a 5 digit (xxxxx) record number in Col A if that would
make things easier. That way it would cover any international addresses that
may pop up in the future.

--

Regards
Michael Koerner


"Peter Jamieson" <pjj.DeleteThis@KillMAPSpjjnet.demon.co.uk> wrote in message
news:OQ4RwnEOKHA.3412@TK2MSFTNGP06.phx.gbl...
Can the phone number only appear in one column in your data source, or
could it be in several? Do your numbers have non-numeric characters,
e.g. extension numbers introduced with "x", international "+" at the
beginning of the number, spaces, etc.?

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv

Michael Koerner wrote:
> I sometimes only need to print one envelope from my mail merge list.
> Thanks
> to Peter Jamieson who a wrote who a while ago wrote the following macro
> for
> me which allows me to select a starting letter for the merge. I was
> wondering if the macro (I have a hard time spelling macro) could be edited
> so that if I inserted a telephone number only that record would pop up in
> the envelope.
>
> Sub OneMergePerInitialLetterAskStart()
> ' error trapping to be added
> Dim bDone As Boolean
> Dim iLetter As Integer
> Dim objMMMD As Word.Document
> Dim strColumnName As String
> Dim strSheetName As String
> Dim strStartLetter As String
> bDone = False
> Do
> strStartLetter = InputBox("Enter the starting letter," & _
> " or blank to quit", "Starting letter", "a")
> strStartLetter = UCase(Trim(strStartLetter))
> If Len(strStartLetter) = 0 Then
> bDone = True
> Else
> If Len(strStartLetter) > 1 Then
> MsgBox "Enter a single letter" & _
> " (from A to Z or a to z)," & _
> " or blank to quit", vbOKOnly
> Else
> If strStartLetter < "A" _
> Or strStartLetter > "Z" Then
> MsgBox "Enter a (single) letter" & _
> " from A to Z or a to z," & _
> " or blank to quit", vbOKOnly
> Else
> bDone = True
> End If
> End If
> End If
> Loop Until bDone
>
> If strStartLetter <> "" Then
> ' Set this to the name of the worksheet or to the range name
> strSheetName = "Nominal Roll$"
> ' Set this to the exact name of the column containing the name
> ' (upper/lower case is probably significant
> strColumnName = "LastName"
>
> Set objMMMD = ActiveDocument
> With objMMMD
> For iLetter = Asc(strStartLetter) To Asc("Z")
> .MailMerge.DataSource.QueryString = _
> " SELECT * FROM [" & strSheetName & "]" & _
> " WHERE ucase(" & strColumnName & ") like '" & _
> Chr(iLetter) & "%'"
> With .MailMerge
> ' remove or change this as necessary
> .Destination = wdSendToNewDocument
> On Error GoTo norecords
> .Execute Pause:=False
> End With
> If MsgBox("Completed Letter " & Chr(iLetter) & _
> ". Do the next letter?", vbOKCancel) = vbCancel Then
> Exit For
> End If
> norecords:
> If Err.Number = 5631 Then
> ' Assume it is because there were no records for this letter
> ' Not necessarily true - could be just a badly formed query
> Err.Clear
> On Error GoTo 0
> Resume atloop
> Else
> ' just stop
> On Error GoTo 0
> End If
> atloop:
> Next
> End With
> Set objMMMD = Nothing
> End If
> End Sub
>
>
Back to top
Peter Jamieson
External


Since: Jan 15, 2009
Posts: 94



PostPosted: Sun Sep 20, 2009 6:10 am    Post subject: Re: Selecting Only One Record In A Merge [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

See how you get on with this (it's a completely separate process from
the last one)

FWIW I try to keep the amount of "coding to order" I do to an absolute
minimum. This is more than I've done for some time.

Sub SelectPhoneNumberAndMerge()
' error trapping to be added
Dim bDone As Boolean
Dim objMMMD As Word.Document
Dim strColumnName As String
Dim lngCount As Long
Dim strSheetName As String
Dim strPhoneNumber As String
bDone = False
strPhoneNumber = ""
Do
strPhoneNumber = InputBox("Enter the phone number -" & _
" only use 0-9, '-' and '+'," & _
" or blank to quit", _
"Phone number", strStartLetter)
strPhoneNumber = Replace(UCase(Trim(strPhoneNumber)), " ", "")
If Len(strPhoneNumber) = 0 Then
bDone = True
Else
If invalidPhoneNumber(strPhoneNumber) Then
MsgBox "Don't put anything except 0-9," & _
"'-' and '+' in the phone number", _
vbOKOnly
Else
' Set this to the name of the worksheet or to the range name
strSheetName = "Nominal Roll$"
' Set this to the exact name of the column containing the phone
number
' (upper/lower case is probably significant
strColumnName = "PhoneNumber"
Set objMMMD = ActiveDocument
With objMMMD.MailMerge
' I would like to use count(*) to get the
' record count but cannot make it work
.DataSource.QueryString = _
" SELECT *" & _
" FROM [" & strSheetName & "]" & _
" WHERE " & CStr(strColumnName) & " like '" & _
strPhoneNumber & "%'"
.DataSource.ActiveRecord = wdLastRecord
lngCount = .DataSource.ActiveRecord
End With
Set objMMMD = Nothing
Select Case lngCount
Case 0
MsgBox "No records matched the number you entered", vbOKOnly
Case 1 ' OK
bDone = True
Case Else
MsgBox "More than 1 record matched the number you entered",
vbOKOnly
End Select
End If
End If
Loop Until bDone

If strPhoneNumber <> "" Then
Set objMMMD = ActiveDocument
With objMMMD.MailMerge
' remove or change this as necessary
.Destination = wdSendToNewDocument
.Execute Pause:=False
End With
Set objMMMD = Nothing
End If

End Sub

Function invalidPhoneNumber(ByRef strPhone As String) As Boolean
' Shouldn't really return a parameter
' using byref in a function but
' there you go
Dim c As Long
Dim s As String
s = ""
invalidPhoneNumber = False
For c = 1 To Len(strPhone)
Select Case Mid(strPhone, c, 1)
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-"
s = s & Mid(strPhone, c, 1)
Case Else
invalidPhoneNumber = True
End Select
Next
strPhone = s
End Function




Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv

Michael Koerner wrote:
> Peter;
>
> The phone number is only in one data source which is in this case is Excel
> Col " O" with a header row called PhoneNumber and appear as xxx-xxx-xxxx
>
Back to top
Michael Koerner
External


Since: Mar 13, 2004
Posts: 2080



PostPosted: Sun Sep 20, 2009 10:18 am    Post subject: Re: Selecting Only One Record In A Merge [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Peter;

After correcting a couple word wraps from the cut and paste, I'm on bended
knees to you. Thank you very much, it works like a charm. Now all I need is
someone to help me get up off my knees. <g>

--

Regards
Michael Koerner


"Peter Jamieson" <pjj.RemoveThis@KillMAPSpjjnet.demon.co.uk> wrote in message
news:u24MxYdOKHA.1232@TK2MSFTNGP05.phx.gbl...
See how you get on with this (it's a completely separate process from
the last one)

FWIW I try to keep the amount of "coding to order" I do to an absolute
minimum. This is more than I've done for some time.

Sub SelectPhoneNumberAndMerge()
' error trapping to be added
Dim bDone As Boolean
Dim objMMMD As Word.Document
Dim strColumnName As String
Dim lngCount As Long
Dim strSheetName As String
Dim strPhoneNumber As String
bDone = False
strPhoneNumber = ""
Do
strPhoneNumber = InputBox("Enter the phone number -" & _
" only use 0-9, '-' and '+'," & _
" or blank to quit", _
"Phone number", strStartLetter)
strPhoneNumber = Replace(UCase(Trim(strPhoneNumber)), " ", "")
If Len(strPhoneNumber) = 0 Then
bDone = True
Else
If invalidPhoneNumber(strPhoneNumber) Then
MsgBox "Don't put anything except 0-9," & _
"'-' and '+' in the phone number", _
vbOKOnly
Else
' Set this to the name of the worksheet or to the range name
strSheetName = "Nominal Roll$"
' Set this to the exact name of the column containing the phone
number
' (upper/lower case is probably significant
strColumnName = "PhoneNumber"
Set objMMMD = ActiveDocument
With objMMMD.MailMerge
' I would like to use count(*) to get the
' record count but cannot make it work
.DataSource.QueryString = _
" SELECT *" & _
" FROM [" & strSheetName & "]" & _
" WHERE " & CStr(strColumnName) & " like '" & _
strPhoneNumber & "%'"
.DataSource.ActiveRecord = wdLastRecord
lngCount = .DataSource.ActiveRecord
End With
Set objMMMD = Nothing
Select Case lngCount
Case 0
MsgBox "No records matched the number you entered", vbOKOnly
Case 1 ' OK
bDone = True
Case Else
MsgBox "More than 1 record matched the number you entered",
vbOKOnly
End Select
End If
End If
Loop Until bDone

If strPhoneNumber <> "" Then
Set objMMMD = ActiveDocument
With objMMMD.MailMerge
' remove or change this as necessary
.Destination = wdSendToNewDocument
.Execute Pause:=False
End With
Set objMMMD = Nothing
End If

End Sub

Function invalidPhoneNumber(ByRef strPhone As String) As Boolean
' Shouldn't really return a parameter
' using byref in a function but
' there you go
Dim c As Long
Dim s As String
s = ""
invalidPhoneNumber = False
For c = 1 To Len(strPhone)
Select Case Mid(strPhone, c, 1)
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-"
s = s & Mid(strPhone, c, 1)
Case Else
invalidPhoneNumber = True
End Select
Next
strPhone = s
End Function




Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv

Michael Koerner wrote:
> Peter;
>
> The phone number is only in one data source which is in this case is Excel
> Col " O" with a header row called PhoneNumber and appear as xxx-xxx-xxxx
>
Back to top
Michael Koerner
External


Since: Mar 13, 2004
Posts: 2080



PostPosted: Sun Sep 20, 2009 2:46 pm    Post subject: Re: Selecting Only One Record In A Merge [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Peter I was a little quick to answer. I ran it again, and received the
following VB Compile Error "Ambiguous name detected: invalidPhoneNumber" and
the following opens up as highlighted

Function invalidPhoneNumber(ByRef strPhone As String) As Boolean

--

Regards
Michael Koerner


"Peter Jamieson" <pjj RemoveThis @KillMAPSpjjnet.demon.co.uk> wrote in message
news:u24MxYdOKHA.1232@TK2MSFTNGP05.phx.gbl...
See how you get on with this (it's a completely separate process from
the last one)

FWIW I try to keep the amount of "coding to order" I do to an absolute
minimum. This is more than I've done for some time.

Sub SelectPhoneNumberAndMerge()
' error trapping to be added
Dim bDone As Boolean
Dim objMMMD As Word.Document
Dim strColumnName As String
Dim lngCount As Long
Dim strSheetName As String
Dim strPhoneNumber As String
bDone = False
strPhoneNumber = ""
Do
strPhoneNumber = InputBox("Enter the phone number -" & _
" only use 0-9, '-' and '+'," & _
" or blank to quit", _
"Phone number", strStartLetter)
strPhoneNumber = Replace(UCase(Trim(strPhoneNumber)), " ", "")
If Len(strPhoneNumber) = 0 Then
bDone = True
Else
If invalidPhoneNumber(strPhoneNumber) Then
MsgBox "Don't put anything except 0-9," & _
"'-' and '+' in the phone number", _
vbOKOnly
Else
' Set this to the name of the worksheet or to the range name
strSheetName = "Nominal Roll$"
' Set this to the exact name of the column containing the phone
number
' (upper/lower case is probably significant
strColumnName = "PhoneNumber"
Set objMMMD = ActiveDocument
With objMMMD.MailMerge
' I would like to use count(*) to get the
' record count but cannot make it work
.DataSource.QueryString = _
" SELECT *" & _
" FROM [" & strSheetName & "]" & _
" WHERE " & CStr(strColumnName) & " like '" & _
strPhoneNumber & "%'"
.DataSource.ActiveRecord = wdLastRecord
lngCount = .DataSource.ActiveRecord
End With
Set objMMMD = Nothing
Select Case lngCount
Case 0
MsgBox "No records matched the number you entered", vbOKOnly
Case 1 ' OK
bDone = True
Case Else
MsgBox "More than 1 record matched the number you entered",
vbOKOnly
End Select
End If
End If
Loop Until bDone

If strPhoneNumber <> "" Then
Set objMMMD = ActiveDocument
With objMMMD.MailMerge
' remove or change this as necessary
.Destination = wdSendToNewDocument
.Execute Pause:=False
End With
Set objMMMD = Nothing
End If

End Sub

Function invalidPhoneNumber(ByRef strPhone As String) As Boolean
' Shouldn't really return a parameter
' using byref in a function but
' there you go
Dim c As Long
Dim s As String
s = ""
invalidPhoneNumber = False
For c = 1 To Len(strPhone)
Select Case Mid(strPhone, c, 1)
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-"
s = s & Mid(strPhone, c, 1)
Case Else
invalidPhoneNumber = True
End Select
Next
strPhone = s
End Function




Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv

Michael Koerner wrote:
> Peter;
>
> The phone number is only in one data source which is in this case is Excel
> Col " O" with a header row called PhoneNumber and appear as xxx-xxx-xxxx
>
Back to top
Peter Jamieson
External


Since: Jan 15, 2009
Posts: 94



PostPosted: Sun Sep 20, 2009 3:10 pm    Post subject: Re: Selecting Only One Record In A Merge [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

It's OK Michael, you can get up now Smile)

Seriously, glad it works.

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv

Michael Koerner wrote:
> Peter;
>
> After correcting a couple word wraps from the cut and paste, I'm on bended
> knees to you. Thank you very much, it works like a charm. Now all I need is
> someone to help me get up off my knees. <g>
>
Back to top
Peter Jamieson
External


Since: Jan 15, 2009
Posts: 94



PostPosted: Sun Sep 20, 2009 4:10 pm    Post subject: Re: Selecting Only One Record In A Merge [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I suspected it was too good to be true!

I don't suppose you have copied/pasted the macro into more than one
place, e.g. into a macro in the document, and a macro in the normal
template or an attached template?

FWIW, when faced with this kind of stuff I tend to
a. ensure that the active document is the one I intended it to be.
After you've run it once, the active document is often something else,
e.g. the output document from the previous merge. (probably wouldn't
cause this particular error though)
b. close Word, preferably make sure it's dead, start Word again, open
your mail merge main document, try again.


Peter Jamieson


http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv

Michael Koerner wrote:
> Peter I was a little quick to answer. I ran it again, and received the
> following VB Compile Error "Ambiguous name detected: invalidPhoneNumber" and
> the following opens up as highlighted
>
> Function invalidPhoneNumber(ByRef strPhone As String) As Boolean
>
Back to top
Michael Koerner
External


Since: Mar 13, 2004
Posts: 2080



PostPosted: Mon Sep 21, 2009 7:59 am    Post subject: Re: Selecting Only One Record In A Merge [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I looked, and it saved it as a docm file instead of a dotx. It works only I
get a macro alert message at the beginning where I have to allow the macro
to run.

--

Regards
Michael Koerner


"Peter Jamieson" <pjj.TakeThisOut@KillMAPSpjjnet.demon.co.uk> wrote in message
news:OwE$LgiOKHA.352@TK2MSFTNGP02.phx.gbl...
I suspected it was too good to be true!

I don't suppose you have copied/pasted the macro into more than one
place, e.g. into a macro in the document, and a macro in the normal
template or an attached template?

FWIW, when faced with this kind of stuff I tend to
a. ensure that the active document is the one I intended it to be.
After you've run it once, the active document is often something else,
e.g. the output document from the previous merge. (probably wouldn't
cause this particular error though)
b. close Word, preferably make sure it's dead, start Word again, open
your mail merge main document, try again.


Peter Jamieson


http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv

Michael Koerner wrote:
> Peter I was a little quick to answer. I ran it again, and received the
> following VB Compile Error "Ambiguous name detected: invalidPhoneNumber"
> and
> the following opens up as highlighted
>
> Function invalidPhoneNumber(ByRef strPhone As String) As Boolean
>
Back to top
Peter Jamieson
External


Since: Jan 15, 2009
Posts: 94



PostPosted: Mon Sep 21, 2009 11:10 am    Post subject: Re: Selecting Only One Record In A Merge [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You may be able to get rid of those by modifying the settings in Word
Office Button->Word Options>Trust Center->Trust Center Settings, then
->Macro Settings and/or Trusted Locations.


Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv

Michael Koerner wrote:
> I looked, and it saved it as a docm file instead of a dotx. It works only I
> get a macro alert message at the beginning where I have to allow the macro
> to run.
>
Back to top
Michael Koerner
External


Since: Mar 13, 2004
Posts: 2080



PostPosted: Tue Sep 22, 2009 8:09 am    Post subject: Re: Selecting Only One Record In A Merge [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Understood, as long as I allow all macros. Thanks

--

Regards
Michael Koerner


"Peter Jamieson" <pjj DeleteThis @KillMAPSpjjnet.demon.co.uk> wrote in message
news:%23K5KeSsOKHA.4700@TK2MSFTNGP05.phx.gbl...
You may be able to get rid of those by modifying the settings in Word
Office Button->Word Options>Trust Center->Trust Center Settings, then
->Macro Settings and/or Trusted Locations.


Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv

Michael Koerner wrote:
> I looked, and it saved it as a docm file instead of a dotx. It works only
> I
> get a macro alert message at the beginning where I have to allow the macro
> to run.
>
Back to top
Display posts from previous:   
Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> MailMerge Fields All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum