Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 30th, 2006, 03:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jmss66
Default New Excel Spreadsheet Column width

I have two reports to create and they both have different column widths in Excel. My problem is, if I set the column width in one worksheet it seems like it is copying the same width to the second report. Below is the code I am using, maybe somebody can tell me what I am doing wrong.

Private sub cmd_first_report()
Dim excelVersion As Integer
Dim objexcel As Object
Dim objTemp As Object
Dim intFontsize As Integer

'--Create reference variable for the spreadsheet
Set objexcel = CreateObject("Excel.application")
objexcel.Visible = True
objexcel.Workbook.Add

'--Set Page Orientation to Landscape

    With ActiveSheet.PageSetup
        .PrintTitleRows = ""
        .PrintTitleColumns = ""
    End With
    ActiveSheet.PageSetup.PrintArea = ""
    With ActiveSheet.PageSetup
        .LeftHeader = ""
        .CenterHeader = ""
        .RightHeader = ""
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = ""
        .LeftMargin = Application.InchesToPoints(0.75)
        .RightMargin = Application.InchesToPoints(0.75)
        .TopMargin = Application.InchesToPoints(1)
        .BottomMargin = Application.InchesToPoints(1)
        .HeaderMargin = Application.InchesToPoints(0.5)
        .FooterMargin = Application.InchesToPoints(0.5)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .PrintQuality = 600
        .CenterHorizontally = False
        .CenterVertically = False
        .Orientation = xlLandscape
        .Draft = False
        .PaperSize = xlPaperLetter
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = 100
        .PrintErrors = xlPrintErrorsDisplayed
    End With



'We need this line to ensure Excel remains visible if we switch to the active sheet
Set objTemp = objexcel


excelVersion = Val(objexcel.Application.Version)
If (excelVersion >= 8) Then
   Set objexcel = objexcel.ActiveSheet
End If


'--Set Column Width here
   Call Column_Width("A", 1, 18)
   Call Column_Width("B", 1, 20)
   Call Column_Width("C", 1, 12.85)
   Call Column_Width("D", 1, 12.85)
   Call Column_Width("E", 1, 8.45)
   Call Column_Width("F", 1, 9.6)
   Call Column_Width("G", 1, 9.86)
   Call Column_Width("H", 1, 12.45)
   Call Column_Width("I", 1, 11)

'--Start First Report here

codes....

End sub

Private sub cmd_Second_Report()


Dim excelVersion As Integer
Dim objexcel As Object
Dim objTemp As Object
Dim intFontsize As Integer


'--Create reference variable for the spreadsheet
Set objexcel = CreateObject("Excel.application")
objexcel.Visible = True
objexcel.Workbooks.Add

'We need this line to ensure Excel remains visible if we switch to the active sheet
Set objTemp = objexcel

excelVersion = Val(objexcel.Application.Version)
If (excelVersion >= 8) Then
   Set objexcel = objexcel.ActiveSheet
End If

'--Set Column Width here
   Call Column_Width("A", 1, 13.29)
   Call Column_Width("B", 1, 12.71)
   Call Column_Width("C", 1, 8)
   Call Column_Width("D", 1, 8.43)
   Call Column_Width("E", 1, 1.57)
   Call Column_Width("F", 1, 11.86)
   Call Column_Width("G", 1, 8.43)

'--Start Per Diem Report Here

codes...

End Sub


If the second report is chosen by the user first, the column width will be copied to the first report.

What am I doing wrong? Help...


Cheers,
jmss66


 
Old June 30th, 2006, 05:10 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Do you see two instances in the task bar when both reports are open? (I'm wondering if perhaps VB is obtaining a 2nd reference to the running instance of Excel, despite your explicit CreateObject() as opposed to GetObject...

Is Column_Width a routine that you have written? If not, is it in the VB environment in which you are writing, or is it something that Excel itself provides?
 
Old July 3rd, 2006, 06:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jmss66
Default

Hi Brian,

I see two instances of Excel open when I click on both command button and yes column_width is a routine I wrote which passes the size of the columns. Below is the column_width rtn.

Public Sub Column_Width(strColumn As String, intRow As Integer, dblColWidth As Double)

    Dim strColumnWidth As String

    strColumnWidth = strColumn & intRow
    Range(strColumnWidth).Select
    Selection.ColumnWidth = dblColWidth

End Sub

Thanks,
Judy
 
Old July 5th, 2006, 03:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jmss66
Default

Anybody please help me on this.

Thanks,
jmss66

 
Old July 10th, 2006, 10:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jmss66
Default

Still no takers?






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to set Column width harapraveen XSLT 2 October 23rd, 2007 03:14 AM
Setting Column Width r_ganesh76 Excel VBA 2 February 2nd, 2005 06:06 AM
DataGrid column width johanyu VS.NET 2002/2003 0 October 30th, 2004 05:29 AM
Column width immanuelsamuel Oracle 2 March 13th, 2004 10:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.