• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

VB6 Save Excel Error

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
neelyjer
Asked:
neelyjer
  • 2
  • 2
2 Solutions
 
Robberbaron (robr)Commented:
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
 
GrahamSkanCommented:
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
 
Robberbaron (robr)Commented:
Graham, does this work ?
GetWkEnd.SaveAs Filename       GetWkEnd is the excel object , not workbook.
0
 
GrahamSkanCommented:
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
 
neelyjerAuthor Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now