Você está na página 1de 8

'do declare these variables you need to add a reference 'to the microsoft excel 'xx' object library.

'you need two text boxes and two command buttons 'on the form, an excel file in c:\book1.xls Dim xl As New Excel.Application Dim xlsheet As Excel.Worksheet Dim xlwbook As Excel.Workbook Private Sub Command1_Click() 'the benifit of placing numbers in (row, col) is that you 'can loop through different directions if required. I could 'have used column names like "A1" 'etc. Text1.Text = xlsheet.Cells(2, 1) ' row 2 col 1 Text2.Text = xlsheet.Cells(2, 2) ' row 2 col 2 'don't forget to do this or you'll not be able to open 'book1.xls again, untill you restart you pc. xl.ActiveWorkbook.Close False, "c:\book1.xls" xl.Quit End Sub Private Sub Command2_Click() xlsheet.Cells(2, 1) = Text1.Text xlsheet.Cells(2, 2) = Text2.Text xlwbook.Save 'don't forget to do this or you'll not be able to open 'book1.xls again, untill you restart you pc. xl.ActiveWorkbook.Close False, "c:\book1.xls" xl.Quit End Sub Private Sub Form_Load() Set xlwbook = xl.Workbooks.Open("c:\book1.xls") Set xlsheet = xlwbook.Sheets.Item(1) End Sub Private Sub Form_Unload(Cancel As Integer) Set xlwbook = Nothing Set xl = Nothing End Sub

Here is code to write to a text file and an excel file. As far as the reading from an excel file I am at a loss. You will need to add the Microsoft Common Dialog Control 6.0 (SP3) reference to your project.

As far as using the text file, I do have problems when users enter a comma in a field, it doesn't use text delimiters. It makes it difficult to parse the data when I receive it. HTH, Tricia Call txt(rstSendFile) Call excel(rstSentFile)

(SEND MODULE) Option Explicit Public rstSendFile As ADODB.Recordset Public objExcel As Object Public objTemp As Object Public rstSendInfo As ADODB.Recordset Public Function txt(rstSendFile As ADODB.Recordset) As Boolean Dim iTotalRecords As Integer Dim sFileToExport As String Dim iFileNum As Integer Dim msg As String Dim iIndx As Integer Dim iNumberOfFields As Integer Screen.MousePointer = vbDefault On Error Resume Next CreateSendFile If rstSendFile.RecordCount = 0 Then Exit Function With frmMain.CD1 .CancelError = True .FileName = "Export" & gsCustomerNumber & ".txt" .InitDir = App.Path .DialogTitle = "Save Text File" .Filter = "Export Files (*.txt)|*.txt" .DefaultExt = "TXT" .Flags = cdlOFNOverwritePrompt Or cdlOFNCreatePrompt .ShowSave End With

'-------------------------------'-- User cancels the operation -'-------------------------------If Err.Number = cdlCancel Then 'operation canceled Screen.MousePointer = vbDefault msg = "The export operation was canceled." & vbCrLf iIndx = MsgBox(msg, vbOKOnly + vbInformation, "Text Export File") txt = False Exit Function Else On Error GoTo ErrorHandler End If '--------------------------------------'-- Let's save the data now. -'-- Get the name of the file to save. -'--------------------------------------Screen.MousePointer = vbHourglass iTotalRecords = 0 sFileToExport = frmMain.CD1.FileName iFileNum = FreeFile() Open sFileToExport For Output As #iFileNum ' Open file for output. '------------------------'-- Stream out the data -'------------------------iNumberOfFields = rstSendFile.Fields.Count - 1 rstSendFile.MoveFirst Do Until rstSendFile.EOF iTotalRecords = iTotalRecords + 1 For iIndx = 0 To iNumberOfFields If (IsNull(rstSendFile.Fields(iIndx))) Then Print #iFileNum, ","; 'simply a comma delimited string Else If iIndx = iNumberOfFields Then Print #iFileNum, Trim$(CStr(rstSendFile.Fields(iIndx))); Else Print #iFileNum, Trim$(CStr(rstSendFile.Fields(iIndx))); ","; End If End If Next Print #iFileNum,

rstSendFile.MoveNext DoEvents Loop '---------------Close #iFileNum Screen.MousePointer = vbDefault msg = "Export File " & sFileToExport & vbCrLf msg = msg & "successfully created." & vbCrLf msg = msg & iTotalRecords & " records written to disk." & vbCrLf iIndx = MsgBox(msg, vbOKOnly + vbInformation, "Comma Delimited File") txt = True Dim iResponse As Integer If MsgBox("Do you want to record these records as sent?", _ vbYesNoCancel, gsDialogTitle) = vbYes Then With rstSendInfo If .EOF Then .MoveFirst !FileSentDate = Now() .Update Else !FileSentDate = Now() .Update End If End With End If Exit Function ErrorHandler: DisplayErrorMessage txt = False End Function Public Sub CreateSendFile() Set rstSendFile = New ADODB.Recordset Set rstSendInfo = New ADODB.Recordset rstSendFile.CursorLocation = adUseClient rstSendFile.CursorLocation = adUseClient rstSendFile.Open "SELECT * FROM qrySendInfo", gcnDue, adOpenForwardOnly, adLockOptimistic, adCmdText

rstSendInfo.Open "select * FROM SendInfo", gcnDue, adOpenForwardOnly, adLockOptimistic, adCmdText If rstSendFile.RecordCount = 0 Then MsgBox "There are no records to export.", vbOKOnly, gsDialogTitle Else MsgBox rstSendFile.RecordCount & " records will be exported.", vbOKOnly, gsDialogTitle End If End Sub Public Sub excel(rstSendFile As ADODB.Recordset) Dim Dim Dim Dim Dim Dim Dim Dim iIndx As Integer iRowIndex As Integer iColIndex As Integer iRecordCount As Integer iFieldCount As Integer sMessage As String avRows As Variant excelVersion As Integer

On Error GoTo ErrHandler CreateSendFile If rstSendFile.RecordCount = 0 Then Exit Sub '-- Read all of the records into our array avRows = rstSendFile.GetRows() '-- Determine how many fields and records iRecordCount = UBound(avRows, 2) + 1 iFieldCount = UBound(avRows, 1) + 1 '-- Create reference variable for the spreadsheet Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True objExcel.Workbooks.Add '-- We need this line to insure Excel remains visible if we switch Set objTemp = objExcel excelVersion = Val(objExcel.Application.Version) If (excelVersion >= 8) Then Set objExcel = objExcel.ActiveSheet

End If '-- Place the names of the fields as column headers -iRowIndex = 1 iColIndex = 1 For iColIndex = 1 To iFieldCount With objExcel.Cells(iRowIndex, iColIndex) .Value = rstSendFile.Fields(iColIndex - 1).Name With .Font .Name = "Arial" .Bold = True .Size = 9 End With End With Next '-- memory management -rstSendFile.Close Set rstSendFile = Nothing '-- Just add data -With objExcel For iRowIndex = 2 To iRecordCount + 1 For iColIndex = 1 To iFieldCount .Cells(iRowIndex, iColIndex).Value = avRows(iColIndex - 1, iRowIndex - 2) Next Next End With objExcel.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit Dim iResponse As Integer If MsgBox("Do you want to record these records as sent?", _ vbYesNoCancel, gsDialogTitle) = vbYes Then With rstSendInfo If .EOF Then .MoveFirst !FileSentDate = Now() .Update Else !FileSentDate = Now() .Update End If

End With End If Exit Sub Exit Sub ErrHandler: If Err.Number = 429 Then MsgBox "You cannot use this feature unless you have Microsoft Excel loaded." Exit Sub Else DisplayErrorMessage Exit Sub End If End Sub dim oconn as adodb.connection dim ors as adodb.recordset ' ' this assumes that the first row contains headers ' Set oConn = New ADODB.Connection oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & sxlsfilename & ";" & "Extended Properties=""Excel 8.0;HDR=YES;""" ' ' the table name is the worksheet name ' sTableName = "[sheet1$]" sTableName = "select * from " & sTableName ' 'Get the recordset ' Set oRS = New ADODB.Recordset oRS.Open sTableName, oConn, adOpenStatic, adLockOptimistic nCols = oRS.Fields.Count Add a reference to Microsft excel object library: Option Explicit Private Sub Command1_Click() Dim xlApp As Excel.Application Dim wb As Workbook Dim ws As Worksheet Dim var As Variant

Set xlApp = New Excel.Application Set wb = xlApp.Workbooks.Open("PATH TO YOUR EXCEL FILE") Set ws = wb.Worksheets("Sheet1") 'Specify your worksheet name var = ws.Range("A1").Value 'or var = ws.Cells(1, 1).Value wb.Close xlApp.Quit Set ws = Nothing Set wb = Nothing Set xlApp = Nothing End Sub