|
-
Oct 18th, 2008, 01:45 AM
#1
Thread Starter
Lively Member
Problem in Form unload procedure.
Friends,
I am using the code as below for exporting and importing data from an excel file to a Flexgrid.The problem i have is in Form unload procedure.If i use "Save" as in line "m_oXlWb.Close SaveChanges:=True", i get a pop up message asking for saving into copy of flexgrid file.If i use "Save as" as in line
" m_oXlWb.SaveAs 'FileName:=m_csFileName" i get message asking whether to replace existing flexgrid.xls file.I want the save to happen to same file without replacing every time and without giving any popup messages.Please help me .It's very urgent.Thanks in advance.
Private Const m_csFileName As String = "D:\FlexGridData.xls"
Private Const m_clCols As Long = 5
Private Const m_clRows As Long = 21
Private m_oXlApp As Object
Private m_oXlWb As Object
Private m_oXlWs As Object
Private Sub Form_Load()
Dim intLoopIndex As Integer
Dim vData As Variant
Dim lRow As Long, lCol As Long
With MSFlexGrid1
.Cols = 5
.Row = 0
.Col = 1
.Text = "Text1"
.Col = 2
.Text = "Text2"
.Col = 3
.Text = "Text3"
.Col = 4
.Text = "Text4"
End With
MSFlexGrid1.Rows = 20
For intLoopIndex = 1 To MSFlexGrid1.Rows - 1
MSFlexGrid1.Col = 0
MSFlexGrid1.Row = intLoopIndex
MSFlexGrid1.Text = "SAVE " & Str(intLoopIndex)
Next intLoopIndex
Set m_oXlApp = CreateObject("Excel.Application")
Set m_oXlWb = m_oXlApp.Workbooks.Open(FileName:=m_csFileName)
Set m_oXlWs = m_oXlWb.ActiveSheet
Set vData = m_oXlWs.Cells(1, 1).Resize(m_clRows, m_clCols)
m_oXlApp.Visible = False
With MSFlexGrid1
.FixedRows = 1
.FixedCols = 0
.Rows = m_clRows + 1
.Cols = m_clCols
For lRow = 1 To m_clRows
For lCol = 2 To m_clCols
.TextMatrix(lRow, lCol - 1) = vData(lRow, lCol)
Next lCol
Next lRow
End With
m_oXlWb.Close SaveChanges:=False
m_oXlApp.Quit
Set m_oXlWs = Nothing
Set m_oXlWb = Nothing
Set m_oXlApp = Nothing
End Sub
Private Sub Form_Unload()
Dim i As Long
Dim p As Long
Dim newCell As String
Set m_oXlApp = CreateObject("Excel.Application")
Set m_oXlWb = m_oXlApp.Workbooks.Open("D:\FlexGridData.xls")
Set m_oXlWs = m_oXlWb.ActiveSheet
m_oXlApp.Visible = False
For i = 1 To MSFlexGrid1.Cols - 1
For p = 1 To MSFlexGrid1.Rows - 1
MSFlexGrid1.Row = p
MSFlexGrid1.Col = i
newCell = Chr(i + 64) & p
m_oXlWb.Worksheets(1).Range(newCell).Value = MSFlexGrid1.Text
Next
Next
'm_oXlWb.Close SaveChanges:=True
m_oXlWb.SaveAs 'FileName:=m_csFileName
m_oXlApp.Quit
Set m_oXlWs = Nothing
Set m_oXlWb = Nothing
Set m_oXlApp = Nothing
Unload Form1
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|