I'm creating a Word document which has several combobox controls in it. Apparently, when you embed a VB object into a document, there is no userform so I can't use userform_initialize and then use .additem to fill my comboboxes. Does anyone have any idea how to populate a combo box VB control that is embedded in a Word 2002 Document? Any help would be appreciated.
PS. Several have told me it would be easier to just use forms... I know this is easier, but the Word forms don't look as nice as the VB controls.
+ Reply to Thread
Results 1 to 5 of 5
-
-
G'day smearbrick1,
I'm assuming you want to populate the combo box when the document is opened.
Highlight the combo box and (making sure the Visual Basic toolbar is visible) open the Control Toolbox and click the View Code button.
Go to the Document_Open() sub and use the combo box .AddItem method to add your items.
e.g.
ComboBox1.AddItem "Item 1"
ComboBox1.AddItem "Item 2"
ComboBox1.AddItem "Item 3"
ComboBox1.AddItem "Item 4" ...etc.
Save the code and document, close the document and then re-open it and your combo box is populated.
You can change the combo box's properties by clicking the Design Mode button on the Control Toolbox, highlight the combo box and click the Properties button on the Control Toolbox.
Hope this helps,
Curlyween -
Thanks... that worked perfectly. I'm very new to all of this, and all of the tutorials I've read give no indication as to how you did this. In fact, the inhouse VB guru (some guru) said that it was impossible and that I would have to use the form functions built into word. I don't know if you've seen them (I'm assuming you have), but they are not as asthetically pleasing as the VB controls.
Once again, thanks for your help. -
One more question and I'm done...
Once again, I have embedded controls (checkboxes) in a document. I want to be able to modify these values from a userform I created (select an option button on the vb form and the check box on the document checks itself.) I've tried coding the option button to control the check box on the form, but I don't think I'm linking to the document from the VB form the way I need to. -
smearbrick1,
The trick to accessing a Word document (or any other Office application for that matter) is to set a reference to the Microsoft Word Object Library in the VB Project/References menu.
I suggest you read the MS KnowledgeBase article Q313193(http://support.microsoft.com/default.aspx?scid=kb;en-us;313193). It is a good start for how to access Word documents from VB.
The object library could be version 9.0, 10.0 or 11.0 depending on what version of Office you have.
Try this:
1. Create a document (Doc1.doc) and put three checkboxes on it (named CheckBox1, CheckBox2, CheckBox3).
2. In your VB app create three option buttons in a single array i.e. Option1(0), Option1(1), Option1(2) to use the default names.
3. Put the following code in the General Declarations:
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim docCheckbox1 As Object, docCheckbox2 As Object, docCheckbox3 As Object
4. In the Form_Load() sub put this code:
'Start Word and open the document template.
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set oDoc = oWord.Documents.Open("C:\Doc1.doc")
'Set references to the document's checkboxes
Set docCheckbox1 = oDoc.CheckBox1
Set docCheckbox2 = oDoc.CheckBox2
Set docCheckbox3 = oDoc.CheckBox3
5. In the Option1_Click(Index as Integer) sub put this code:
Select Case Index
Case 0
docCheckbox1.Value = True
docCheckbox2.Value = False
docCheckbox3.Value = False
Case 1
docCheckbox1.Value = False
docCheckbox2.Value = True
docCheckbox3.Value = False
Case 2
docCheckbox1.Value = False
docCheckbox2.Value = False
docCheckbox3.Value = True
End Select
6. If you want to save and close the Word document when you exit your VB app put this in the Form_Unload sub:
oDoc.Save
oDoc.Close
oWord.Quit
7. Run the app. You'll have to select your VB app from the taskbar (Word will be over the top of it probably). Clicking the option buttons in your app will change the values of the check boxes in the Word document.
That's it! You can access any Word object (VB control, graph, text etc.) from a VB app.
Best of luck (maybe you should direct your VB "guru" to the MS KnowledgeBase articles).
curlyween
Similar Threads
-
Calling MediaInfo from Visual Basic to et video info
By SearchQuality in forum ProgrammingReplies: 18Last Post: 2nd Nov 2011, 06:36 -
Basic video projector question
By jbaugh in forum Newbie / General discussionsReplies: 10Last Post: 14th Apr 2009, 01:58 -
visual basic code
By kudoshinichi in forum ProgrammingReplies: 1Last Post: 21st Jan 2009, 08:30 -
Programming FFMPEG (Visual Basic)
By Ace-Of-Spades in forum ProgrammingReplies: 4Last Post: 9th Oct 2007, 06:18