AlecL
10-31-2017, 05:08 AM
Hey everyone,
I'm new to the board and joined specifically to stack up on my VBA game. So far, I'm basically self-taught, with a lot of recording and scavenging from topics I found via google dealing with the tasks arising in my projects, so please bear with me doing stuff in convoluted or stupid ways.
So, here's my situation:
I to have a word document () representing a form I and colleagues at work have to use regularly in projects. In it, I use DropDown form fields so that everyone upon making a new document can pick their name from. So far, names are just added without any programming in word.
Now, I do have an excel book (C:\Documents\Users.xlsx), containing all the names in Sheet 1, column C.
I would like for the Document upon opening to access the excel file and add every cell from column C as a new entry to the form field. There could be varying numbers of entries, so it would be best to have a solution that is sensitive to that.
My two approaches so far were (a), import the Range from excel into an array in my document, or (b) import the data directly into the FormFields
What I have put together so far is the following, which produces an "Object not found" error, and after three days of playing around with it, I'd be really grateful if someone could point out where I went wrong.
Sub Document_Open()
Dim MyArr As Variant 'Array for variant (a)
Dim xlApp As Object
Dim xlWB As Object
Dim xlWS As Object
Dim i as integer
Set xlApp = CreateObject("Excel.Application")
'Open the spreadsheet to get data
Set xlWB = xlApp.Workbooks.Open("C:\Documents\Users.xlsx")
Set xlWS = xlWB.Worksheets(1)
MyArr = x1WS.Range("C1:C14")
ActiveDocument.FormFields("user1").DropDown.ListEntries.Clear
For i = 1 To 20
ActiveDocument.FormFields("user1").DropDown.ListEntries.Add (myArr (i, 2))
Next
'ActiveDocument.FormFields("user1").DropDown.ListEntries.Add (x1WS.Range("C1:C14").Cells(2, 6)) 'Left-over from variant (b) approach
'Clean up
Set xlWS = Nothing
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
End Sub
Bonus question: I tend to favour variant (a), as it would allow me in a next step to use further data from the table pertaining to the different people (such as phone numbers, e-mail-adresses) to be displayed according to the selection. At least that is the plan, unless anyone tells me this won't work anyway?
Thanks a lot for any help in advance!
I'm new to the board and joined specifically to stack up on my VBA game. So far, I'm basically self-taught, with a lot of recording and scavenging from topics I found via google dealing with the tasks arising in my projects, so please bear with me doing stuff in convoluted or stupid ways.
So, here's my situation:
I to have a word document () representing a form I and colleagues at work have to use regularly in projects. In it, I use DropDown form fields so that everyone upon making a new document can pick their name from. So far, names are just added without any programming in word.
Now, I do have an excel book (C:\Documents\Users.xlsx), containing all the names in Sheet 1, column C.
I would like for the Document upon opening to access the excel file and add every cell from column C as a new entry to the form field. There could be varying numbers of entries, so it would be best to have a solution that is sensitive to that.
My two approaches so far were (a), import the Range from excel into an array in my document, or (b) import the data directly into the FormFields
What I have put together so far is the following, which produces an "Object not found" error, and after three days of playing around with it, I'd be really grateful if someone could point out where I went wrong.
Sub Document_Open()
Dim MyArr As Variant 'Array for variant (a)
Dim xlApp As Object
Dim xlWB As Object
Dim xlWS As Object
Dim i as integer
Set xlApp = CreateObject("Excel.Application")
'Open the spreadsheet to get data
Set xlWB = xlApp.Workbooks.Open("C:\Documents\Users.xlsx")
Set xlWS = xlWB.Worksheets(1)
MyArr = x1WS.Range("C1:C14")
ActiveDocument.FormFields("user1").DropDown.ListEntries.Clear
For i = 1 To 20
ActiveDocument.FormFields("user1").DropDown.ListEntries.Add (myArr (i, 2))
Next
'ActiveDocument.FormFields("user1").DropDown.ListEntries.Add (x1WS.Range("C1:C14").Cells(2, 6)) 'Left-over from variant (b) approach
'Clean up
Set xlWS = Nothing
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
End Sub
Bonus question: I tend to favour variant (a), as it would allow me in a next step to use further data from the table pertaining to the different people (such as phone numbers, e-mail-adresses) to be displayed according to the selection. At least that is the plan, unless anyone tells me this won't work anyway?
Thanks a lot for any help in advance!