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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using combo box on MS word 2003 and visual basic editor

Status
Not open for further replies.

14adrian

Technical User
Nov 9, 2011
2
US
Hello, I am new to using the visual basic editor. I was able to create a word document with combo boxes. When I run the combo boxes, they run fine. When I save the document and re-open it with visual basic editor closed, the document doesn't show the values for the combo boxes on the drop-down list. The combo boxes are blank. Again, they DO work when I "run" them. I don't know how to save the document so that the combo-boxes can be used by other people when I save it. This is what I have for the combo boxes:

Sub TypeOfReport()
With ActiveDocument.ComboBox1
.Clear
.AddItem "Near-miss"
.AddItem "1st Aid"
.AddItem "Injury"
.AddItem "Non-work related"
End With
End Sub
Sub Sex()
With ActiveDocument.ComboBox2
.Clear
.AddItem "Female"
.AddItem "Male"
End With
End Sub
Sub Department()
With ActiveDocument.ComboBox3
.Clear
.AddItem "2nd Shift Assembly"
.AddItem "2nd Shift Mass Finish"
.AddItem "2nd Shift Polishing"
.AddItem "2nd Shift QA"
.AddItem "2nd Shift Ring Cell"
.AddItem "2nd Shift Stamp & Strike"
.AddItem "2nd Shift Tooling"
.AddItem "2nd Shift Waxing"
.AddItem "Assembly"
.AddItem "Casting"
.AddItem "DBY"
.AddItem "Engineering"
.AddItem "Facilities"
.AddItem "Inventory Control"
.AddItem "Mass Finish"
.AddItem "Polishing"
.AddItem "QA"
.AddItem "Ring Cell"
.AddItem "SDR"
.AddItem "Security"
.AddItem "Shipping & Receiving"
.AddItem "Stamp & Strike"
.AddItem "Studio"
.AddItem "Tooling"
.AddItem "Waxing/Molding"
.AddItem "Wrapping"
End With
End Sub

Any help greatly appreciated. Thank you.

 
How are ya 14adrian . . .
14adrian said:
[blue]When I save the document and re-open it with visual basic editor closed, [purple]the document doesn't show the values for the combo boxes[/purple] on the drop-down list.[/blue]
The combobox listings don't show becuse your not calling your subroutines. You need to use one of the doc [blue]events[/blue] to do this. Here are the steps:
[ol][li]Open the VBA editor and backup your code using NotePad. Then delete the code ... stay in the VBA editor and make sure the [blue]Project Explorer[/blue] window is open [blue]MenuBar[/blue] - [blue]View[/blue] - [blue]Project Explorer[/blue].[/li]
[li]Add a module to the project [blue]MenuBar[/blue] - [blue]Insert[/blue] - [blue]Module[/blue].[/li]
[li]In the [blue]code window[/blue] on the right, under [blue]Option Explicit[/blue], copy/paste the following:
Code:
[blue]Public Sub cbxInitialize()
   [green]'Type of Report[/green]
   With ActiveDocument.ComboBox1
      .Clear
      .AddItem "Near-miss"
      .AddItem "1st Aid"
      .AddItem "Injury"
      .AddItem "Non-work related"
   End With
   
   [green]'Sex[/green]
   With ActiveDocument.ComboBox2
      .Clear
      .AddItem "Female"
      .AddItem "Male"
   End With
   
   [green]'Department[/green]
   With ActiveDocument.ComboBox3
      .Clear
      .AddItem "2nd Shift Assembly"
      .AddItem "2nd Shift Mass Finish"
      .AddItem "2nd Shift Polishing"
      .AddItem "2nd Shift QA"
      .AddItem "2nd Shift Ring Cell"
      .AddItem "2nd Shift Stamp & Strike"
      .AddItem "2nd Shift Tooling"
      .AddItem "2nd Shift Waxing"
      .AddItem "Assembly"
      .AddItem "Casting"
      .AddItem "DBY"
      .AddItem "Engineering"
      .AddItem "Facilities"
      .AddItem "Inventory Control"
      .AddItem "Mass Finish"
      .AddItem "Polishing"
      .AddItem "QA"
      .AddItem "Ring Cell"
      .AddItem "SDR"
      .AddItem "Security"
      .AddItem "Shipping & Receiving"
      .AddItem "Stamp & Strike"
      .AddItem "Studio"
      .AddItem "Tooling"
      .AddItem "Waxing/Molding"
      .AddItem "Wrapping"
   End With

End Sub[/blue]
Now initializing the comboboxes is available from anywhere in the doc.[/li]
[li]Your going to use the [blue]Document_Open[/blue] event of the [blue]ThisDocument[/blue] word object to trigger and run the [blue]cbxInitialize[/blue] routine when the doc opens. So double-click on the [blue]ThisDocument[/blue] object in the [blue]Project[/blue] window.[/li]
[li]In the code window, from the combobox on the left select [blue]Document[/blue]. Then from the right combobox select [blue]Open[/blue]. Now copy/paste the following in the [blue]Document_Open[/blue] routine:
Code:
[blue]   Call cbxInitialize[/blue]
When your done it should look like:
Code:
[blue]Private Sub Document_Open()
   Call cbxInitialize
End Sub[/blue]
[/li]
[li]Hit the [blue]Save[/blue] button then [blue]Alt+Q[/blue] to return to the doc.[/li]
[li]Thats it! ... close then reopen the doc to test.[/li][/ol]
[blue]I don't know how to save the document so that the combo-boxes can be used by other people when I save it.[/blue]
Assuming that most users will have security set to [blue]high[/blue] there's two things you can do:
[ol][li]The document can be [blue]digitally signed[/blue] (I don't remember if [blue]self signing[/blue] will work for this).[/li]
[li]Inform receivers of the doc that they need to set their [blue]Security Level[/blue] to [purple]Medium[/purple] and to click [blue]Enable Macros[/blue] when the security warning prompt appears.[/li][/ol]

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top