Solved

Word VBA-Populate MS Word Combobox1 with Excel data Problem

Posted on 2011-03-09
4
323 Views
Last Modified: 2012-05-11
Greetinga All;

I've been tinkering with this Word VBA script that loads an MS Word (2003)
Combobox1 with data from an Excel (2003) spreadsheet (all data in Sheet1/Column A).

Click the command button - locate/highlight the spreadsheet in the file directory
window - data is loaded in Combobox1.

However when I SAVE, EXIT and reopen the MS Word (2003) document only the value
selected in Combobox1 remains. All the other loaded values are gone.

Two Questions...
(1) How can this script be modified so the Combobox1 values permanently remain in
the MS Word (2003) Combobox1 after SAVING, EXITING and reopening the document?

(2) How can this script be modified so that no blanks are loaded in the MS Word (2003)
Combobox1?


Thank You for you time and consideration.
´*•.¸(`*•.¸?¸.•*´)¸.•*´
?*°Doris 42DoubleDDs*°•´»?
.¸.•*(¸.•*´?`*•.¸) *•.¸


Option Explicit

Private Sub CommandButton1_Click()
Dim objExcel        As New Excel.Application
Dim wb              As Excel.Workbook
Dim FName           As Variant
Dim x               As Long
Dim LastRow         As Long

    FName = objExcel.GetOpenFilename("Excel Files (*.xls), *.xls")
    If FName = "" Or FName = False Then
        GoTo Canceled:
    End If
    Set wb = objExcel.Workbooks.Open(FName)
    With wb.Sheets(1)
        LastRow = .Range("A65536").End(xlUp).Row
        For x = 1 To LastRow
            Me.ComboBox1.AddItem (.Range("A" & x).Text)
        Next x
    End With

Canceled:

    objExcel.Quit

End Sub

Open in new window

0
Comment
Question by:DMackallii
  • 2
4 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35091069
AFAIK, the only way to do it is

1) Either save the values in a text file on your harddrive and load it again when you open the document. OR

2) Save the values in the word document it self and re-populate the combo again when you open the document.

Sid
0
 
LVL 14

Accepted Solution

by:
Brook Braswell earned 500 total points
ID: 35094195
How to prevent Blanks...
Option Explicit

Private Sub CommandButton1_Click()
Dim objExcel        As New Excel.Application
Dim wb              As Excel.Workbook
Dim FName           As Variant
Dim x               As Long
Dim LastRow         As Long

    FName = objExcel.GetOpenFilename("Excel Files (*.xls), *.xls")
    If FName = "" Or FName = False Then
        GoTo Canceled:
    End If
    Set wb = objExcel.Workbooks.Open(FName)
    With wb.Sheets(1)
        LastRow = .Range("A65536").End(xlUp).Row
        For x = 1 To LastRow
            If trim(.Range("A" & x).Text) <> "" then
            Me.ComboBox1.AddItem (.Range("A" & x).Text)
            End IF
        Next x
    End With

Canceled:

    objExcel.Quit

End Sub

Toggle HighlightingOpen in New WindowSelect All

Open in new window

0
 

Author Closing Comment

by:DMackallii
ID: 35099280
That's just what I was looking for Brook1966. You make it look so easy.

Thank You for your time and considerable talents.

´*•.¸(`*•.¸?¸.•*´)¸.•*´
?*°Doris 42DoubleDDs*°•´»?
.¸.•*(¸.•*´?`*•.¸) *•.¸
0
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 35107952
No worries...always glad to help :)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question