Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
mkz mkz is offline
Junior Member
 
Posts: 3
Default Problems with VBA mailmerge

Hi,

I am having some problems getting a VBA to work. It's supposed to open up a mailmerge template and fill it in from an excel sheet, which it has already been linked to, then print the sheets. Here's my code:


Dim wd As Object
Dim wdocSource As Object

Dim strWorkbookName As String

On Error Resume Next
Set wd = GetObject(, "Word.Application")
If wd Is Nothing Then
Set wd = CreateObject("Word.Application")
End If
On Error GoTo 0

Set wdocSource = wd.Documents.Open("X:\y\z\a.doc")

strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name

wdocSource.MailMerge.MainDocumentType = wdFormLetters

wdocSource.MailMerge.OpenDataSource _
Name:=strWorkbookName, _
AddToRecentFiles:=False, _
Revert:=False, _
Format:=wdOpenFormatAuto, _
Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
SQLStatement:="SELECT * FROM `sheet$` WHERE `Method`='xyz' And `Amount:` 0"

With wdocSource.MailMerge
.Destination = wdSendToPrinter
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With

wd.Visible = True
wdocSource.Close SaveChanges:=False

Set wdocSource = Nothing
Set wd = Nothing

End Sub


I get the error "Run-Time error 4198: command failed." When i click debug it takes me to this section:

wdocSource.MailMerge.OpenDataSource _
Name:=strWorkbookName, _
AddToRecentFiles:=False, _
Revert:=False, _
Format:=wdOpenFormatAuto, _
Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
SQLStatement:="SELECT * FROM `sheet$` WHERE `Method`='xyz' And `Amount:` 0"


any ideas what could be wrong? I used this macro in another spreadsheet and it worked just fine.

Thanks so much!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 147
Default Problems with VBA mailmerge

mkz wrote:

Connection:="Data Source=" & strWorkbookName & ";Mode=Read",



I did not try that but I am not sure if that is valid connection string.
Try to google some examples or setup connection manually and read
connection property.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mailmerge zip codes Ronnie Excel Discussion (Misc queries) 2 February 28th 08 07:11 PM
Mailmerge and Charts JJD Charts and Charting in Excel 0 September 26th 07 10:46 AM
Word/Excel Mailmerge problems havenlad Excel Discussion (Misc queries) 3 July 11th 06 03:45 PM
mailmerge frank Excel Discussion (Misc queries) 1 February 24th 05 07:55 PM
Mailmerge in Excel? beginner Excel Programming 1 April 9th 04 05:44 PM


All times are GMT +1. The time now is 11:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"