Solved

Is this File open?

Posted on 2014-10-28
9
61 Views
Last Modified: 2014-10-28
Why does the following code think that the file is always open?  I am having issues getting the Else part of the If to fire.  Also how do I get the file closed and not save changes?  I just want the file closed so I can use it.  It is a   .XML file.

  Dim i As Integer
  Dim bIsOpened
  For i = 1 To Application.Workbooks.Count
   bIsOpened = bIsOpened Or (Application.Workbooks(i).Name = "SALESORDERDOWNLOAD.XML" _
   And Application.Workbooks(i).Path = "C:\_SAP\Extracts\SalesOrd")
  Next i

  If bIsOpened Then
   MsgBox "Target save file is open, Closing file so process can use it.", vbOKOnly + vbInformation, "Cannot have: C:\_SAP\EXTRACTS\SALESORD\SALESORDERDOWNLOAD.xml open and run this process.  Close it and try again."
   Application.DisplayAlerts = False
   Windows("SALESORDERDOWNLOAD.XML").Activate
   ActiveWorkbook.Close
   
   Application.DisplayAlerts = True

Open in new window

0
Comment
Question by:RWayneH
  • 6
  • 3
9 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
Try changing your bIsOpened checking loop to this...

    For i = 1 To Application.Workbooks.Count
        If (Application.Workbooks(i).Name = "SALESORDERDOWNLOAD.XML" _
            And Application.Workbooks(i).Path = "C:\_SAP\Extracts\SalesOrd") Then
            bIsOpened = True
            Exit For
        End If
    Next i

Open in new window

0
 

Author Comment

by:RWayneH
Comment Utility
This is not working either, bIsOpened is still = True when I run this over, (after closing the file) it is still True.  How do I reset the value of bIsOpened after it checks?  I started with it opened.  The code closed the workbook.  Why does it still think that it open?
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
Can you post the remaining code of the routine?
0
 

Author Comment

by:RWayneH
Comment Utility
Dim i As Integer
 Dim bIsOpened
    For i = 1 To Application.Workbooks.Count
        If (Application.Workbooks(i).Name = "SALESORDERDOWNLOAD.XML" _
            And Application.Workbooks(i).Path = "C:\_SAP\Extracts\SalesOrd") Then
            bIsOpened = True
            Exit For
        End If
    Next i

  If bIsOpened Then
    MsgBox "Target save file is open, Closing file so process can use it.", vbOKOnly + vbInformation, "Cannot have: C:\_SAP\EXTRACTS\SALESORD\SALESORDERDOWNLOAD.xml open and run this process.  Close it and try again."
    Application.DisplayAlerts = False
    Windows("SALESORDERDOWNLOAD.XML").Activate
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
    GoTo 100  'Removed Exit Sub
  Else
   GoTo 100
  End If







 
'  For i = 1 To Application.Workbooks.Count
'   bIsOpened = bIsOpened Or (Application.Workbooks(i).Name = "SALESORDERDOWNLOAD.XML" _
'   And Application.Workbooks(i).Path = "C:\_SAP\Extracts\SalesOrd")
'  Next i

  If bIsOpened Then
    MsgBox "Target save file is open, Closing file so process can use it.", vbOKOnly + vbInformation, "Cannot have: C:\_SAP\EXTRACTS\SALESORD\SALESORDERDOWNLOAD.xml open and run this process.  Close it and try again."
    Application.DisplayAlerts = False
    Windows("SALESORDERDOWNLOAD.XML").Activate
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
    GoTo 100  'Removed Exit Sub
  Else
   GoTo 100
  End If

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:RWayneH
Comment Utility
Lets try that again.

Dim i As Integer
 Dim bIsOpened
    For i = 1 To Application.Workbooks.Count
        If (Application.Workbooks(i).Name = "SALESORDERDOWNLOAD.XML" _
            And Application.Workbooks(i).Path = "C:\_SAP\Extracts\SalesOrd") Then
            bIsOpened = True
            Exit For
        End If
    Next i

 
'  For i = 1 To Application.Workbooks.Count
'   bIsOpened = bIsOpened Or (Application.Workbooks(i).Name = "SALESORDERDOWNLOAD.XML" _
'   And Application.Workbooks(i).Path = "C:\_SAP\Extracts\SalesOrd")
'  Next i

  If bIsOpened Then
    MsgBox "Target save file is open, Closing file so process can use it.", vbOKOnly + vbInformation, "Cannot have: C:\_SAP\EXTRACTS\SALESORD\SALESORDERDOWNLOAD.xml open and run this process.  Close it and try again."
    Application.DisplayAlerts = False
    Windows("SALESORDERDOWNLOAD.XML").Activate
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
    GoTo 100  'Removed Exit Sub
  Else
   GoTo 100
  End If

Open in new window

0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
Comment Utility
Where does "GoTo 100" go?

Try setting bIsOpened to False immediately after closing the workbook.
0
 

Author Comment

by:RWayneH
Comment Utility
Setting bIsOpened to False after closing the workbook worked.  Thanks.
0
 

Author Comment

by:RWayneH
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for RWayneH's comment #a40409868

for the following reason:

Thanks for the help.
0
 

Author Closing Comment

by:RWayneH
Comment Utility
I think I closed this question wrg.  Thanks for the help
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

762 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

6 Experts available now in Live!

Get 1:1 Help Now