Solved

Is this File open?

Posted on 2014-10-28
9
64 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)
ID: 40409505
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
ID: 40409796
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)
ID: 40409802
Can you post the remaining code of the routine?
0
 

Author Comment

by:RWayneH
ID: 40409807
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:RWayneH
ID: 40409810
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
ID: 40409831
Where does "GoTo 100" go?

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

Author Comment

by:RWayneH
ID: 40409868
Setting bIsOpened to False after closing the workbook worked.  Thanks.
0
 

Author Comment

by:RWayneH
ID: 40409884
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
ID: 40409885
I think I closed this question wrg.  Thanks for the help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

863 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

25 Experts available now in Live!

Get 1:1 Help Now