Solved

Is this File open?

Posted on 2014-10-28
9
67 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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
 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

777 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