Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default AutoFit not working in vb.net when creating a xls


I have an application that pulls the data from a datagridview and creates an
xls
using the following code. I need to set the col with to autofit and then
protect all col
but S,T,U. but I cant get the autofit to work or to set width... Help Please
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)
nTotalRowCount = rowcount
nCurrentRowCount = 0
nRowPointer = 0
'------------end the row count

oInsertRange = oSheet.Range("A2")

Do While nCurrentRowCount < nTotalRowCount

nRowsInArray = nTotalRowCount - nCurrentRowCount
If nRowsInArray 20000 Then
nCurrentRowCount = nCurrentRowCount + 20000
nRowsInArray = 20000
nRowPointer = nCurrentRowCount - nRowsInArray
Else
nCurrentRowCount = nTotalRowCount
End If

ReDim DataArray(nRowsInArray - 1, colcount)

For r = 0 To nRowsInArray - 1

For c = 0 To colcount - 1
DataArray(r, c) = DataGridView1(c, nRowPointer +
r).Value.ToString
Next
Next

oInsertRange = oInsertRange.Resize(nRowsInArray, colcount)
oInsertRange.Value = DataArray

oInsertRange = oInsertRange.Offset(nRowsInArray, 0)
Loop
oExcel.Visible = True
''''''''''''Save the Workbook and quit Excel.
oRange = oSheet.range("S1").entireColumn
oRange.locked = False
oRange.interior.colorindex = 36

oRange = oSheet.range("T1").entireColumn
oRange.locked = False
oRange.interior.colorindex = 36
oRange = oSheet.range("U1").entireColumn
oRange.locked = False
oRange.interior.colorindex = 36

oSheet.protect("mypassword")
oBook.SaveAs(sSampleFolder & "Book1.xls")

oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()





  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default AutoFit not working in vb.net when creating a xls

oSheet.Autofit
oSheet.protect("mypassword")
oBook.SaveAs(sSampleFolder & "Book1.xls")

Hopefully you don't have merged cells.

--
Regards,
Tom Ogilvy


"ljhopkins_LOST and Cant find My Way" wrote:


I have an application that pulls the data from a datagridview and creates an
xls
using the following code. I need to set the col with to autofit and then
protect all col
but S,T,U. but I cant get the autofit to work or to set width... Help Please
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)
nTotalRowCount = rowcount
nCurrentRowCount = 0
nRowPointer = 0
'------------end the row count

oInsertRange = oSheet.Range("A2")

Do While nCurrentRowCount < nTotalRowCount

nRowsInArray = nTotalRowCount - nCurrentRowCount
If nRowsInArray 20000 Then
nCurrentRowCount = nCurrentRowCount + 20000
nRowsInArray = 20000
nRowPointer = nCurrentRowCount - nRowsInArray
Else
nCurrentRowCount = nTotalRowCount
End If

ReDim DataArray(nRowsInArray - 1, colcount)

For r = 0 To nRowsInArray - 1

For c = 0 To colcount - 1
DataArray(r, c) = DataGridView1(c, nRowPointer +
r).Value.ToString
Next
Next

oInsertRange = oInsertRange.Resize(nRowsInArray, colcount)
oInsertRange.Value = DataArray

oInsertRange = oInsertRange.Offset(nRowsInArray, 0)
Loop
oExcel.Visible = True
''''''''''''Save the Workbook and quit Excel.
oRange = oSheet.range("S1").entireColumn
oRange.locked = False
oRange.interior.colorindex = 36

oRange = oSheet.range("T1").entireColumn
oRange.locked = False
oRange.interior.colorindex = 36
oRange = oSheet.range("U1").entireColumn
oRange.locked = False
oRange.interior.colorindex = 36

oSheet.protect("mypassword")
oBook.SaveAs(sSampleFolder & "Book1.xls")

oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default AutoFit not working in vb.net when creating a xls

vb.net puts oSheet.Autofit()
and it blows up
I did do this and it worked

Dim ColX As Integer = 1
Dim oCol As Object

For ColX = 1 To colcount
oCol = oSheet.cells.item(1, ColX).EntireColumn.autofit
Next

oSheet.protect("mypassword")

"Tom Ogilvy" wrote:

oSheet.Autofit
oSheet.protect("mypassword")
oBook.SaveAs(sSampleFolder & "Book1.xls")

Hopefully you don't have merged cells.

--
Regards,
Tom Ogilvy


"ljhopkins_LOST and Cant find My Way" wrote:


I have an application that pulls the data from a datagridview and creates an
xls
using the following code. I need to set the col with to autofit and then
protect all col
but S,T,U. but I cant get the autofit to work or to set width... Help Please
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)
nTotalRowCount = rowcount
nCurrentRowCount = 0
nRowPointer = 0
'------------end the row count

oInsertRange = oSheet.Range("A2")

Do While nCurrentRowCount < nTotalRowCount

nRowsInArray = nTotalRowCount - nCurrentRowCount
If nRowsInArray 20000 Then
nCurrentRowCount = nCurrentRowCount + 20000
nRowsInArray = 20000
nRowPointer = nCurrentRowCount - nRowsInArray
Else
nCurrentRowCount = nTotalRowCount
End If

ReDim DataArray(nRowsInArray - 1, colcount)

For r = 0 To nRowsInArray - 1

For c = 0 To colcount - 1
DataArray(r, c) = DataGridView1(c, nRowPointer +
r).Value.ToString
Next
Next

oInsertRange = oInsertRange.Resize(nRowsInArray, colcount)
oInsertRange.Value = DataArray

oInsertRange = oInsertRange.Offset(nRowsInArray, 0)
Loop
oExcel.Visible = True
''''''''''''Save the Workbook and quit Excel.
oRange = oSheet.range("S1").entireColumn
oRange.locked = False
oRange.interior.colorindex = 36

oRange = oSheet.range("T1").entireColumn
oRange.locked = False
oRange.interior.colorindex = 36
oRange = oSheet.range("U1").entireColumn
oRange.locked = False
oRange.interior.colorindex = 36

oSheet.protect("mypassword")
oBook.SaveAs(sSampleFolder & "Book1.xls")

oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default AutoFit not working in vb.net when creating a xls

osheet.Columns.Autofit

was what I intended - not sure how the Columns part got left out.

I definitely wouldn't loop through the columns.

--
Regards,
Tom Ogilvy


"ljhopkins_LOST and Cant find My Way" wrote:

vb.net puts oSheet.Autofit()
and it blows up
I did do this and it worked

Dim ColX As Integer = 1
Dim oCol As Object

For ColX = 1 To colcount
oCol = oSheet.cells.item(1, ColX).EntireColumn.autofit
Next

oSheet.protect("mypassword")

"Tom Ogilvy" wrote:

oSheet.Autofit
oSheet.protect("mypassword")
oBook.SaveAs(sSampleFolder & "Book1.xls")

Hopefully you don't have merged cells.

--
Regards,
Tom Ogilvy


"ljhopkins_LOST and Cant find My Way" wrote:


I have an application that pulls the data from a datagridview and creates an
xls
using the following code. I need to set the col with to autofit and then
protect all col
but S,T,U. but I cant get the autofit to work or to set width... Help Please
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)
nTotalRowCount = rowcount
nCurrentRowCount = 0
nRowPointer = 0
'------------end the row count

oInsertRange = oSheet.Range("A2")

Do While nCurrentRowCount < nTotalRowCount

nRowsInArray = nTotalRowCount - nCurrentRowCount
If nRowsInArray 20000 Then
nCurrentRowCount = nCurrentRowCount + 20000
nRowsInArray = 20000
nRowPointer = nCurrentRowCount - nRowsInArray
Else
nCurrentRowCount = nTotalRowCount
End If

ReDim DataArray(nRowsInArray - 1, colcount)

For r = 0 To nRowsInArray - 1

For c = 0 To colcount - 1
DataArray(r, c) = DataGridView1(c, nRowPointer +
r).Value.ToString
Next
Next

oInsertRange = oInsertRange.Resize(nRowsInArray, colcount)
oInsertRange.Value = DataArray

oInsertRange = oInsertRange.Offset(nRowsInArray, 0)
Loop
oExcel.Visible = True
''''''''''''Save the Workbook and quit Excel.
oRange = oSheet.range("S1").entireColumn
oRange.locked = False
oRange.interior.colorindex = 36

oRange = oSheet.range("T1").entireColumn
oRange.locked = False
oRange.interior.colorindex = 36
oRange = oSheet.range("U1").entireColumn
oRange.locked = False
oRange.interior.colorindex = 36

oSheet.protect("mypassword")
oBook.SaveAs(sSampleFolder & "Book1.xls")

oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel autofit row height not working Josafa Excel Worksheet Functions 0 February 8th 10 02:05 PM
Excel 2007 AutoFit Row Height not working KristinQ Excel Discussion (Misc queries) 5 March 11th 09 06:43 PM
Autofit for row height is not working. Why? Leo Excel Discussion (Misc queries) 3 July 28th 08 06:43 PM
Wrap text and row autofit not working in Excel dclink9 Excel Discussion (Misc queries) 1 June 23rd 06 03:11 AM
format-row-autofit not working Redheads Rule Excel Worksheet Functions 2 July 23rd 05 03:38 AM


All times are GMT +1. The time now is 07:18 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"