K
Karel
Hi,
I have developed a vb.net application to create a mail-merged
document by
using Automation to word from VB.NET.
I added the following reference to the application:
Microsoft Office 10.0 Object Library
In runtime, when I click that button, I open a word doc. I create a
commandbar whith in that commandbar a combobox with a list of fields
to mailmerge.
When I select any value from the combobox,that value is added as
mailmerge field and is displayed on the word doc
This works perfect in Office XP but in Office 2000 you can't select a
field out of the list.
This is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim d1 As Word.Document
Dim dtcol As DataColumn
WordApp.Visible = True
WordApp.WindowState = Word.WdWindowState.wdWindowStateMaximize
d1 = WordApp.Documents.Open(FileName:=Application.StartupPath
& "\test.doc", Visible:=True)
d1.Activate()
Dim fld
Dim cbar1 As Office.CommandBar
Dim myControl As Office.CommandBarComboBox
cbar1 = d1.CommandBars.Add(Name:="Velden",
Position:=Office.MsoBarPosition.msoBarFloating, Temporary:=True)
cbar1.Visible = True
myControl = cbar1.Controls.Add(Type:=Office.MsoControlType.msoControlComboBox)
For Each dtcol In
lijstpersonen.oDataTable.DefaultView.Table.Columns
myControl.AddItem(dtcol.ColumnName)
Next
myControl.DropDownLines = 10
myControl.DropDownWidth = 150
myControl.ListHeaderCount = 0
ctlComboBoxHandler.SyncBox(myControl, d1)
End Sub
End Class
'Following class to handle change event of combobox of commandbar in
worddocument
Public Class ComboBoxHandler
Private d As Word.Document
Private WithEvents ComboBoxEvent As Office.CommandBarComboBox
Public Sub SyncBox(ByVal box As Office.CommandBarComboBox, ByVal
d1 As Word.Document)
ComboBoxEvent = box
d = d1
End Sub
Private Sub ComboBoxEvent_Change(ByVal Ctrl As
Office.CommandBarComboBox) Handles ComboBoxEvent.Change
Dim a As Word.Selection
a = d.ActiveWindow.Selection
d.MailMerge.Fields.Add(a.Range, Ctrl.Text)
a = Nothing
End Sub
Another problem I have is when I open the document the first time, it
works perfectly. I close it again, and then I open it for a second
time, I get a message that the RPC-server is not avalailable.
Can somebody help me with these two problems?
Thanx
Karel
I have developed a vb.net application to create a mail-merged
document by
using Automation to word from VB.NET.
I added the following reference to the application:
Microsoft Office 10.0 Object Library
In runtime, when I click that button, I open a word doc. I create a
commandbar whith in that commandbar a combobox with a list of fields
to mailmerge.
When I select any value from the combobox,that value is added as
mailmerge field and is displayed on the word doc
This works perfect in Office XP but in Office 2000 you can't select a
field out of the list.
This is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim d1 As Word.Document
Dim dtcol As DataColumn
WordApp.Visible = True
WordApp.WindowState = Word.WdWindowState.wdWindowStateMaximize
d1 = WordApp.Documents.Open(FileName:=Application.StartupPath
& "\test.doc", Visible:=True)
d1.Activate()
Dim fld
Dim cbar1 As Office.CommandBar
Dim myControl As Office.CommandBarComboBox
cbar1 = d1.CommandBars.Add(Name:="Velden",
Position:=Office.MsoBarPosition.msoBarFloating, Temporary:=True)
cbar1.Visible = True
myControl = cbar1.Controls.Add(Type:=Office.MsoControlType.msoControlComboBox)
For Each dtcol In
lijstpersonen.oDataTable.DefaultView.Table.Columns
myControl.AddItem(dtcol.ColumnName)
Next
myControl.DropDownLines = 10
myControl.DropDownWidth = 150
myControl.ListHeaderCount = 0
ctlComboBoxHandler.SyncBox(myControl, d1)
End Sub
End Class
'Following class to handle change event of combobox of commandbar in
worddocument
Public Class ComboBoxHandler
Private d As Word.Document
Private WithEvents ComboBoxEvent As Office.CommandBarComboBox
Public Sub SyncBox(ByVal box As Office.CommandBarComboBox, ByVal
d1 As Word.Document)
ComboBoxEvent = box
d = d1
End Sub
Private Sub ComboBoxEvent_Change(ByVal Ctrl As
Office.CommandBarComboBox) Handles ComboBoxEvent.Change
Dim a As Word.Selection
a = d.ActiveWindow.Selection
d.MailMerge.Fields.Add(a.Range, Ctrl.Text)
a = Nothing
End Sub
Another problem I have is when I open the document the first time, it
works perfectly. I close it again, and then I open it for a second
time, I get a message that the RPC-server is not avalailable.
Can somebody help me with these two problems?
Thanx
Karel