Close.xls file by a macro

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Close.xls file by a macro

Post by zyxw1234 »

I have a software & when I download a file from it 1.xls then it's open automatically (I am not opening the file manually by me, it opens up automatically when it get's downloaded)

So the problem is, I have tried many macros to close that file but that file is not closed by any macros, that I get from many coders

If I use that macros to close the file then the file should be opened by me manually then only all the macros work


So for that there will be a different macro
Or this problem is not solvable by vba?

If it's not solvable by vba then may I know the alternative option to close any excel file

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Close.xls file by a macro

Post by HansV »

Does this work for you?

Code: Select all

Sub CloseWorkbook()
    Dim wb As Object
    On Error Resume Next
    Set wb = GetObject("1.xls")
    If Not wb Is Nothing Then
        wb.Close SaveChanges:=False
    End If
End Sub
Best wishes,
Hans

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Close.xls file by a macro

Post by zyxw1234 »

Code: Select all

Sub CloseWorkbook()
    Dim wb As Object
    On Error Resume Next
    Set wb = GetObject("1.xls")
    If Not wb Is Nothing Then
        wb.Close SaveChanges:=False
    End If
End Sub

I use this & file is not close by the macro
& i use the below

Code: Select all

Sub CloseWorkbook()
    Dim wb As Object
    On Error Resume Next
    Set wb = GetObject("C:\Users\**I've been banned**\Desktop\1.xls")
    If Not wb Is Nothing Then
        wb.Close SaveChanges:=False
    End If
End Sub
& both the macros didn't close that file

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Close.xls file by a macro

Post by HansV »

That's a pity. If closing normally doesn't work and this macro doesn't either, I'm out of ideas.
Best wishes,
Hans

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Close.xls file by a macro

Post by zyxw1234 »

Ok HansV Sir

User avatar
Guessed
2StarLounger
Posts: 102
Joined: 04 Feb 2010, 22:44
Location: Melbourne Australia

Re: Close.xls file by a macro

Post by Guessed »

Is this code running in Excel or something else?

Can you iterate through the open workbooks to see if any have that name.

Code: Select all

Sub CloseWorkbook()
  Dim aWB As Workbook
  For Each aWB In Application.Workbooks
    If aWB.Name = "1.xlsx" Then
      aWB.Close SaveChanges:=False
      Exit For
    End If
  Next aWB
End Sub
Andrew Lockton
Melbourne Australia

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Close.xls file by a macro

Post by StuartR »

I wonder if the problematic workbook is open in a different instance of Excel to the workbook where the Macro is running
StuartR


zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Close.xls file by a macro

Post by zyxw1234 »

Code: Select all

Sub CloseWorkbook()
  Dim aWB As Workbook
  For Each aWB In Application.Workbooks
    If aWB.Name = "C:\Users\**I've been banned**\Desktop\1.xls" Then
      aWB.Close SaveChanges:=False
      Exit For
    End If
  Next aWB
End Sub

Code: Select all

Sub CloseWorkbook()
  Dim aWB As Workbook
  For Each aWB In Application.Workbooks
    If aWB.Name = "1.xls" Then
      aWB.Close SaveChanges:=False
      Exit For
    End If
  Next aWB
End Sub

I have used both the macros & it doesn' close that file Sir

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Close.xls file by a macro

Post by zyxw1234 »

I wonder if the problematic workbook is open in a different instance of Excel to the workbook where the Macro is running


Ok so what will be the macro u suggest StuartR Sir?

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Close.xls file by a macro

Post by StuartR »

zyxw1234 wrote:
28 Jul 2020, 08:10
Ok so what will be the macro u suggest StuartR Sir?
I'm not that clever!
How is the problem workbook being opened, is it in a Macro or what? You say it is downloaded, but that doesn't tell us who or what is opening the workbook.
StuartR


zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Close.xls file by a macro

Post by zyxw1234 »

Yes u can see
what is the problem
I am uploading the .xls file here just download that .xls file from this post & once u download the file, then it will open automatically in ur pc & try to close that file by a macro which u have given it to me (put the macro to macro.xlsm & run the macro ) & u will notice that file which is opened is not closing by the macro
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Close.xls file by a macro

Post by HansV »

It doesn't work that way - apparently the workbook is automatically opened when you download it from the original website. It doesn't open automatically when I download it from this thread to my hard disk. I have to open it myself (and then, any of the macros that you tried should close it)
Best wishes,
Hans

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Close.xls file by a macro

Post by zyxw1234 »

So HansV Sir u mean to say that u downloaded this file & its not opened automatically in ur pc ?
U have opened it manually ?

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Close.xls file by a macro

Post by HansV »

Yes, indeed. The workbook did not open automatically when I downloaded it.
Best wishes,
Hans

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Close.xls file by a macro

Post by zyxw1234 »

Ok That's Great, So we found the problem
In my pc this file is opened automatically
So plz suggest me the solution to stop the file opening automatically after downloading
Plz provide me which settings i have to change in excel
I am using Excel 2013

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Close.xls file by a macro

Post by HansV »

Which browser are you using when you download the workbook?
Best wishes,
Hans

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Close.xls file by a macro

Post by zyxw1234 »

Google chrome
When I download .xlsx file then it is downloaded but It never opens automatically
But only .XLS files opens up automatically

One more thing I would like to inform u, why we are playing with browser , Excel doesn't have setting to stop automatically opening of files?

(The reason why I am asking this question is bcoz If we can fix the Problem from Excel settings it will be very very beneficial for me & If we are fixing the issue by targetting browser setting then it will only work for browser

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Close.xls file by a macro

Post by HansV »

As far as I can tell, it must be a browser setting, not an Excel setting.

Download a .xls file from Chrome.
The filename should be displayed in the download bar at the bottom of the Chrome window.
Click the small arrow to the right of the filename.
You will probably see a check mark next to 'Always open files of this type'.
Click on this option to remove the check mark.
.xls files shouldn't be opened automatically anymore from now on.
Best wishes,
Hans

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Close.xls file by a macro

Post by zyxw1234 »

Ok No Problem
Thnx for the info

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Close.xls file by a macro

Post by zyxw1234 »

https://www.excelforum.com/excel-progra ... rompt.html

taskkill /F /IM EXCEL.EXE
this line i put in cmd &I run that, then it closes all the excel file including that file also sir(This is also a Great Option for me )


But i found one more line to put in cmd that will close the excel file (it wil not close all the excel file, it will only close a specified excel file)

TASKKILL /FI "WINDOWTITLE eq Microsoft Excel - Book3.xls"


I need clarity on this line
what is this "WINDOWTITLE eq Microsoft Excel - Book3.xls"

plz guide HansV Sir & plz make this line workable TASKKILL /FI "WINDOWTITLE eq Microsoft Excel - Book3.xls"