如何使用vba以html格式访问excel数据?

时间:2015-09-01 16:35:23

标签: excel vba excel-vba

Access中的数据库来自ODBC数据库。我需要的数据在备忘录字段中有大约10,000个字符。我通常使用HTML格式复制+粘贴excel。

ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:= _
    False, NoHTMLFormatting:=True 

但是,我想自动执行此过程,并尝试将其合并到以下代码中。在建立连接后,PasteSpecial不起作用。

Private Sub RetrieveRecordset(strSQL As String, clTrgt As Range)
'Author       : Ken Puls (www.excelguru.ca)
'Macro Purpose: To retrieve a recordset from a database (via an SQL query)  and place
 '               it in the supplied worksheet range
 'NOTE         : Requires a reference to "Microsoft ActiveX Data Objects 2.x Library"
 '               (Developed with reference to version 2.0 of the above)
Dim cnt1 As New ADODB.Connection
Dim rst1 As New ADODB.Recordset
Dim rcArray As Variant
Dim lFields As Long
Dim lRecrds As Long 
Dim lCol As Long
Dim lRow As Long
'Open connection to the database
cnt1.Open glob_sConnect
'Open recordset based on Orders table
rst1.Open strSQL, cnt1
'Count the number of fields to place in the worksheet
lFields = rst1.Fields.Count
'Copy the recordset from the database
On Error Resume Next
clTrgt.CopyFromRecordset rst1  
'CopyFromRecordset will fail if the recordset contains an OLE
'object field or array data such as hierarchical recordsets
If Err.Number <> 0 Then GoTo EarlyExit
EarlyExit:
'Close and release the ADO objects
rst1.Close
cnt1.Close
Set rst1 = Nothing
Set cnt1 = Nothing
On Error GoTo 0
End Sub

Sub GetRecords()
'Find the newest record which contains data which has previously been used for each database

'Generate the SQL queries
sFCESQLQry = " SELECT...."

'Paste in Workbook

wbFurnace.Activate
Set rngTarget = ActiveSheet.Range("A2")
Range("A1").PasteSpecial **Format:="HTML", Link:=False, DisplayAsIcon:= _
    False, NoHTMLFormatting:=True**
Call RetrieveRecordset(sFCESQLQry, rngTarget)

End Sub

在这种情况下,PasteSpecial不起作用。如果我使用x1Pasteall它会工作,但数据是表格而不是我想要的HTML格式。关于这个问题的任何想法? 如果有其他方法可以获取html格式的数据,请告诉我。

0 个答案:

没有答案