Solved

Mail Merge VB Script

Posted on 2014-04-04
6
1,103 Views
Last Modified: 2014-04-06
HI EE,

I've created this vba script in excel which using an already defined/built spreadsheet and merges it with a policy.doc on my desktop.

This works lovely

Sub mergetest()

Set objword = CreateObject("Word.Application")
objword.Documents.Open ("C:\Users\ross turner.DOMAIN\Desktop\Policy.doc")
objword.Visible = True

With objword.ActiveDocument.MailMerge
 .OpenDataSource Name:="C:\Users\ross turner.DOMAIN\Desktop\mailmerge.xls", _
 ReadOnly:=True, _
 SQLStatement:="SELECT * FROM [Sheet1$]"
 .Execute
  'objword.ActiveDocument.PrintOut
  'objword.ActiveDocument.Quit
  objword.Quit (0)
End With

End Sub

Open in new window


Now here is my problem i need to do the same logic but for VB Script rather than VBA.

However ever time i try to run said script it throw up and error (Expected statement)  on the
.OpenDataSource Name:="C:\Users\ross turner.DOMAIN\Desktop\mailmerge.xls",

Help... i've been googling like a loon

Sub mergetest()

'First Get The Table Data
Set obj = ActiveDocument.GetSheetObject("TB08")
 
'Export Table Data  
obj.ExportBiff "C:\Users\ross turner.DOMAIN\Desktop\mailmerge.xls"
 
'Remove Data 
Set obj = nothing

'-----Everything above this line creates the spreasheet and works Tickety Boo-----


Set objword = CreateObject("Word.Application")
objword.Documents.Open ("C:\Users\ross turner.DOMAIN\Desktop\Policy.doc")
objword.Visible = True

With objword.ActiveDocument.MailMerge
 .OpenDataSource Name:="C:\Users\ross turner.DOMAIN\Desktop\mailmerge.xls", _
 ReadOnly:=True, _
 SQLStatement:="SELECT * FROM [Sheet1$]"
 .Execute
  objword.ActiveDocument.PrintOut
  objword.ActiveDocument.Quit
  objword.Quit (0)
End With


End Sub

Open in new window


Cheers Ross
0
Comment
Question by:Ross Turner
  • 3
  • 2
6 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39977718
Do you still get the error with following code?


Sub mergetest()

'First Get The Table Data
Set obj = ActiveDocument.GetSheetObject("TB08")
 
'Export Table Data  
obj.ExportBiff "C:\Users\ross turner.DOMAIN\Desktop\mailmerge.xls"
 
'Remove Data 
Set obj = nothing

'-----Everything above this line creates the spreasheet and works Tickety Boo-----


Set objword = CreateObject("Word.Application")
objword.Documents.Open ("C:\Users\ross turner.DOMAIN\Desktop\Policy.doc")
objword.Visible = True

objword.ActiveDocument.MailMerge.OpenDataSource Name:="C:\Users\ross turner.DOMAIN\Desktop\mailmerge.xls", ReadOnly:=True, SQLStatement:="SELECT * FROM [Sheet1$]"
objword.ActiveDocument.MailMerge.Execute 
objword.ActiveDocument.PrintOut  
objword.ActiveDocument.Quit 
objword.Quit (0)
End Sub

Open in new window

0
 
LVL 7

Author Comment

by:Ross Turner
ID: 39977736
Yeah still get the (Expected statement)  Error

QV_Script
0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 500 total points
ID: 39977907
appears the VBS version wont accept named parameters for the mailmerge.

this is only a partial solution as i cant test your mailmerge and its not something im familiar with... but the code runs and attempts to open the mailmerge.

  
Sub mergetest()

'-----Everything above this line creates the spreasheet and works Tickety Boo-----

	dim objWord, oDoc, wdName, xlName
	Set objword = CreateObject("Word.Application")

	wdName = "C:\Users\ross turner.DOMAIN\Desktop\Policy.doc"
	xlName = "C:\Users\ross turner.DOMAIN\Desktop\mailmerge.xls"
	
	set oDoc = objword.Documents.Open (wdName)
	objword.Visible = True

		 oDoc.MailMerge.OpenDataSource xlName, , , True, , , , , , , , , "SELECT * FROM [Sheet1$]"
		 oDoc.MailMerge.Execute

		  oDoc.PrintOut
	  oDoc.Close()
	  objword.Quit (0)

End Sub

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:Ross Turner
ID: 39978165
You are the man.... it does the job.... however i get a bit of a mong because it tries to close before it printed....i think i need to put a wait in between print and closing word
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 39979394
try something like...
  	  oDoc.PrintOut
	  
	  Waite 10
	  
	  oDoc.Close()
	  objword.Quit (0)



End Sub

Sub Waite(nSec)
	WScript.Sleep 1000*nSec
End Sub

Open in new window

0
 
LVL 7

Author Closing Comment

by:Ross Turner
ID: 39981087
Awesome Awesome Awesome
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question