0

我有这段 VB.NET 代码要转换为 VBScript。它基本上启动 Microsoft Word,显示打开对话框并邮件合并选定的文档。任何帮助将不胜感激。

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
4

2 回答 2

2

干得好。

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
于 2009-06-26T21:52:57.570 回答
0

这将在 VBscript 中打开文档:

Set wd = CreateObject("Word.Application")
Set obdoc = wd.Documents.Open("c:\test.doc")
于 2009-06-26T13:53:56.363 回答