Solved

VB6 Save Excel Error

Posted on 2007-12-05
5
516 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

679 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