Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2005

Tip: Looking for answers? Try searching our database.

Get vaules from Excel based on Word Drop Down Box

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robert_L_Ross - 04 Mar 2005 22:41 GMT
Here's where I'm at:

I want to have a drop down box value determine what values to return to
other fields from Excel.

I have dropdown1 and I currently use the Case process (Case 1, Case 2,
etc.).  Problem is you have to keep modifying the macro to add new values.

I'd like Word to search an Excel spreadsheet and return a specific value
(kind of like how DLookup works) back to a cell in the Word Doc.  Here's what
I have so far:

Sub SetToInformation()
   Select Case _
       ActiveDocument.FormFields("ATTNBox").DropDown.Value
   Case 1
       ToBoxTemp = " "
       FaxNumberTemp = "(000) 000-0000"
       TelNumberTemp = "(000) 000-0000"
   Case 2
       ToBoxTemp = "Citibank"
       FaxNumberTemp = "(800) 846-6444"
       TelNumberTemp = "(585) 248-7254"
   Case 3
       ToBoxTemp = "EDFUND"
       FaxNumberTemp = "(123) 456-7890"
       TelNumberTemp = "(098) 765-4321"
   Case Else
       ToBoxTemp = ""
       FaxNumberTemp = "(999) 999-9999"
       TelNumberTemp = "(999) 999-9999"
   End Select
   ActiveDocument.FormFields("ToBox").Result = ToBoxTemp
   ActiveDocument.FormFields("FaxNumber").Result = FaxNumberTemp
   ActiveDocument.FormFields("TelNumber").Result = TelNumberTemp

End Sub

I'm not sure if this will work to get the Excel sheet open, I get a "User
Defined type not defined" error on the line marked with an **:

   Dim FAXApp As excel.Application **
   Dim FAXBook As excel.Workbook
   Dim FAXSheet As excel.sheet
   Dim FAXData As Variant
   Dim ListCount As Long
   Dim ToBoxTemp As String
   Dim FaxNumberTemp As String
   Dim TelNumberTemp As String
   
   Set FAXApp = GetObject(, "Excel Application")
   Set FAXBook = FAXApp.Workbooks.Open("G:\A F S\Document Templates\FAX
COVER SHEET\FAX.xls")
   Set FAXSheet = FAXBook.Worksheets(1)
   
   FAXData = FAXSheet.Range("A2:D3").Value

Any help would be greatly appreciated!
Perry - 06 Mar 2005 11:04 GMT
Hi Robert,

Consider this:

From within Word VBA:
   Dim wb as object
   Dim arrFaxInfo as variant
   Set wb = GetObject("c:\MyPath\Workbook1.xls")
   '<< Have Excel to return values in array
   On Local Error Goto ErrHandler    '<< always use an errhandler (to be
further developed by you)
   arrFaxInfo =
wb.GetValue(ActiveDocument.FormFields("ATTNBox").DropDown.Value)
   wb.Close 0 '<< close without saving changes
   '<< check second dimension of array
   MsgBox affFaxInfo(2)

In other words, you'll be retrieving data from Excel in an array to be used
in word.
Some Excel programming is involved here, to pick up the corresponding other
faxinfo.
The Excel function to do so:

From within Excel VBA (c:\MyPath\Workbook1.xls)
   ThisWorkbook module

   Public Function GetValue(byval DropDownValue as string) as variant
   Dim sht as worksheet, cel as range
   Dim arr(), x as integer
   set sht = me.sheets(1)    '<< first sheet with data
   On Local Error GoTo NoMatch
   set cel = sht.usedrange.columns(1).Find(DropDownValue)
   If not cel is nothing then
       for x = 0 to sht.usedrange.columns.count
           redim preserve arr(x)
           arr(x) = cel.offset(, x)
       next
   End if
ExitHere:
   GetValue = arr
   Exit function
NoMatch:
   Resume exithere
End Function

Krgrds,
Perry

> Here's where I'm at:
>
[quoted text clipped - 54 lines]
>
> Any help would be greatly appreciated!
Robert_L_Ross - 07 Mar 2005 19:31 GMT
Perry,

I think it might work, but I'm getting a type mismatch:
   Dim CSFaxWorkbook As Object

...

   Set CSFaxWorkbook = ("C:\pathname\filename.xls") 'I get a type mismatch
on this line.  Any ideas?

THX again!

> Here's where I'm at:
>
[quoted text clipped - 54 lines]
>
> Any help would be greatly appreciated!
Perry - 07 Mar 2005 23:29 GMT
replace
>     Set CSFaxWorkbook = ("C:\pathname\filename.xls")
by
Set CSFaxWorkbook = GetObject("C:\pathname\filename.xls")

Kindly report yr progress.

Krgrds,
Perry

> Perry,
>
[quoted text clipped - 66 lines]
> >
> > Any help would be greatly appreciated!
Robert_L_Ross - 08 Mar 2005 21:17 GMT
Perry,

I had to make one additional change by adding:
   Dim ArrFaxInfo As String
But then I received:
Run-Time Error '438'
Ojbect doesn't support this property or method
on this line:
ArrFaxInfo =
CSFaxWorkbook.GetValue(ActiveDocument.FormFields("ATTNBox").DropDown.Value)

I noticed as I typed that line, the usual prompts you get after the first
period:
ArrFaxInfo = CSFaxWorkbook.
did not appear.  It's as if my system doesn't know what the GetValue command
is.

So, right now the code looks like this:
   Dim CSFaxWorkbook As Object
   Dim ToBoxInfo As Variant
   Dim FAXBoxInfo As Variant
   Dim PHONEBoxInfo As Variant
   Dim ArrFaxInfo As String
   Set CSFaxWorkbook = GetObject("pathname\filename.xls")
'    On Local Error GoTo ErrHandler - rem'd this till i get farther in the
process
   ArrFaxInfo =
CSFaxWorkbook.getvalue(ActiveDocument.FormFields("ATTNBox").DropDown.Value)
   CSFaxWorkbook.Close 0
   MsgBox (ArrFaxInfo)

Any ideas?

BTW, thanks a LOT for the help and time you've already given me!

> replace
> >     Set CSFaxWorkbook = ("C:\pathname\filename.xls")
[quoted text clipped - 5 lines]
> Krgrds,
> Perry
Perry - 08 Mar 2005 23:41 GMT
ArrFaxInfo =
CSFaxWorkbook.GetValue(ActiveDocument.FormFields("ATTNBox").DropDown.Value)

This should be one line; second line dropped due to word wrapping.

Notice the Line Continuation sign in below VBA statement:

ArrFaxInfo = _
CSFaxWorkbook.GetValue(ActiveDocument.FormFields("ATTNBox").DropDown.Value)

Try again.
Krgrds,
Perry

> Perry,
>
[quoted text clipped - 5 lines]
> on this line:
> ArrFaxInfo =

CSFaxWorkbook.GetValue(ActiveDocument.FormFields("ATTNBox").DropDown.Value)

> I noticed as I typed that line, the usual prompts you get after the first
> period:
[quoted text clipped - 12 lines]
> process
>     ArrFaxInfo =

CSFaxWorkbook.getvalue(ActiveDocument.FormFields("ATTNBox").DropDown.Value)
>     CSFaxWorkbook.Close 0
>     MsgBox (ArrFaxInfo)
[quoted text clipped - 12 lines]
> > Krgrds,
> > Perry
Robert_L_Ross - 09 Mar 2005 16:35 GMT
Perry,

I have this as one line (my screen resolution is high so I don't have the
wrap indicator - the newsgroup screen is so small it wraps here, but it
doesn't wrap on my screen).

Plus, since it didn't work, I tried to hand-enter the line, and like I said,
my system doesn't seem to have the GetValue function.  You know how as you
enter, when the system detects that it has a list to choose from, it presents
that list.  When I'm entering and I get here:

ArrFaxInfo = CSFaxWorkbook.

I get the list of options, but GetValue is not one of those options.  The
list goes from "FullNameURLEncoded" to "HasPassword".  Also, when I type
getvalue, it should capitalize if it recognizes the command (getvalue =
GetValue), but it doesn't.  I'm thinking I don't have the correct library
registered?  Currently, I have the following, listed in the order the appear
in my References window:

Visual Basic for Applications
Microsoft Word 10.0 Object Library
OLE Automation
Normal
Microsoft Office 10.0 Object Library
Microsoft Excel 10.0 Object Library
Microsoft VBScript REgular Expressions 1.0
Microsoft VBScript REgular Expressions 5.5
Microsoft Visual Basic for Applications Extensibility 5.3

Is the GetValue function in some other reference?

THX!

> ArrFaxInfo =
> CSFaxWorkbook.GetValue(ActiveDocument.FormFields("ATTNBox").DropDown.Value)
[quoted text clipped - 57 lines]
> > > Krgrds,
> > > Perry

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.