how to create word CommandBarComboBox using vb.net(automate word)

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.
 
C

Cindy M.

Hi =?Utf-8?B?c3JpZA==?=,
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:
Are you still looking for help with this?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
S

srid

thanks for asking cindy. still i am looking for help.

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 #...)

Question:
1)What is the best way to create a CommandBarComboBox with data in vb.net?
2)there are 995 user created templates. merge fields are tagged as
{{fieldname}}. how to change them to word 2003 merge fields?
I tried to replace them with <<fieldname>> or {mergefield "fieldname"} but
word is not recognizing them as merge fiels.

if you can pls provide some details

thanks in advance
 
S

srid

here is my code. i am using string relace method

what i am doing is:
reading the document to clipboard and capturing as string and replacing the
character and saving the document

--------------------
Dim appdoc as New Word.Application
Dim appdoc As New Word.Document
appdoc = oApp.Documents.Add("C:\FirstName.doc")

Dim str2 As String

appdoc.ActiveWindow.Selection.WholeStory()
appdoc.ActiveWindow.Selection.Copy()

Dim data As IDataObject = Clipboard.GetDataObject()

str2 = data.GetData(DataFormats.Text).ToString

Dim str3 As String

str3 = str2.Replace("{{", "{MERGEFIELD ")
str2 = str3.Replace("}}", " \* MERGEFORMAT}")

appdoc.ActiveWindow.Selection.InsertBefore(str2)
appdoc.Save()

Results:
---------------
Fieldname str3
str2(final results)

{{Firstname}}---->{MERGEFIELD Firstname}}--->{MERGEFIELD Firstname \*
MERGEFORMAT}")
 
S

srid

I was able to write a method that finds the specified text and convert it to
mergefield

**still looking for help with combobox

------------>vb.net code

Dim oApp As New Word.ApplicationClass
Dim appdoc As New Word.Document

Public Sub ChangeTextToMergeField()
'open the document
appdoc = oApp.Documents.Add("C:\FirstName.doc")
oApp.Visible = True

Dim rng As Range = oApp.Selection.Range
'assign range to find
Dim fnd As Word.Find = rng.Find
'merge fields name
Dim fieldname() As String = {"FirstName", "LastName", "Addr1",
"Dear", "SalesAsso"}
Dim fName As String
Dim outerloop As Integer = 1
Dim Innerloop As Integer = 1

'pass the merge fields name
For Each fName In fieldname

'Clear existing formatting.
fnd.ClearFormatting()
'assign the string--{{fieldname}}
fnd.Text = "{{" & fName & "}}"
'seach throught out the document
fnd.Wrap = WdFindWrap.wdFindContinue

' Execute the search--->
Do While fnd.Execute
'slect the range
rng.Select()
'add the merge field
appdoc.MailMerge.Fields.Add(rng, fName)
'increment the counter--optional
Innerloop = Innerloop + 1
Loop

Innerloop = 1
'increment the counter--optional
outerloop = outerloop + 1

Next
'save the file
appdoc.SaveAs("c:\f1.doc")
oApp.Quit()

End Sub
 
C

Cindy M.

Hi =?Utf-8?B?c3JpZA==?=,
**still looking for help with combobox

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 #...)
It would help to see the code you're using to manage the combobox, but
right off-hand I'd say you're application needs to

1. Be sure to set the CustomizationContext when creating the combobox
2. Be sure to also delete the combobox, again setting the
CustomizationContext (to the same container as when creating it) before
issuing the command
3. It probably also wouldn't hurt to check for the presence of the
Combobox in the target toolbar before creating it again, deleting the
control if it's found.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
S

srid

thanks for the info. let me give it a try

Cindy M. said:
Hi =?Utf-8?B?c3JpZA==?=,

It would help to see the code you're using to manage the combobox, but
right off-hand I'd say you're application needs to

1. Be sure to set the CustomizationContext when creating the combobox
2. Be sure to also delete the combobox, again setting the
CustomizationContext (to the same container as when creating it) before
issuing the command
3. It probably also wouldn't hurt to check for the presence of the
Combobox in the target toolbar before creating it again, deleting the
control if it's found.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)


This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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