jwbrooks99
New member
- Local time
- Today, 14:37
- Joined
- Mar 31, 2009
- Messages
- 7
I have been battling to automate a word mailmerge using SQLExpress. I cannot figure out what the path should be to the mailmerge datasource. I am completely new to VBA. I come from a legacy procedural environment and will really appreciate a detailed explanation on the topic. I managed to come up with the following code so far.
Code:
[FONT=Arial][SIZE=2][FONT=Arial]Dim objword As Object[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] Dim objdoc As Object[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] Set objword =CreateObject("Word.Application")[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] Set objdoc =objword.Documents.Open(strInputFileName)[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] Dim strDBPath As String[/FONT][/SIZE][/FONT]
[B][B][FONT=Arial][SIZE=2][COLOR=red][COLOR=red][FONT=Arial] strDBPath = xxxxxxxxxxxxxxxx[/FONT][/COLOR][/COLOR][/SIZE][/FONT][/B][/B]
[FONT=Arial][SIZE=2][FONT=Arial] Dim strTableName As String[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] strTableName="recordslettersvw"[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] MsgBox strDBPath[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] Withobjdoc.MailMerge[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] 'SetMerge Data Source[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] [COLOR=red][COLOR=red]objdoc[B][B][FONT=Arial][FONT=Arial].MailMerge.OpenDataSourcename:=strDBPath,_[/FONT][/FONT][/B][/B][/COLOR][/COLOR][/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] LinktoSource:=True,_[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] Connection:="TABLE"& strTableName, _[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] SQLStatement:="SELECT* FROM" & strTableName[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] .Destination= wdSendToNewDocument[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] .Execute[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] objword.ActiveDocument.PrintOutFalse[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] objword.ActiveDocument.ClosewdDoNotSaveChanges[/FONT][/SIZE][/FONT]
[FONT=Arial][SIZE=2][FONT=Arial] EndWith[/FONT][/SIZE][/FONT]