Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / December 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
srid - 08 Nov 2006 16:13 GMT
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.
Cindy M. - 23 Nov 2006 17:35 GMT
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 - 23 Nov 2006 20:11 GMT
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

> Hi,
> I am wrting a vb.net(2005) windows application with word(2003) mail merge
[quoted text clipped - 99 lines]
>
> thanks in advance.
srid - 23 Nov 2006 20:45 GMT
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}")

> thanks for asking cindy. still i am looking for help.
>
[quoted text clipped - 117 lines]
> >
> > thanks in advance.
srid - 24 Nov 2006 17:46 GMT
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

> here is my code. i am using string relace method
>
[quoted text clipped - 153 lines]
> > >
> > > thanks in advance.
Cindy M. - 25 Dec 2006 09:50 GMT
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 - 25 Dec 2006 18:04 GMT
thanks for the info. let me give it a try

> Hi =?Utf-8?B?c3JpZA==?=,
>
[quoted text clipped - 23 lines]
> This reply is posted in the Newsgroup; please post any follow question or
> reply in the newsgroup and not by e-mail :-)

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.