PDA

View Full Version : how to create word CommandBarComboBox using vb.net(automate word)


srid
November 8th 06, 04:13 PM
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.msoControl ComboBox
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.msoControl ComboBox)

' 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(wrdSel ection.Range,
Name:=ocombo.Text)

End Sub

end class
'===

thanks in advance.

Cindy M.
November 23rd 06, 05:35 PM
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 :-)

srid
November 23rd 06, 08:11 PM
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



"srid" wrote:

> 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.msoControl ComboBox
> 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.msoControl ComboBox)
>
> ' 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(wrdSel ection.Range,
> Name:=ocombo.Text)
>
> End Sub
>
> end class
> '===
>
> thanks in advance.
>
>

srid
November 23rd 06, 08:45 PM
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}")


"srid" wrote:

> 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
>
>
>
> "srid" wrote:
>
> > 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.msoControl ComboBox
> > 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.msoControl ComboBox)
> >
> > ' 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(wrdSel ection.Range,
> > Name:=ocombo.Text)
> >
> > End Sub
> >
> > end class
> > '===
> >
> > thanks in advance.
> >
> >

srid
November 24th 06, 05:46 PM
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


"srid" wrote:

> 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}")
>
>
> "srid" wrote:
>
> > 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
> >
> >
> >
> > "srid" wrote:
> >
> > > 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.msoControl ComboBox
> > > 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.msoControl ComboBox)
> > >
> > > ' 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(wrdSel ection.Range,
> > > Name:=ocombo.Text)
> > >
> > > End Sub
> > >
> > > end class
> > > '===
> > >
> > > thanks in advance.
> > >
> > >

Cindy M.
December 25th 06, 09:50 AM
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 :-)

srid
December 25th 06, 06:04 PM
thanks for the info. let me give it a try

"Cindy M." wrote:

> 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 :-)
>
>

Google