CommandBarComboBox

S

srid

Hi,
I am wrting a vb.net(2005) windows application with word(2003) mail merge
features. When user clicks add new template button in my windows application,
the button click events fires and opens my word template. this part is
working.

To do:
user will see a new CommandBarComboBox filled with merge fields. these merge
fields are coming from sql server 2000. when user save and close the word
application the new commandBarCombobox should get deleted from word tool bar.

PROBLEMS:
1)when I close or open the word from my application getting security
warninng to save the changes to normal.dot which I don't want to do
2)if i open word from my application ten time,there are ten
CommandBarComboBoxes on the word toolbar
3)even if i open the word seperately(not via vb.net) still i see newly
created CommandBarComboBoxes(10 or #...)
4)when user open the word template for the second time from my application,
i am getting a com error at:
ocombo = oCommandBar.Controls.Add(MsoControlType.msoControlComboBox
I think that i am getting above error as this code is fired when i open the
form not every time i click the cmdAddNewTemplate_Click->

Dim WithEvents ocombo As CommandBarComboBox


Question:
1)What is the best way to create a CommandBarComboBox?
2)How to stop the security warnings?
3)how to solve above problems(4)?

Here is my code:


Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Word
Imports System.Runtime.InteropServices 'capture the COM errors
Imports Microsoft.Office.Core

'class
public Class Mailmerge

Dim WithEvents ocombo As CommandBarComboBox
Dim WithEvents wordApp As Word.Application

'click event
Private Sub cmdAddNewTemplate_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdAddNewTemplate.Click

'define CommandBarControl
'Dim comBarControl As CommandBarControl = Nothing
'define office commandbar
Dim oCommandBar As CommandBar = Nothing

'ocombo = Core.CommandBarComboBox
wordApp = New Word.Application
Dim oMainDoc As New Word.Document

'Start a new main document for mail merge ;assign the word template
path
oMainDoc = wordApp.Documents.Add("C:\word Template\mytemplate.frm")
wordApp.Visible = True

' Create a new command bar.
ocombo = oCommandBar.Controls.Add(MsoControlType.msoControlComboBox)

' Add items to the combo box.

ocombo.AddItem("FirstName")

ocombo.AddItem("LastName")

ocombo.AddItem("Dear")

' Set the caption and style.

ocombo.Caption = "Select the merge fields:"

ocombo.Style = MsoComboStyle.msoComboLabel

' Show the command bar to the user.
oCommandBar.Visible = True

'AddWordToolbar()
'wordApp = Nothing

End Sub

'combobox change event
'insert merge field
Private Sub oCombo_Change(ByVal Ctrl As CommandBarComboBox) Handles
ocombo.Change

Dim wrdSelection As Word.Selection = wordApp.Application.Selection

wordApp.ActiveDocument.MailMerge.Fields.Add(wrdSelection.Range,
Name:=ocombo.Text)

End Sub

end class
'===

thanks in advance.
 
D

Doug Robbins - Word MVP

Instead of using a CommandBarComboBox, have the code in your template open a
UserForm that contains a list box that is populated with the mergefields.

Such a userform is used in the addin that you can download from:

http://www.gmayor.com/individual_merge_letters.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

srid

thanks for the info.
let me give it a try
Doug Robbins - Word MVP said:
Instead of using a CommandBarComboBox, have the code in your template open a
UserForm that contains a list box that is populated with the mergefields.

Such a userform is used in the addin that you can download from:

http://www.gmayor.com/individual_merge_letters.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top