Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-17-2021, 03:35 AM
HealTheWord HealTheWord is offline Mailmerge WHERE filter Windows 10 Mailmerge WHERE filter Office 2013
Novice
Mailmerge WHERE filter
 
Join Date: Aug 2021
Posts: 2
HealTheWord is on a distinguished road
Default Mailmerge WHERE filter

Hello!
I have a mailmerge code that creates and saves automatically word files in a folder.
A part of the code is written below:


Quote:
Set MainDoc = ThisDocument
With MainDoc.MailMerge

.OpenDataSource Name:=DB_PATH, SQLStatement:="SELECT * FROM[TABLE NAME] WHERE [FIELD01] = 'VALUE OF INTEREST'"

totalRecord = .DataSource.RecordCount

For recordNumber = 1 To totalRecord



With .DataSource
.ActiveRecord = recordNumber
.FirstRecord = recordNumber
.LastRecord = recordNumber
End With


.Destination = wdSendToNewDocument
.Execute False

Set TargetDoc = ActiveDocument

TargetDoc.SaveAs2 DESTINAZIONE & .DataSource.DataFields("file_name").Value & ".docx", wdFormatDocumentDefault

TargetDoc.Close False

Set TargetDoc = Nothing

Next recordNumber

End With

Set MainDoc = Nothing


End Sub
It works enough well, but at the end of the macro execution code there is a debug error message because the function "totalRecord" reads the total number of the record exisisting in the table not the total number of record filtered by WHERE query.


How can i assign to a variable only the numbers of records existing after filtered them by a WHERE query? I read about a VBA "DCount" function but it's only works with ACCESS.


Thanks for your reply!
Reply With Quote
  #2  
Old 08-17-2021, 07:47 AM
HealTheWord HealTheWord is offline Mailmerge WHERE filter Windows 10 Mailmerge WHERE filter Office 2013
Novice
Mailmerge WHERE filter
 
Join Date: Aug 2021
Posts: 2
HealTheWord is on a distinguished road
Default

any idea?
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mailmerge WHERE filter Macro to insert an image to a word mailmerge document based on the value of a mailmerge field? Jake93 Mail Merge 3 07-02-2019 05:38 PM
Mailmerge WHERE filter Filter Mail Merge based on a list of filter criteria AusSteelMan Mail Merge 2 05-09-2016 03:35 PM
Mailmerge WHERE filter Mailmerge to Email with a mailmerge attachment Baldeagle Mail Merge 8 02-11-2013 04:40 AM
Mailmerge WHERE filter Mailmerge to Email with a mailmerge attachment Baldeagle Mail Merge 13 05-29-2012 02:04 PM
Mailmerge WHERE filter filter kwlickt Excel 1 03-28-2011 11:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:19 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft