Solved

Mail Merge VB Script

Posted on 2014-04-04
6
1,041 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now