Solved

Word VBA-Populate MS Word Combobox1 with Excel data Problem

Posted on 2011-03-09
4
318 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
No worries...always glad to help :)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
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.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now