How do I open a text data file from WORD using VB code?

S

SMTH

I am in Microsoft word and adding a combo box control to the document. I need
a code that will allow me to open a data file..(text file or an excel
worksheet) when the user chooses an item from the combo list.
 
H

Helmut Weber

Hi,
fill the combobox with text from a textfile on initializing:

Private Sub UserForm_Initialize()
Dim s As String
Open "c:\editx\ini.txt" For Input As #1
While Not EOF(1)
Input #1, s
UserForm1.ComboBox1.AddItem s
Wend
Close #1
End Sub

Trigger an action from the combobox:
Private Sub ComboBox1_Change()
MsgBox ComboBox1.List(ComboBox1.ListIndex)
' or whatever
' see UserForm_Initialize() for opening a text file
' reading from it and closing it again
End Sub

With Excel it would be a bit more complicated:

http://www.mvps.org/word/FAQs/InterDev/ControlXLFromWord.htm

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
F

fumei

Are you wanting to fill the combobox with the CONTENTS of the text file, or
just open the file using the filename listed in the combobox?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top