Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with commandbar controls...

Status
Not open for further replies.

nesplb

Programmer
Jul 29, 2003
109
NO
Hi!

I'm making a macro who's supposed to add custom controls to a commandbar via an input field. The string the user inputs in the field is supposed to be the caption of the control and the name of a new textstyle. Further when a user clicks this particular control it's supposed to write it's own caption in the right style.

I guess when a user clicks a commandbar control it have to send it's own id so that I can get it's caption and then select the style and write the text....

Is this possible? -and how?


here's my code so far:

Code:
For the userform:

Private Sub CommandButton1_Click()

'Here comes the text from the user input
ButtonInit(TextBox1.Text)

UserForm1.hide

End Sub



For the buttonInit function:

Function buttonInit(ByVal text As String)

ActiveDocument.Styles.Add ("Type_" + text)
ActiveDocument.Styles("type_" + text).Font.Bold = True
ActiveDocument.Styles("type_" + text).Font.Color = wdColorBlue

Dim buttonName

buttonName = "type_" + text

Set buttonName = CommandBars("My Menu").Controls.Add(Type:=msoControlButton)

With buttonName
.Caption = text
.OnAction = prosessText(<Here should the id of the control come I guess>)

End With

End Function

The prosessText function:

Function prosessText(byVal id as String)
dim text

text = Commanbars(&quot;My menu&quot;).controls(id).caption

Selection.TypeText Text:=text

Selection.Style = ActiveDocument.Styles(&quot;type_&quot; + text)
Selection.MoveRight Unit:=wdCharacter, Count:=1

End Function



Thanx!



Pål Nesteby

PDC-Tangen
Norway
 
When I've been faced with a similar problem, I've used the Windows registry (a simple text file would do, too) to store the information needed (here, it would be the names of the styles), and made the .onaction properties simply &quot;dothis1&quot;, &quot;dothis2&quot;, &quot;dothis3&quot;, etc., with in my module

sub dothis1
MyStyle=GetSetting(&quot;MyCustomStyles&quot;, Application.Name, &quot;1&quot;)
..do things with MyStyle...
end sub

If you have an unlimited number of entries, this becomes less desirable, but it's quite manageable for, say, up to twenty entries.


Rob
[flowerface]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top