Results 1 to 3 of 3

Thread: Opening an .xlsm file not working inside if statement

  1. #1
    verkisto is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2012
    Posts
    2
    Reputation
    0

    Opening an .xlsm file not working inside if statement

    I'm putting together a program that will open an .xlsm file, copy some data from other workbooks into the .xlsm file, and then run two macros in the .xlsm file. This is the code I've put together to accomplish this:

    Code:
    Private Sub TERM_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load
    
    Dim xlobjApp AsNew Excel.Application()
    Dim originWbkIR, originWbkCPR, destinationWbk As Excel.Workbook
    Dim originWshtIR, originWshtCPR, destinationWshtIR, destinationWshtCPR As Excel.Worksheet
    Dim myDocs AsString
    Dim originWshtIRRowCount, originWshtCPRRowCount AsInteger
    
    'Get path for user's My Documents folder.
    myDocs = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    
    'Open the IR-Data Dump file and get the rowcount for used rows.
    originWbkIR = xlobjApp.Workbooks.Open(myDocs & "\IR - Data Dump.xls")
    originWshtIR = originWbkIR.Sheets("IR - Data Dump")
    originWshtIRRowCount = originWshtIR.UsedRange.Rows.Count
    
    destinationWbk = xlobjApp.Workbooks.Open(myDocs & "\TERM_macro_15_feb_2012_2010.xlsm")
    destinationWshtIR = destinationWbk.Sheets("UPDATED POTENTIAL")
    destinationWshtCPR = destinationWbk.Sheets("Export Worksheet")
    
    'Copy the data from the IR-Data Dump report and paste it into the UPDATED POTENTIAL worksheet in the TERM Macro file.
    originWshtIR.Cells.Range("A9", "AH" & originWshtIRRowCount).Copy()
    releaseObject(originWshtIR)
    destinationWshtIR.Activate()
    destinationWshtIR.Cells.Range("A9", "AH" & originWshtIRRowCount).Select()
    destinationWshtIR.Paste()
    
    'Clear the clipboard to prevent the message from popping up when closing the IR-Data Dump file.
    My.Computer.Clipboard.Clear()
    
    'Close the IR-Data Dump file.
    originWbkIR.Close()
    
    'Open the CPR-Data Dump file and get the rowcount for used rows.
    originWbkCPR = xlobjApp.Workbooks.Open(myDocs & "\CPR - Data Dump.xls")
    originWshtCPR = originWbkCPR.Sheets("CPR - Data Dump")
    originWshtCPRRowCount = originWshtCPR.UsedRange.Rows.Count
    
    'Copy the data from the CPR-Data Dump report and paste it into the Export Worksheet worksheet in the TERM Macro file.
    originWshtCPR.Cells.Range("A9", "CK" & originWshtCPRRowCount).Copy()
    releaseObject(originWshtCPR)
    destinationWshtCPR.Activate()
    destinationWshtCPR.Cells.Range("A9", "CK" & originWshtCPRRowCount).Select()
    destinationWshtCPR.Paste()
    
    'Clear the clipboard to prevent the message from popping up when closing the CPR-Data Dump file.
    My.Computer.Clipboard.Clear()
    
    'Close the CPR-Data Dump file.
    originWbkCPR.Close()
    
    'Save the TERM Macro file.
    destinationWbk.Save()
    
    'Run the macros.
    xlobjApp.Run("Potential_Risk_status_report")
    xlobjApp.Run("TERM_status_report")
    
    'Close the TERM Macro file
    destinationWbk.Saved = True
    destinationWbk.Close()
    
    'Close Excel.
    xlobjApp.Quit()
    
    'Close form.
    Me.Close()
    By itself, this code works just fine. It doesn't create problems until I try to embed that into an if-else statement, to see if the file is already open. In that case, I embed the file into this code:

    Code:
    If fileOpen(myDocs & "\TERM_macro_15_feb_2012_2010.xlsm") = False Then
    'Open the IR-Data Dump file and get the rowcount for used rows.
    originWbkIR = xlobjApp.Workbooks.Open(myDocs & "\IR - Data Dump.xls")
    originWshtIR = originWbkIR.Sheets("IR - Data Dump")
    originWshtIRRowCount = originWshtIR.UsedRange.Rows.Count
    
    destinationWbk = xlobjApp.Workbooks.Open(myDocs & "\TERM_macro_15_feb_2012_2010.xlsm")
    destinationWshtIR = destinationWbk.Sheets("UPDATED POTENTIAL")
    destinationWshtCPR = destinationWbk.Sheets("Export Worksheet")
    
    'Copy the data from the IR-Data Dump report and paste it into the UPDATED POTENTIAL worksheet in the TERM Macro file.
    originWshtIR.Cells.Range("A9", "AH" & originWshtIRRowCount).Copy()
    releaseObject(originWshtIR)
    destinationWshtIR.Activate()
    destinationWshtIR.Cells.Range("A9", "AH" & originWshtIRRowCount).Select()
    destinationWshtIR.Paste()
    
    'Clear the clipboard to prevent the message from popping up when closing the IR-Data Dump file.
    My.Computer.Clipboard.Clear()
    
    'Close the IR-Data Dump file.
    originWbkIR.Close()
    
    'Open the CPR-Data Dump file and get the rowcount for used rows.
    originWbkCPR = xlobjApp.Workbooks.Open(myDocs & "\CPR - Data Dump.xls")
    originWshtCPR = originWbkCPR.Sheets("CPR - Data Dump")
    originWshtCPRRowCount = originWshtCPR.UsedRange.Rows.Count
    
    'Copy the data from the CPR-Data Dump report and paste it into the Export Worksheet worksheet in the TERM Macro file.
    originWshtCPR.Cells.Range("A9", "CK" & originWshtCPRRowCount).Copy()
    releaseObject(originWshtCPR)
    destinationWshtCPR.Activate()
    destinationWshtCPR.Cells.Range("A9", "CK" & originWshtCPRRowCount).Select()
    destinationWshtCPR.Paste()
    
    'Clear the clipboard to prevent the message from popping up when closing the CPR-Data Dump file.
    My.Computer.Clipboard.Clear()
    
    'Close the CPR-Data Dump file.
    originWbkCPR.Close()
    
    'Save the TERM Macro file.
    destinationWbk.Save()
    
    'Run the macros.
    xlobjApp.Run("Potential_Risk_status_report")
    xlobjApp.Run("TERM_status_report")
    
    'Close the TERM Macro file
    destinationWbk.Saved = True
    destinationWbk.Close()
    
    'Close Excel.
    xlobjApp.Quit()
    
    'Close form.
    Me.Close()
    
    Else
    
    MsgBox("Please close TERM_macro_15_feb_2012_2010.xlsm and run the program again.")
    Me.Close()
    
    End If
    In that case, I get the following error message:

    "Excel cannot open the file 'TERM_macro_15_feb_2012_2010.xlsm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

    I'm at a bit of a loss here. I've tried switching the expression and the order of the if-else statement, but still get that same message. The file is correct and has the proper extension (as I said, it runs outside of the if-then statement). Does anyone have any suggestions on how to get rid of this error message?

    Thanks in advance!

  2. #2
    verkisto is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2012
    Posts
    2
    Reputation
    0
    Also, here is my fileOpen function:

    Code:
    Public Function fileOpen(ByVal sFile As String) As Boolean
    Try
    
    System.IO.File.Open(sFile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
    FileClose(1)
    Return False
    
    Catch ex As Exception
    
    Return True
    End Try
    End Function


  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,192
    Reputation
    2368
    System.IO.File.Open(sFile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
    FileClose(1)
    Those methods are not related at all. File.Open, if successful, return a FileStream object, and that is what you must call Close on.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking