Solved

Word VBA-Populate MS Word Combobox1 with Excel data Problem

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

687 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