Solved

How to Setup Excel sheet object in visual basic 6?

Posted on 2014-02-11
14
1,008 Views
Last Modified: 2014-02-27
Hi guys,

I don't know to much about Excel objects, my code works good but I only need to do the following:

How to:
Set the sheet = landscape
set Horizontal alignment on a cell = Center
set vertical alignment on a cell = Center
Set borders on my range
Set font and size.

This is my code:

Sub ExportaaExcel()
    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object
    Dim nRenglon As Integer
    Dim nContador As Integer
    Dim cCambio As String
    Dim nConsecutivo As Integer
    
    cpNombreArchivo = cpNombreArchivo & "_" & cFecha & "_" & cHora & ".xls"
    Set oExcel = CreateObject("Excel.Application")
    Set oBook = oExcel.Workbooks.Add
       
    Set oSheet = oBook.Worksheets(1)
    Select Case npOpcionReporte
        Case 1
            oSheet.Range("A1").Value = "CONSEC"
            oSheet.Range("B1").Value = "FECHA RECIBIDO"
            oSheet.Range("C1").Value = "NUMERO EXPEDIENTE"
            oSheet.Range("D1").Value = "AUTORIDAD SOLICITA"
            oSheet.Range("E1").Value = "PROMOVENTE"
            oSheet.Range("F1").Value = "SOLICITUD"
            oSheet.Range("G1").Value = "SEGUIMIENTO"
            nRenglon = 2
            nConsecutivo = 1

            Do While Not rsTransacciones.EOF
                oSheet.Range("A" & nRenglon).Value = nConsecutivo
                oSheet.Range("B" & nRenglon).Value = rsTransacciones!Fecha_recibido
                oSheet.Range("C" & nRenglon).Value = rsTransacciones!Num_expediente
                oSheet.Range("D" & nRenglon).Value = rsTransacciones!Autoridad_solicita
                oSheet.Range("E" & nRenglon).Value = rsTransacciones!Promovente
                oSheet.Range("F" & nRenglon).Value = rsTransacciones!Solicitud
                oSheet.Range("G" & nRenglon).Value = rsTransacciones!Seguimiento
                nRenglon = (nRenglon + 1)
                nConsecutivo = (nConsecutivo + 1)
                rsTransacciones.MoveNext
            Loop

            oSheet.Columns("A:G").WrapText = True
            oSheet.Range("A1:L1").Font.Bold = True
            oSheet.Range("A1:L1").Font.Bold = True
            oSheet.Columns("A:A").Columnwidth = 5
            oSheet.Columns("B:B").Columnwidth = 10
            oSheet.Columns("C:C").Columnwidth = 15
            oSheet.Columns("D:D").Columnwidth = 25
            oSheet.Columns("E:E").Columnwidth = 25
            oSheet.Columns("F:F").Columnwidth = 35
            oSheet.Columns("G:G").Columnwidth = 28
            oSheet.Range("A1:B1").Font.Bold = True
    End Select
    oBook.SaveAs "C:\Reportes\" & cpNombreArchivo
    oExcel.Quit
End Sub

Open in new window

0
Comment
Question by:elm9999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
14 Comments
 
LVL 3

Expert Comment

by:Conner Turner
ID: 39851515
Hello There,

You can use a With statement based on the active sheet to replicate the Macro within VBA:

With ActiveSheet.PageSetup
    .Orientation = xlLandscape
End With

Open in new window


You can see all functions an uses of the PageSetup function on the msdn website here:
[MSDN]
0
 

Author Comment

by:elm9999
ID: 39851550
If I use this:

With ActiveSheet.PageSetup
    .Orientation = xlLandscape
End With

Appears this error:  "variable not defined"  ActiveSheet.

If I use this:

With oSheet.PageSetup
    .Orientation = xlLandscape
End With


Appears this error:  "object required"  Orientation = xlLandscape

So, do I have to activate it first?
0
 

Author Comment

by:elm9999
ID: 39851636
If I use:

oSheet.PageSetup.Orientation = 2

Works fine but if I use xlLandscape instead of 2, the error occurs!
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 3

Expert Comment

by:Conner Turner
ID: 39851727
Hmm,

I suspense it make s no difference what you use , a s long as you don't need to relay or print it back to the sheet it should all be fine !

HTH,

Conner
0
 

Author Comment

by:elm9999
ID: 39851791
Yea it works but the thing is that I have to border too. and center text in the cells.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39851793
Since you use late binding (CreateObject("Excel.Application")), you do not have access to Excel constants. I don't think it is worth it, but if you want to have them available, instructions for including them can be found at http://support.microsoft.com/kb/112671 .

The other way is to use early binding, by adding a reference to Excel, allowing direct access to Excel objects and constants. Helpful instructions are posted at http://www.vbforums.com/showthread.php?391665-Tutorial-FAQ-How-to-Automate-Excel-from-VB6-(or-VB5-VBA) .

Using Active* isn't recommended. Whenever possible, use the var pointing to the object directly (i.e. oSheet). Using Active* tends to break on user interaction, e.g. accidental clicks or keystrokes.
0
 

Author Comment

by:elm9999
ID: 39851794
I tried this function:

Sub RedOutlineCells()
    Dim rng As Range

    Set rng = Range("A1:F20")

    With rng.Borders
        .LineStyle = xlContinuous
        .Color = vbRed
        .Weight = xlThin
    End With
End Sub

but the is like the vb6 doesn't recognize some object instructions.
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39851822
You'll need to prefix an object reference. Range needs e.g. a worksheet object it can get applied against:
   Set rng = oSheet.Range("A1:F20")

Open in new window

Directly using Range only works in Excel VBA.
0
 

Author Comment

by:elm9999
ID: 39851854
Thank you Qlemo,

Sometimes I hate Microsoft :)
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39851861
Sometimes? Lucky man ...
0
 

Author Comment

by:elm9999
ID: 39851911
All right Q I didn't know.

Can I change it?
0
 

Author Comment

by:elm9999
ID: 39851935
OK,

Thanks.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question