?
Solved

Word VBA-Populate MS Word Combobox1 with Excel data Problem

Posted on 2011-03-09
4
Medium Priority
?
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
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…

801 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