Solved

VB6 Save Excel Error

Posted on 2007-12-05
5
519 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
[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
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

738 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