Option Explicit
Dim sStoredSQL As String
Function QueryRefreshInternal(Optional Ref As Variant) As Variant
Dim Ref1 As Object
On Error GoTo ReturnPoundRef
If IsMissing(Ref) Then
Set Ref1 = ActiveCell
Else
Set Ref1 = Ref
End If
If Application.DataEntryMode <> xlOff Then GoTo ReturnPoundRef
If Application.ActiveWindow.SelectedSheets.Count > 1 Then GoTo ReturnPoundRef
If TypeName(Ref1) <> "Range" Then GoTo ReturnPoundRef
If Ref1.Areas.Count > 1 Then GoTo ReturnPoundRef
If Ref1.Rows.Count > 1 Or Ref1.Columns.Count > 1 Then GoTo ReturnPoundRef
' If the single cell is within a querytable range, then refresh that range synchronously.
' Accessing the QueryTable property for a range that isn't within a querytable
' will generate a runtime exception that will cause #REF to be returned. Failure
' to refresh will generate a runtime exception that will cause #REF to be returned.
Ref1.QueryTable.Refresh False
QueryRefreshInternal = True
Exit Function
ReturnPoundRef:
QueryRefreshInternal = CVErr(xlErrRef)
Exit Function
End Function
Private Function ValidateBooleanArgument(arg As Variant, fDefault As Boolean, fResult As Boolean) As Boolean
ValidateBooleanArgument = True
If IsMissing(arg) Then
fResult = fDefault
Else
On Error GoTo CannotConvertToBool
fResult = CBool(arg)
End If
Exit Function
CannotConvertToBool:
ValidateBooleanArgument = False
Exit Function
End Function
Function QueryGetDataDialogInternal(Optional ConnectionStr As Variant, Optional QueryText As Variant, Optional KeepQueryDef As Variant, Optional FieldNames As Variant, Optional RowNumbers As Variant, Optional Destination As Variant, Optional Execute As Variant, Optional KeepPassword As Variant) As Variant
Dim sConnectionString As String
Dim sSQL As String
' GetDataDialog doesn't actually work for Excel97, since there's no interactive form.
' Fail out if the connection string and/or querytext string are missing or empty
On Error GoTo ReturnPoundRef
If IsMissing(ConnectionStr) Then GoTo ReturnPoundRef
sConnectionString = CStr(ConnectionStr)
If Len(sConnectionString) = 0 Then GoTo ReturnPoundRef
If IsMissing(QueryText) Then GoTo ReturnPoundRef
sSQL = CStr(QueryText)
If Len(sSQL) = 0 Then GoTo ReturnPoundRef
QueryGetDataDialogInternal = QueryGetDataValidateArgs(sConnectionString, sSQL, KeepQueryDef, FieldNames, RowNumbers, Destination, Execute, KeepPassword)
Exit Function
ReturnPoundRef:
QueryGetDataDialogInternal = CVErr(xlErrRef)
Exit Function
End Function
Function QueryGetDataXLMStub(ConnectionStr As Variant, QueryText As Variant, Optional KeepQueryDef As Variant, Optional FieldNames As Variant, Optional RowNumbers As Variant, Optional Destination As Variant, Optional Execute As Variant, Optional KeepPassword As Variant) As Variant
Dim sConnectionString As String
Dim sSQL As String
Dim v As Variant
On Error GoTo ReturnPoundRef
If Not IsArray(ConnectionStr) Then
sConnectionString = CStr(ConnectionStr)
Else
For Each v In ConnectionStr
sConnectionString = sConnectionString & CStr(v)
Next
End If
If Not IsArray(QueryText) Then
sSQL = CStr(QueryText)
Else
For Each v In QueryText
sSQL = sSQL & CStr(v)
Next
End If
QueryGetDataXLMStub = QueryGetDataValidateArgs(sConnectionString, sSQL, KeepQueryDef, FieldNames, RowNumbers, Destination, Execute, KeepPassword)
Exit Function
ReturnPoundRef:
QueryGetDataXLMStub = CVErr(xlErrRef)
Exit Function
End Function
Function QueryGetDataValidateArgs(ConnectionStr As String, QueryText As String, Optional KeepQueryDef As Variant, Optional FieldNames As Variant, Optional RowNumbers As Variant, Optional Destination As Variant, Optional Execute As Variant, Optional KeepPassword As Variant) As Variant
Dim fKeepQueryDef As Boolean
Dim fFieldNames As Boolean
Dim fRowNumbers As Boolean
Dim fExecute As Boolean
Dim fKeepPassword As Boolean
Dim sSQL As String
Dim rDestination As Object
If Not ValidateBooleanArgument(KeepQueryDef, True, fKeepQueryDef) Then GoTo ReturnPoundRef
If Not ValidateBooleanArgument(FieldNames, True, fFieldNames) Then GoTo ReturnPoundRef
If Not ValidateBooleanArgument(RowNumbers, True, fRowNumbers) Then GoTo ReturnPoundRef
If Not ValidateBooleanArgument(Execute, True, fExecute) Then GoTo ReturnPoundRef
If Not ValidateBooleanArgument(KeepPassword, True, fKeepPassword) Then GoTo ReturnPoundRef
' handle building up the array of SQL. Instead of keeping around an array like the Excel5 implementation,
' we keep the SQL in a single string for convenience.
If fExecute Then
If Len(sStoredSQL) > 0 Then
sSQL = sStoredSQL & QueryText
Else
sSQL = QueryText
End If
sStoredSQL = ""
' now that we have the SQL text, validate the destination range object
If Application.DataEntryMode <> xlOff Then GoTo ReturnPoundRef
If Application.ActiveWindow.SelectedSheets.Count > 1 Then GoTo ReturnPoundRef
On Error GoTo ReturnPoundRef
If IsMissing(Destination) Then
Set rDestination = ActiveCell
Else
Set rDestination = Destination
End If
If TypeName(rDestination) <> "Range" Then GoTo ReturnPoundRef
QueryGetDataValidateArgs = QueryGetDataFetch(ConnectionStr, sSQL, fKeepQueryDef, fFieldNames, fRowNumbers, rDestination, fKeepPassword)
Else
' not executing, just building the SQL string.
sStoredSQL = sStoredSQL & QueryText
QueryGetDataValidateArgs = True
End If
Exit Function
ReturnPoundRef:
QueryGetDataValidateArgs = CVErr(xlErrRef)
Exit Function
End Function
Private Function QueryGetDataFetch(sConnectionString As String, sSQL As String, fKeepQueryDef As Boolean, fFieldNames As Boolean, fRowNumbers As Boolean, rDestination As Range, fKeepPassword As Boolean) As Variant
Dim qt As QueryTable
On Error GoTo NotInQueryTable
Set qt = rDestination.QueryTable
If Not (qt Is Nothing) Then
' QueryTable already covers the destination range. Modify it to have the new connectionstring
' and query parameter
On Error GoTo ReturnPoundRef
qt.Connection = "ODBC;" & sConnectionString
qt.Sql = sSQL
Else
CreateNewQueryTable:
On Error GoTo ReturnPoundRef
Set qt = rDestination.Worksheet.QueryTables.Add("ODBC;" & sConnectionString, rDestination, sSQL)
End If
qt.RowNumbers = fRowNumbers
qt.FieldNames = fFieldNames
qt.SavePassword = fKeepPassword
On Error GoTo RefreshFailed
qt.Refresh False
If Not fKeepQueryDef Then
qt.Delete
End If
QueryGetDataFetch = True
Exit Function
NotInQueryTable:
Resume CreateNewQueryTable
ReturnPoundRef:
QueryGetDataFetch = CVErr(xlErrRef)
Exit Function
RefreshFailed:
qt.Delete
GoTo ReturnPoundRef
End Function
Sub Compiler()
Dim rLanguageRange As Range
Dim sLang As String
Dim wsLocal As Worksheet
Set wsLocal = ThisWorkbook.Worksheets("Loc Table")
sLang = wsLocal.Range("SelectedLanguage").Value
Set rLanguageRange = wsLocal.Range("USAColumnStart")
' Do the USA VBA functions, which are present in addition to the localized
' VBA functions
If StrComp(sLang, "USA", 1) <> 0 Then CompileVBAFunctionNames rLanguageRange
' and do the rest of the compilation for the selected language
Do Until IsEmpty(rLanguageRange.Value)
If StrComp(rLanguageRange.Value, sLang, 1) = 0 Then
' Compile the VBA Function names
CompileVBAFunctionNames rLanguageRange
' Compile the XLM Function names
CompileXLMFunctionNames rLanguageRange
' Compile the XLM Function arguments
CompileXLMFunctionArgs rLanguageRange
' Setup the summary info using this language
CompileSummaryInfo rLanguageRange
Exit Do
End If
Set rLanguageRange = rLanguageRange.Offset(0, 1)
Loop
End Sub
Sub CompileVBAFunctionNames(rLocRange As Range)
' QueryRefresh
Application.MacroOptions rLocRange.Offset(6, 0).Value, Description:=rLocRange.Offset(5, 0).Value, _
hasmenu:=False, hasshortcutkey:=False, Category:=6
' QueryGetData
Application.MacroOptions rLocRange.Offset(9, 0).Value, Description:=rLocRange.Offset(8, 0).Value, _
hasmenu:=False, hasshortcutkey:=False, Category:=6
' QueryGetDataDialog
Application.MacroOptions rLocRange.Offset(12, 0).Value, Description:=rLocRange.Offset(11, 0).Value, _
hasmenu:=False, hasshortcutkey:=False, Category:=6
End Sub
Sub CompileXLMFunctionNames(rLocRange As Range)
Dim sSheetPrefix As String
Dim wsXLQUERY As Worksheet
Dim sMacroName As String
Set wsXLQUERY = ThisWorkbook.Excel4IntlMacroSheets("XLQUERY")
sSheetPrefix = "=XLQUERY!"
' the QUERY.REFRESH name is offset down by 4 rows
sMacroName = rLocRange.Offset(4, 0).Value
ThisWorkbook.Names.Add sMacroName, _
sSheetPrefix & wsXLQUERY.Range("QueryRefreshLocation").Address(True, True, xlA1, False), _
MacroType:=2, Category:=6
Application.MacroOptions sMacroName, Description:=rLocRange.Offset(5, 0).Value, _
hasmenu:=False, hasshortcutkey:=False, Category:=6
' the QUERY.GET.DATA name is offset down by 6 rows
sMacroName = rLocRange.Offset(7, 0).Value
ThisWorkbook.Names.Add sMacroName, _
sSheetPrefix & wsXLQUERY.Range("QueryGetDataLocation").Address(True, True, xlA1, False), _
MacroType:=2, Category:=6
Application.MacroOptions sMacroName, Description:=rLocRange.Offset(8, 0).Value, _
hasmenu:=False, hasshortcutkey:=False, Category:=6
' the QUERY.GET.DATA? name is offset down by 8 rows
sMacroName = rLocRange.Offset(10, 0).Value
ThisWorkbook.Names.Add sMacroName, _
sSheetPrefix & wsXLQUERY.Range("QueryGetDataDialogLocation").Address(True, True, xlA1, False), _
MacroType:=2, Category:=6
Application.MacroOptions sMacroName, Description:=rLocRange.Offset(11, 0).Value, _
hasmenu:=False, hasshortcutkey:=False, Category:=6
End Sub
Sub CompileXLMFunctionArgs(rLocRange As Range)
Dim wsXLQUERY As Worksheet
Dim rDest As Range
Dim rSrc As Range
Dim i As Integer
Set wsXLQUERY = ThisWorkbook.Excel4IntlMacroSheets("XLQUERY")
Set rDest = wsXLQUERY.Range("mg00s.addin1")
Set rSrc = rLocRange.Offset(13, 0)
For i = 0 To 7
rDest.Offset(i, 0).Value = rSrc.Offset(i, 0).Value
Next
End Sub
Sub CompileSummaryInfo(rLocRange As Range)
Dim sOldUserName As String
ThisWorkbook.Title = rLocRange.Offset(1, 0).Value
ThisWorkbook.Author = rLocRange.Offset(2, 0).Value
ThisWorkbook.BuiltinDocumentProperties("Company").Value = rLocRange.Offset(2, 0).Value
ThisWorkbook.Comments = rLocRange.Offset(3, 0).Value
sOldUserName = Application.UserName
Application.UserName = rLocRange.Offset(2, 0).Value
ThisWorkbook.IsAddin = True
'ThisWorkbook.SaveCopyAs "C:\xlquery.xla"
'Application.Dialogs(xlDialogSaveCopyAs).Show
ThisWorkbook.IsAddin = False
Application.UserName = sOldUserName
End Sub
-------完完完完完完完完完-完完完完完完完完完-------
[此贴子已经被作者于2005-5-4 16:38:27编辑过] |