PDA

View Full Version : Solved: Combo Box problems



ABrown
04-24-2008, 03:54 AM
I have a combo box that needs to populate with all the styles in use in a word document. I have the code working that takes the styles in use and lists them in a message box, but.... when I additem to the combo box as per the code below the list is not a list but a straight line! Any help would be much appreciated - this is driving me insane! I have rem'd out the msgbox code but if you run it with this in you will get a list of styles in use in a list in the message box.

the code is very simple but just doesn't quite work!:


Private Sub Cmb1_Enter()
Dim docActive As Document
Dim strMessage As String
Dim styleLoop As Style
Set docActive = ActiveDocument
strMessage = ""
For Each styleLoop In docActive.Styles
If styleLoop.InUse = True Then

With docActive.Content.Find
.ClearFormatting
.Text = ""
.Style = styleLoop
.Execute Format:=True
If .Found = True Then

strMessage = strMessage & styleLoop.NameLocal & vbNewLine
End If
End With
End If

Next styleLoop
Cmb1.AddItem (strMessage)
'MsgBox (strMessage)



Thanks again

Annette

Tommy
04-24-2008, 08:23 AM
Hi :hi:

Why not add the style name when you find it?


Private Sub Cmb1_Enter()
Dim docActive As Document
'Dim strMessage As String
Dim styleLoop As Style
Set docActive = ActiveDocument
'strMessage = ""
For Each styleLoop In docActive.Styles
If styleLoop.InUse = True Then
With docActive.Content.Find
.ClearFormatting
.Text = ""
.Style = styleLoop
.Execute Format:=True
If .Found = True Then
Cmb1.AddItem (styleLoop.NameLocal)
End If
End With
End If
Next styleLoop
'Cmb1.AddItem (strMessage)
'MsgBox (strMessage)

ABrown
04-24-2008, 09:00 AM
Tommy you are a star :beerchug:

Thanks soo much that works perfectly!!

next question if I may??

I need to populate 10 combo boxes with the same code - is there any way I can make the combobox a variable and loop through doing all the same to each one?

Thanks again for all your help.

Annette

Tommy
04-24-2008, 09:07 AM
Public Sub Cmb1_Enter(Cmb1 as combobox)



Usage:


Cmb1_Enter ComboBox1

ABrown
04-24-2008, 09:19 AM
Sorry Tommy - when I try that it doesn't work - I am obviously doing something wrong.....:motz2:

I know this is probably basic but am very new to VB.

Thanks.

Annette

Tommy
04-24-2008, 09:24 AM
Show me how you are using it please :)

I would also need the name of the combobox

Thanks

ABrown
04-24-2008, 09:30 AM
The Combo Boxes are called Cmb1, Cmb2, Cmb3 through to Cmb10

They all need to be populated with the styles of the current document.

The code is looking like this:


Public Sub Cmb1_Enter(Cmb1 As ComboBox)
Dim docActive As Document
Dim styleLoop As Style
Dim objCmd As ComboBox

Set docActive = ActiveDocument
For Each styleLoop In docActive.Styles
If styleLoop.InUse = True Then
With docActive.Content.Find
.ClearFormatting
.Text = ""
.Style = styleLoop
.Execute Format:=True
If .Found = True Then
Cmb1.AddItem (styleLoop.NameLocal)
End If
End With
End If
Next styleLoop
End Sub


Private Sub Cmb2_Enter()
Cmb1_Enter Cmb2
End Sub


This is the error message:

Procedure declaration does not match description of event or procedure having the same name

Thanks

Annette:banghead:

Tommy
04-24-2008, 09:33 AM
:mkay I was using the name of a Combo Box My Bad



Public Sub Combs(Comb As ComboBox)

Dim docActive As Document
Dim styleLoop As Style
Dim objCmd As ComboBox


Set docActive = ActiveDocument
For Each styleLoop In docActive.Styles
If styleLoop.InUse = True Then
With docActive.Content.Find
.ClearFormatting
.Text = ""
.Style = styleLoop
.Execute Format:=True
If .Found = True Then
Comb.AddItem (styleLoop.NameLocal)
End If
End With
End If
Next styleLoop
End Sub



Private Sub Cmb2_Enter()
Combs Cmb2
End Sub

ABrown
04-24-2008, 10:03 AM
Tommy - you are truly wonderful - I can go home now!!!:bow:

Thanks.

Annette