Returning data from a closed workbook to a variable

Pauly

New Member
Joined
Feb 16, 2002
Messages
1
Hi,
I've been using the following code to find a name and return the associated address from a closed file to automatically address email in Outlook. The result of the search is currently placed in a cell on the activeworksheet but I want it to return to a variable or put it straight into the To box in Outlook.
I've found several other methods but none incorporate the 'find' function I require and all my efforts to get it to work have failed.

I want it to work in Excel 97 & 2000 and would prefer not to use ActiveX unless it can be programmatically activated.

Any help greatly appreciated.

Pauly

Here's the current code:

Sub GetClientAddress()

Dim strPath As String
Dim strFile As String
Dim connstring As String
Dim sqlstring As String
Dim strClientName As String

strClientName = ActiveSheet.Range("b4")
strPath = frmSetup.TxtMasterFolder & "/" 'important to have the / added
strFile = "Client Addresses.xls"

connstring = _
"ODBC;DBQ= " & strPath & strFile & ";DefaultDir=" & strPath & ";Driver={Microsoft Excel Driver (*.xls)}"

sqlstring = _
"SELECT Addresses.`Client Address` FROM Addresses Addresses WHERE (Addresses.`Client Name`='" & strClientName & "')"

With ActiveSheet.QueryTables.Add(Connection:=connstring, _
Destination:=Range("A1"), Sql:=sqlstring)
.FieldNames = False
.RefreshStyle = xlOverwriteCells
.Refresh BackgroundQuery:=False

End With

End Sub
This message was edited by pauly on 2002-02-17 13:39
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
On 2002-02-17 03:42, Pauly wrote:
Hi,
I've been using the following code to find a name and return the associated address from a closed file to automatically address email in Outlook. The result of the search is currently placed in a cell on the activeworksheet but I want it to return to a variable or put it straight into the To box in Outlook.
I've found several other methods but none incorporate the 'find' function I require and all my efforts to get it to work have failed.

Any help greatly appreciated.

Pauly

Here's the current code:

Sub GetClientAddress()

Dim strPath As String
Dim strFile As String
Dim connstring As String
Dim sqlstring As String
Dim strClientName As String

strClientName = ActiveSheet.Range("b4")
strPath = frmSetup.TxtMasterFolder & "" 'important to have the added
strFile = "Client Addresses.xls"

connstring = _
"ODBC;DBQ= " & strPath & strFile & ";DefaultDir=" & strPath & ";Driver={Microsoft Excel Driver (*.xls)}"

sqlstring = _
"SELECT Addresses.`Client Address` FROM Addresses Addresses WHERE (Addresses.`Client Name`='" & strClientName & "')"

With ActiveSheet.QueryTables.Add(Connection:=connstring, _
Destination:=Range("A1"), Sql:=sqlstring)
.FieldNames = False
.RefreshStyle = xlOverwriteCells
.Refresh BackgroundQuery:=False

End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,237
Members
448,555
Latest member
RobertJones1986

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top