Solved

Mail Merge VB Script

Posted on 2014-04-04
6
1,003 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
Comment Utility
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
Comment Utility
Yeah still get the (Expected statement)  Error

QV_Script
0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 500 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Author Comment

by:Ross Turner
Comment Utility
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)
Comment Utility
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
Comment Utility
Awesome Awesome Awesome
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

744 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

15 Experts available now in Live!

Get 1:1 Help Now