Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 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 November 10th, 2006, 03:02 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 180
Thanks: 1
Thanked 1 Time in 1 Post
Send a message via ICQ to dpkbahuguna Send a message via MSN to dpkbahuguna Send a message via Yahoo to dpkbahuguna
Default help me with excel

 Hi all.....
   help me here........

        exception in red text ........

  and exception is
                           [ Run-time error - '91':
                             Pbject variable or with block variable not set ]

Private Sub Form_Load()
    Dim wb As Excel.Workbook, app1 As New Excel.Application
    Dim code As String, a As String, loc As Integer, i As Integer
    Dim rowno As Integer, price As Double
    Dim fsys As New Scripting.FileSystemObject
    Dim exist As String
    Set wb = app1.Workbooks.Add
    app1.Visible = True
    ActiveWorkbook.Worksheets(1).Name = "Price List2"
    ActiveSheet.Cells(1, 4).Font.Bold = True
    ActiveSheet.Cells(1, 4) = "PRICE LIST"
    ActiveSheet.Range("a4:dc5").Interior.Color = 16744448
    ActiveSheet.Range("a5:dc5").Interior.Color = RGB(220, 220, 220)
    Set abc = fsys.GetFile("c:\temp\xyz.xls")
    abc.Attributes = Normal
    'fsys.DeleteFile ("c:\temp\exp.xls")
    fsys.DeleteFile ("c:\temp\xyz.xls")
    'wb.SaveAs FileName:="C:\temp\exp.xls", FileFormat:=xlNormal, CreateBackup:=True
    wb.SaveAs FileName:="c:\temp\xyz.xls", FileFormat:=xlNormal, CreateBackup:=True
    app1.ActiveWorkbook.Save
    app1.Quit



    myfunction
End Sub

Private Sub myfunction()
    Dim wb As Excel.Workbook, app1 As New Excel.Application
    Dim code As String, a As String, loc As Integer, i As Integer
    Dim rowno As Integer, price As Double
    Dim exist As String
    Set wb = app1.Workbooks.Add
    app1.Visible = True
    ActiveWorkbook.Worksheets(1).Select
    ActiveWorkbook.Worksheets(1).Name = "Price List2"
    ActiveSheet.Cells(1, 4).Font.Bold = True
    ActiveSheet.Cells(1, 4) = "PRICE LIST"
    ActiveSheet.Range("a4:dc5").Interior.Color = 16744448
    ActiveSheet.Range("a5:dc5").Interior.Color = RGB(220, 220, 220)
    Set abc = fsys.GetFile(App.Path & "\xyz.xls")
    abc.Attributes = Normal
    'fsys.DeleteFile ("c:\temp\exp.xls")
    fsys.DeleteFile (App.Path & "\xyz.xls")
    'wb.SaveAs FileName:="C:\temp\exp.xls", FileFormat:=xlNormal, CreateBackup:=True
    wb.SaveAs FileName:=App.Path & "\xyz.xls", FileFormat:=xlNormal, CreateBackup:=True
    app1.ActiveWorkbook.Save
    app1.Quit
End Sub

DPK..
__________________
DPK..
 
Old November 10th, 2006, 03:17 PM
Registered User
 
Join Date: Nov 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

to reference the ActiveWorkbook you need to put app1.ActiveWorkbook. Same for activesheet or activecell etc...
or else you can put them in a with block:

With app1
    .ActiveWorkbook......
    .......
End With

or else declare a variable to point to the object in question like you did already with:

Set wb = app1.Workbooks.Add
 
Old November 11th, 2006, 12:29 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 180
Thanks: 1
Thanked 1 Time in 1 Post
Send a message via ICQ to dpkbahuguna Send a message via MSN to dpkbahuguna Send a message via Yahoo to dpkbahuguna
Default

Thanks bluenose!!!

  Now the programe running perfectly...
  but i am not able to get how the code of Form_Load is running good and why was MyFunction's code giving me error while both functions had same code. Pls will you make me understand why this is..?

  I mean to ask in my form_load i am not prefixing app1 to ActiveWorkbook.Worksheets(1).Select but it is running so why it is demanding for app1 in MyFunction......

  I hope you are getting me what i want to say.....

  waiting for your reply....:)

DPK..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in opening excel file in MS Excel 2000 kallol Visual C++ 0 November 16th, 2007 05:48 AM
ASP to Excel - excel output formatting issue mat41 Classic ASP Professional 0 August 13th, 2006 06:41 AM
write multi-sheet Excel w/o Excel.Application manmoth Classic ASP Components 2 November 22nd, 2005 10:56 AM
Excel in win 2000 to excel winxp (excel 2002) Max Excel VBA 3 August 28th, 2003 04:44 AM





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