Solved

VB6 Save Excel Error

Posted on 2007-12-05
5
512 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)
ID: 20417097
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
ID: 20417996
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
ID: 20418145
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
ID: 20418298
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
ID: 31412969
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel 2016 Not Responding Issues 6 28
Excel 2016 formulas 5 26
Help with Excel formula 6 38
Google Sheets - Artificial Intelligence 2 6
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

910 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

22 Experts available now in Live!

Get 1:1 Help Now