Solved

Mail Merge VB Script

Posted on 2014-04-04
6
1,063 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

777 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