?
Solved

Is this File open?

Posted on 2014-10-28
9
Medium Priority
?
81 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

800 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