Click here to Skip to main content
15,896,153 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Application.ProductVersion [EDITED] Pin
Dave Kreskowiak13-Dec-04 3:24
mveDave Kreskowiak13-Dec-04 3:24 
Questionadjust height of RptLine at run-time in data report? Pin
jitwadee12-Dec-04 17:03
jitwadee12-Dec-04 17:03 
AnswerRe: adjust height of RptLine at run-time in data report? Pin
Dave Kreskowiak13-Dec-04 3:13
mveDave Kreskowiak13-Dec-04 3:13 
AnswerRe: adjust height of RptLine at run-time in data report? Pin
jitwadee14-Dec-04 15:30
jitwadee14-Dec-04 15:30 
GeneralReferencing an Excel Spreadsheet in VB6 Pin
itsmattdee12-Dec-04 14:22
itsmattdee12-Dec-04 14:22 
GeneralRe: Referencing an Excel Spreadsheet in VB6 Pin
Dave Kreskowiak13-Dec-04 2:07
mveDave Kreskowiak13-Dec-04 2:07 
GeneralRe: Referencing an Excel Spreadsheet in VB6 Pin
itsmattdee13-Dec-04 5:13
itsmattdee13-Dec-04 5:13 
GeneralRe: Referencing an Excel Spreadsheet in VB6 Pin
JWSantora118-Dec-04 17:00
JWSantora118-Dec-04 17:00 
Matt,
a) You've got a bit of work ahead of you.
b) you're in the wrong forum, this one is for VB.NET
c) anyway, here's a class and some code I used to read and write to Excel files.


----
Option Explicit

Public sFileName As String

Dim sCurrentWorksheet As String

Dim bExcelWasRunning As Boolean

Public oExcel As Object


'Sub EnterValue()
' Worksheets("Sheet1").Cells(6, 1).Value = 10
'End Sub
'The Cells property works well for looping through a range of cells, because you can substitute variables for the index numbers, as shown in the following example.'

'Sub CycleThrough()
' Dim counter As Integer
' For counter = 1 To 20
' Worksheets("Sheet1").Cells(counter, 3).Value = counter
' Next counter
'End Sub

Public Function Init(ByVal asExcelFileName As String) ' , ByVal asCurrentWorksheet) as boolean

' Get handle to currently loaded instance of Excel if there is one.

Init = False

bExcelWasRunning = False
Err.Clear
On Error Resume Next
Set oExcel = CreateObject("Excel.Application")
If Err.number <> 0 Then
MsgBox ("Could not start up Excel. [E9810180901]")
Exit Function
End If
On Error GoTo OpenError

If asExcelFileName = "" Then
Dim xlWb As Object
Dim xlWs As Object
Set xlWb = oExcel.Workbooks.Add
Set xlWs = xlWb.Worksheets("Sheet1")
Else
sFileName = asExcelFileName
On Error GoTo OpenError
oExcel.Workbooks.Open asExcelFileName

On Error GoTo 0
Err.Clear
End If

Init = True

Exit Function

OpenError:
MsgBox ("Error while opening the Excel file: " & asExcelFileName & " " & vbCrLf & vbCrLf & Err.Description)
Init = False

End Function

Public Sub destroy()

' Shut down word if we loaded it ourselves, otherwise don't shut down an app we did not load.
If Not bExcelWasRunning Then oExcel.Quit

End Sub

----


----
' some mainline code that uses the class above.
dim objExcel as clsExcel

Set objExcel = New clsExcel

Call objExcel.Init(txtExcelInputFile.Text) ' open the file...like c:\mydirectory\SomeExcelFile.xls

Dim s As String
On Error Resume Next
s = objExcel.sFileName
If Err.number = 91 Then
MsgBox ("Please use File / Open Excel Input File Menu to connect to the Excel file you want to import.")
Exit Sub
End If


On Error Resume Next
With objExcel.oExcel.Worksheets(SheetID) '("Sheet1")
foo = .cells(1,5)

etc...
---------

Finally, you need to be careful pulling cell values from the spreadsheet.
The cell could be empty, not contain the datatype you think should be there,
or worse yet, may contain a formula.
Here's a wrapper function I wrote for pulling out what is suppose to be date-type data


Private Function GetDateField(ByVal sFieldName as string , ByVal iRow As Integer, Optional bReq As Variant) As String
Dim nCol As Integer
Dim s As String
Dim dt As Date

' bReq: is this field Required? if yes, and the data in the cell is missing, put out an error message.
If IsMissing(bReq) Then bReq = False

GetDateField = ""

' pass in the "name" of a field, look up in colInfo array within the application,
' obtain the column that data is in. Example: CityName is mapped to column 14, Salary to col 20, etc)
nCol = ColInfo(sFieldName)
If nCol = 0 Then
If bReq Then
' send error to the log file. the value is missing from the Cell, and it was a required field.
Call AppendError("Row=" & iRow & ", column " & quote(quote(ColInfo(nField, 1))) & _
" is missing from input Excel file. Please correct.")
End If
Exit Function
End If

s = Trim(objExcel.oExcel.Worksheets(SheetID).Cells(iRow, nCol).Value)
If s = "" Then
If bReq Then
Call AppendError("Row=" & iRow & " Column=" & nCol & ", Field " & quote(quote(ColInfo(nField, 1))) & _
" Field is empty. Please correct.")
End If
Exit Function
End If

On Error Resume Next
dt = CDate(s)
If Err.number <> 0 Then
' can't convert the date to a date, we're in trouble.
Call AppendError("Row=" & iRow & " Column=" & nCol & ", Field " & quote(quote(ColInfo(nField, 1))) & _
" Field is invalid: " & quote(s) & ". Please correct.")
Exit Function
End If
On Error GoTo 0

GetDateField = Format(s, "mm/dd/yyyy")

End Function
GeneralTAB control change tab order Pin
Mekong River12-Dec-04 14:14
Mekong River12-Dec-04 14:14 
GeneralRe: TAB control change tab order Pin
Tom John13-Dec-04 2:59
Tom John13-Dec-04 2:59 
GeneralRe: TAB control change tab order Pin
Mekong River13-Dec-04 17:30
Mekong River13-Dec-04 17:30 
GeneralRe: TAB control change tab order Pin
Tom John13-Dec-04 22:00
Tom John13-Dec-04 22:00 
GeneralRe: TAB control change tab order Pin
Mekong River14-Dec-04 10:56
Mekong River14-Dec-04 10:56 
GeneralRe: TAB control change tab order Pin
Tom John14-Dec-04 22:58
Tom John14-Dec-04 22:58 
GeneralNeed rectangle box with little compartments. Pin
preesteya12-Dec-04 14:12
preesteya12-Dec-04 14:12 
GeneralRe: Need rectangle box with little compartments. Pin
Tom John13-Dec-04 0:27
Tom John13-Dec-04 0:27 
QuestionHow to call a MouseEvent from a Keyboard Event Handler? Pin
manivannan.p12-Dec-04 6:29
manivannan.p12-Dec-04 6:29 
AnswerRe: How to call a MouseEvent from a Keyboard Event Handler? Pin
Dave Kreskowiak13-Dec-04 2:05
mveDave Kreskowiak13-Dec-04 2:05 
GeneralBeginner: converting stings and integers Pin
toefuti11-Dec-04 23:45
toefuti11-Dec-04 23:45 
GeneralRe: Beginner: converting stings and integers Pin
Edbert P12-Dec-04 13:37
Edbert P12-Dec-04 13:37 
GeneralRe: Beginner: converting stings and integers Pin
John Santora18-Dec-04 17:10
sussJohn Santora18-Dec-04 17:10 
GeneralRe: User Controls? Pin
Mekong River11-Dec-04 16:47
Mekong River11-Dec-04 16:47 
GeneralRe: User Controls? Pin
hermitbluedog12-Dec-04 2:14
hermitbluedog12-Dec-04 2:14 
Questionmore efficient mp3 control? Pin
Nadroj11-Dec-04 7:42
Nadroj11-Dec-04 7:42 
Generalvb dll Pin
Member 158209411-Dec-04 7:30
Member 158209411-Dec-04 7:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.