Solved

VB6 Save Excel Error

Posted on 2007-12-05
5
511 Views
Last Modified: 2010-04-21
Hey there Experts.  I have a snippet of code below that is causing me a bit of trouble and generally has me at a loss.  I am attempting to verify if 2 files exist.  If the first file does not exist, open a template file and save it with the variable filename provided in the code.  If the second file does not exist, open a different file, not the template, and save it as VRE.End_Month.xls, where Month a variable fully spelled month.  The first half of the code works great.  It looks for the file, and if it cannot find, it opens the template file, and saves it under a different name.  The second half of the code is where the problems begin...

The code is able to verify if the file exists or not.  If it cannot find the file, it attempts to create a new by opening the file provided in the variables and saving that file under a different name.  the code is able to open the source file, however it fails while attempting to save.  I am getting the error as follows:

Run-time error '91':
Object variable or With block not set

I pulling my freaking hair out at this point.  Any assistance that any of you can provide will be most appreciated.

Thanks.  here's my code:


Dim Template
Dim VREMnthStrt
Dim VRENewMnth

    VREMnthStrt = "VRE." & VREYearYY & VREMonthMM & "01.xls"
   
With Application.FileSearch
    .NewSearch
    .FileName = VREMnthStrt
    .LookIn = "G:\Volume\Stats\" & VREYear & "\" & VREMonth & "\"
    .SearchSubFolders = False
    If .Execute() > 0 Then
    Else
        Template = "G:\Volume\Templates\VRETempWk"
        Set VRENewMnth = CreateObject("Excel.application")
        VRENewMnth.Workbooks.Open Template
        ChDir "G:\Volume\Stats\" & VREYear & "\" & VREMonth & "\"
        ActiveWorkbook.SaveAs FileName:="G:\Volume\Stats\" & VREYear & "\" & VREMonth & "\" & VREMnthStrt, _
        Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
        CreateBackup:=False
        ActiveWorkbook.Close
        Workbooks.Close
    End If
End With
End If

    '***************************************************************************************************
    '***    Open VRE.YYMMDD.xls & save as VRE.End_<Month>.xls
    '***************************************************************************************************

If PrevSun = True And NewMnth = True Then
   
Dim SunFile
Dim MnthEnd
Dim GetWkEnd
Dim SunDest

    SunFile = "VRE." & VREWeek & ".xls"
    MnthEnd = "VRE.End_" & VREPrevMnth & ".xls"
    SunDest = "G:\Volume\Stats\" & VRPrevYear & "\" & VREPrevMnth & "\"
   
With Application.FileSearch
    .NewSearch
    .FileName = MnthEnd
    .LookIn = SunDest
    .SearchSubFolders = False
    If .Execute() > 0 Then
    Else
        Template = SunDest & SunFile
        Set GetWkEnd = CreateObject("Excel.application")
        GetWkEnd.Workbooks.Open SunFile
        ChDir SunDest

    '***************************************************************************************************************************
    '*** The code works great up to this point.  However, the code fails while attempting to save.
    '*** The error highlights the following section of code, starting here....
    '***************************************************************************************************************************
        ActiveWorkbook.SaveAs FileName:=SunDest & MnthEnd, _
        Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
        CreateBackup:=False
    '***************************************************************************************************************************
    '***  and ending here...
    '***************************************************************************************************************************
        ActiveWorkbook.Close
        Workbooks.Close
    End If
End With
End If
0
Comment
Question by:neelyjer
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:Robberbaron (robr)
Comment Utility
often i get this because i've not closed an if statement but i dont think that is your problem

ActiveWorkbooks is not a valid object in VB.  Only in excel VBA.



Dim sNameNew As String

    Set GetWkEnd = CreateObject("Excel.application")

    Set myBook = GetWkEnd.Workbooks.Open(SunFile)

     ChDir SunDest
 

    sNameNew = SunDest & MnthEnd

    MsgBox "Check new name=<" & sNameNew & ">", vbQuestion

    

    myBook.SaveAs Filename:=sNameNew, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
I like to use the specific object variables for objects such as workbooks and worksheets objects, where possible, rather than relying on whether they are active or not.
With Application.FileSearch

    .NewSearch

    .Filename = MnthEnd

    .LookIn = SunDest

    .SearchSubFolders = False

    If .Execute() > 0 Then

    Else

        Template = SunDest & SunFile

        Set GetWkEnd = CreateObject("Excel.application")

        GetWkEnd.Workbooks.Open SunFile

        ChDir SunDest
 

    '***************************************************************************************************************************

    '*** The code works great up to this point.  However, the code fails while attempting to save.

    '*** The error highlights the following section of code, starting here....

    '***************************************************************************************************************************

        GetWkEnd.SaveAs Filename:=SunDest & MnthEnd, _

        Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _

        CreateBackup:=False

    '***************************************************************************************************************************

    '***  and ending here...

    '***************************************************************************************************************************

        GetWkEnd.Close

        Workbooks.Close

    End If

End With

Open in new window

0
 
LVL 32

Assisted Solution

by:Robberbaron (robr)
Robberbaron (robr) earned 100 total points
Comment Utility
Graham, does this work ?
GetWkEnd.SaveAs Filename       GetWkEnd is the excel object , not workbook.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 400 total points
Comment Utility
You are right, robberbaron, thank you. The declarations are all Variants, and the naming doesn't help much.
Hopefully this is better.

With Application.FileSearch

    .NewSearch

    .Filename = MnthEnd

    .LookIn = SunDest

    .SearchSubFolders = False

    If .Execute() > 0 Then

    Else

        Template = SunDest & SunFile

        Set GetWkEnd = CreateObject("Excel.application")

    Dim wbkSunFile as Workbook
 

    Set wbkSunFile = GetWkEnd.Workbooks.Open(SunFile)

        ChDir SunDest

 

    '***************************************************************************************************************************

    '*** The code works great up to this point.  However, the code fails while attempting to save.

    '*** The error highlights the following section of code, starting here....

    '***************************************************************************************************************************

        wbkSunFile.SaveAs Filename:=SunDest & MnthEnd, _

        Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _

        CreateBackup:=False

    '***************************************************************************************************************************

    '***  and ending here...

    '***************************************************************************************************************************

        wbkSunFile.Close

        Workbooks.Close

    End If

End With

Open in new window

0
 

Author Closing Comment

by:neelyjer
Comment Utility
Thanks so much for your help everyone.  The problem is finally fixed.
Graham,
Your code worked perfectly.  All I had to do was declare the new variables.  Thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now