By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

How to put For Each loop in a With block?

P: n/a
Problem: why doesn't this With block work? is it possible to have a For
Each loop within a With block?

With objWord
.Documents.Add Template:=strTemplate, NewTemplate:=False,
DocumentType:=0
For Each varBookmark In Array("ReturnAddress",
"NameAddressBlock", "Salutation", "FullName")
If DLookup(varBookmark, "tblTemplate", "template_ID = " &
strTid) = -1 Then
.Selection.GoTo what:=wdGoToBookmark, Name:=varBookmark
.Selection.TypeText Text:=varBookmark
End If
.Application.NormalTemplate.Saved = True
End With

rsOutput.MoveNext
Loop

The purpose of this code is to allow the user to select from a series of
predefined Word Templates and corresponding Bookmarks when running a Word
Mail Merge. The predefined Bookmarks are in Array varBookmark

The user-selected Bookmarks are saved in tblTemplate as yes/no datatype.
So, if the user selects a particular bookmark, it has a value of -1.

The For Each loop checks the user's selections... but it seems the For Each
loop screws up the With block...

Am I missing something, or is it not possible to have a For Each loop within
a With block??
suggestions/comments welcome!!

Thanks!!
Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Perhaps I could use Resume Next on error 5101 ("Bookmark does not exist")
and not need the for each loop?

"deko" <dj****@hotmail.com> wrote in message
news:wP******************@newssvr25.news.prodigy.c om...
Problem: why doesn't this With block work? is it possible to have a For
Each loop within a With block?

With objWord
.Documents.Add Template:=strTemplate, NewTemplate:=False,
DocumentType:=0
For Each varBookmark In Array("ReturnAddress",
"NameAddressBlock", "Salutation", "FullName")
If DLookup(varBookmark, "tblTemplate", "template_ID = " &
strTid) = -1 Then
.Selection.GoTo what:=wdGoToBookmark, Name:=varBookmark .Selection.TypeText Text:=varBookmark
End If
.Application.NormalTemplate.Saved = True
End With

rsOutput.MoveNext
Loop

The purpose of this code is to allow the user to select from a series of
predefined Word Templates and corresponding Bookmarks when running a Word
Mail Merge. The predefined Bookmarks are in Array varBookmark

The user-selected Bookmarks are saved in tblTemplate as yes/no datatype.
So, if the user selects a particular bookmark, it has a value of -1.

The For Each loop checks the user's selections... but it seems the For Each loop screws up the With block...

Am I missing something, or is it not possible to have a For Each loop within a With block??
suggestions/comments welcome!!

Thanks!!

Nov 12 '05 #2

P: n/a
Where's the Next statement corresponding to your For Each statement? It
doesn't appear before the End With.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(No private e-mails, please)

"deko" <dj****@hotmail.com> wrote in message
news:wP******************@newssvr25.news.prodigy.c om...
Problem: why doesn't this With block work? is it possible to have a For
Each loop within a With block?

With objWord
.Documents.Add Template:=strTemplate, NewTemplate:=False,
DocumentType:=0
For Each varBookmark In Array("ReturnAddress",
"NameAddressBlock", "Salutation", "FullName")
If DLookup(varBookmark, "tblTemplate", "template_ID = " &
strTid) = -1 Then
.Selection.GoTo what:=wdGoToBookmark, Name:=varBookmark .Selection.TypeText Text:=varBookmark
End If
.Application.NormalTemplate.Saved = True
End With

rsOutput.MoveNext
Loop

The purpose of this code is to allow the user to select from a series of
predefined Word Templates and corresponding Bookmarks when running a Word
Mail Merge. The predefined Bookmarks are in Array varBookmark

The user-selected Bookmarks are saved in tblTemplate as yes/no datatype.
So, if the user selects a particular bookmark, it has a value of -1.

The For Each loop checks the user's selections... but it seems the For Each loop screws up the With block...

Am I missing something, or is it not possible to have a For Each loop within a With block??
suggestions/comments welcome!!

Thanks!!

Nov 12 '05 #3

P: n/a
Hi and thanks for the reply. Yes, that "For Each" statement needs some
help.

But I think this will do the trick:

With objWord
On Error Resume Next 'skip over bookmarks that don't exist in
selected template
.Documents.Add Template:=strTemplate, NewTemplate:=False,
DocumentType:=0
.Selection.GoTo what:=wdGoToBookmark, Name:="FullName"
.Selection.TypeText Text:=strFullName
.Selection.GoTo what:=wdGoToBookmark, Name:="NameAddressBlock"
.Selection.TypeText Text:=strNameAddressBlock
-- and so on --
.Application.NormalTemplate.Saved = True
End With
On Error GoTo 0

The Mail Merge solution I'm creating will give the user a number of
predefined Bookmark "handles" which can be used in Word Templates. I'm
leaving it up to the user to create the Word Template and name the Bookmarks
in the Template according to the name of the predefined handles.

Here are some of the handles:

ReturnAddress
FirstName
LastName
FullName
etc...
etc...

Each handle will have code behind it, for example:

strReturnAddress = IIf(IsNull(rsReturnAddress!RtName), "",
rsReturnAddress!RtName & " ") & _
IIf(IsNull(rsReturnAddress!RtAddress1), "", Chr(13) & Chr(10) &
Chr(9) & Chr(9) & rsReturnAddress!RtAddress1) & _
IIf(IsNull(rsReturnAddress!RtAddress2), "", Chr(13) & Chr(10) &
Chr(9) & Chr(9) & rsReturnAddress!RtAddress2) & _
IIf(IsNull(rsReturnAddress!RtCity), "", Chr(13) & Chr(10) & Chr(9)
& Chr(9) & rsReturnAddress!RtCity) & _
IIf(IsNull(rsReturnAddress!RtState), "", ", " &
rsReturnAddress!RtState) & _
IIf(IsNull(rsReturnAddress!RtZipCode), "", " " &
rsReturnAddress!RtZipCode)

So the user creates his own Template(s) using Bookmarks named after the
predefined handles, and viola! -- instant mail merge.

I'll need to loop through the records the for the merge, and will be working
on a "For Each... Next" loop for this in the coming days.

Using the "On Error Resume Next" trick, I won't need to loop through the
BookMark handles (as previously thought).

If you have any suggestions or comments, please let me know...

Thanks again for the reply!

"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message
news:OA********************@twister01.bloor.is.net .cable.rogers.com...
Where's the Next statement corresponding to your For Each statement? It
doesn't appear before the End With.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(No private e-mails, please)

"deko" <dj****@hotmail.com> wrote in message
news:wP******************@newssvr25.news.prodigy.c om...
Problem: why doesn't this With block work? is it possible to have a For
Each loop within a With block?

With objWord
.Documents.Add Template:=strTemplate, NewTemplate:=False,
DocumentType:=0
For Each varBookmark In Array("ReturnAddress",
"NameAddressBlock", "Salutation", "FullName")
If DLookup(varBookmark, "tblTemplate", "template_ID = " & strTid) = -1 Then
.Selection.GoTo what:=wdGoToBookmark,

Name:=varBookmark
.Selection.TypeText Text:=varBookmark
End If
.Application.NormalTemplate.Saved = True
End With

rsOutput.MoveNext
Loop

The purpose of this code is to allow the user to select from a series of
predefined Word Templates and corresponding Bookmarks when running a Word Mail Merge. The predefined Bookmarks are in Array varBookmark

The user-selected Bookmarks are saved in tblTemplate as yes/no datatype.
So, if the user selects a particular bookmark, it has a value of -1.

The For Each loop checks the user's selections... but it seems the For

Each
loop screws up the With block...

Am I missing something, or is it not possible to have a For Each loop

within
a With block??
suggestions/comments welcome!!

Thanks!!


Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.