Solved

Word VBA-Populate MS Word Combobox1 with Excel data Problem

Posted on 2011-03-09
4
327 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 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