views:

608

answers:

2

I have this snippet of VB.NET code that I want to convert to VBScript. It basically starts Microsoft Word, displays the Open dialog and mail merges the selected document. Any help will be highly appreciated.

Dim oMissing As Object = System.Reflection.Missing.Value
Dim oEndOfDoc As Object = "\\endofdoc"
Dim oFalse As Object = False


'Start Word and create a new document.
Dim oWord As Word._Application
Dim oDoc As Word._Document

oWord = New Word.Application()
oWord.Visible = True

'show box
Dim dlg As Word.Dialog = oWord.Dialogs(Word.WdWordDialog.wdDialogFileOpen)
Dim dlgType As System.Type = GetType(Word.Dialog)

' Set the Name property of the dialog box.
dlgType.InvokeMember("Name", Reflection.BindingFlags.SetProperty Or  Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance, Nothing, dlg, New Object() {"C:\Documents and Settings\My Documents\MailMerge\"}, System.Globalization.CultureInfo.InvariantCulture)

Dim timeOut As Object = 0
Dim a As Int16 = dlg.Show(timeOut)

'if a document has been opened.
If (a = -1) Then

    oDoc = oWord.ActiveDocument
    oDoc.Select()
    oDoc.MailMerge.OpenDataSource("C:\\usr\\mergequery.txt", oMissing, oMissing,  oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing)

    oDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
    oDoc.MailMerge.Execute(oFalse)

    'Close the original form document.
    oDoc.Saved = True
    oDoc.Close(oFalse, oMissing, oMissing)
End If
A: 

this is will opend the doc in vbscript.

Set wd = CreateObject("Word.Application") Set obdoc = wd.Documents.Open("c:\test.doc")

sorry i can't do more break is over

Anthony
+2  A: 

Here you go.

Option Explicit

Dim wdDialogFileOpen, wdSendToNewDocument
wdDialogFileOpen = 80
wdSendToNewDocument = 0

Dim oEndOfDoc 
oEndOfDoc = "\\endofdoc"

Dim oWord, oDoc
Set oWord = CreateObject("Word.Application")
oWord.Visible = True

Dim dlg
Set dlg = oWord.Dialogs(wdDialogFileOpen)

dlg.Name = "C:\Documents and Settings\My Documents\MailMerge\"

Dim a
a = dlg.Show(0)

'if a document has been opened.
If (a = -1) Then
    Set oDoc = oWord.ActiveDocument
    oDoc.Select
    oDoc.MailMerge.OpenDataSource "C:\usr\mergequery.txt"

    oDoc.MailMerge.Destination = wdSendToNewDocument
    oDoc.MailMerge.Execute False

    'Close the original form document.
    oDoc.Saved = True
    oDoc.Close False
End If
Tmdean