VideoHelp Forum




+ Reply to Thread
Results 1 to 5 of 5
  1. Member
    Join Date
    Jul 2002
    Location
    Up in yo' bitch.
    Search Comp PM
    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.
    Quote Quote  
  2. Member
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Search Comp PM
    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
    Quote Quote  
  3. Member
    Join Date
    Jul 2002
    Location
    Up in yo' bitch.
    Search Comp PM
    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.
    Quote Quote  
  4. Member
    Join Date
    Jul 2002
    Location
    Up in yo' bitch.
    Search Comp PM
    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.
    Quote Quote  
  5. Member
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Search Comp PM
    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
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!