Solved

Mail Merge VB Script

Posted on 2014-04-04
6
1,083 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
question about excel functionality 3 32
Web Query 1 20
Need Help with Mapping and Toggling 3 21
I need help with an EXCEL Nested formula. 5 21
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

856 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