问题
Thanks for your time! Using snippets of code gathered here on stackoverflow and elsewhere, I was able to cobble together a macro that will populate a combobox in Word from a defined range of data in an Excel file, and then get a label to print the second column from the combobox (which is too long to display in the combobox itself). So far, so good. Here's my code:
Private Sub ComboBox1_DropButtonClick()
'Late binding. No reference to Excel Object required.
Dim xlApp As Object
Dim xlWB As Object
Dim xlWS As Object
Dim cRows As Long
Dim i As Long
Set xlApp = CreateObject("Excel.Application")
'Open the spreadsheet to get data
Set xlWB = xlApp.Workbooks.Open("EXCEL FILEPATH")
Set xlWS = xlWB.Worksheets(1)
cRows = xlWS.Range("$A2:$B216").Rows.Count - xlWS.Range("$A2:$B216").Row + 1
ComboBox1.ColumnCount = 2
'Populate the listbox.
With Me.ComboBox1
For i = 2 To cRows
'Use .AddItem property to add a new row for each record and populate column 0
.AddItem xlWS.Range("$A1:$B216").Cells(i, 1)
'Use .List method to populate the remaining columns
.List(.ListCount - 1, 1) = xlWS.Range("$A1:$B216").Cells(i, 2)
Next i
End With
'Clean up
Set xlWS = Nothing
Set xlWB = Nothing
xlApp.Quit
'Make label print column 2 of ComboBox
With ComboBox1
Label1.Caption = .List(.ListIndex, 1)
End With
End Sub
The problem is that when the Excel file is moved and then the Word file is closed and reopened, the comobobx no longer gets populated. Unfortunately, this needs to be a standalone Word doc that is distributable via e-mail to multiple users. Is there a way to populate the combobox so the Word document holds the data without having to refer back to the Excel file each time the doc is opened?
Thanks again!
回答1:
One way to achieve this that I've used before is not to use Excel at all, when you send out the Word document, have it connect to a trusted web site that returns the list instead.
While I've not done this in Word, I have done it with Excel where the spreadsheet was emailed out as a blank template for filling in. A web site was used to populate reference data dropdowns. The reference data changed periodically so that was the best way to provide the freshest data while retaining an existing spreadsheet that people were familiar with.
If you are not too bothered about populating from a spreadsheet, you can of course, create a combo from code, the values for the combo could be kept in a field in Word.
Let me know if either of these is helpful & I will try to expand if needed.
Update:
You can make use of a DocProperty Field to store the entries for your combo if you don't actually need anything else from your spreadsheet.
Create a field called Combo Options or whatever and put into it:
Option 1;Option 2;Another Option
Or whatever text options you want.
In VBA, you can access the fields using:
ActiveDocument.CustomDocumentProperties("Combo Options").Value
Then you can split the field into it's components and iterate over them to add the combo box options.
回答2:
Why don't you create file in user's computer with desired data? That means, it only requires first time for them to open excel file and afterwards it would read the data from .txt file.
Here is link on how to create simple file in user's computer: http://www.java2s.com/Code/VBA-Excel-Access-Word/File-Path/WritingtoTextFilesUsingPrint.htm
来源:https://stackoverflow.com/questions/24163819/populating-a-combobox-in-word-from-excel-stops-working-after-excel-file-is-mov