Powershell как закрыть excel

I am initiating a macro in a workbook from powershell (to automate a process). The below in powershell opens the excel workbook and runs the macro without visualizing the process.

The issue is even though I do not see the macro running, the new instance of excel generated from the macro is still open.

# start Excel
$excel = New-Object -comobject Excel.Application

#open file
$FilePath = 'C:fileBook1.xlsm'
$workbook = $excel.Workbooks.Open($FilePath)


#access the Application object and run a macro
$app = $excel.Application
$app.Run("macro")


#close excel
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Start-Sleep 1
'Excel processes: {0}' -f @(Get-Process excel -ea 0).Count
Remove-Variable $excel

exit $LASTEXITCODE

The excel file still comes up as a process in task manager and is taking up memory space.

How do I have powershell completely close the instance of the excel application that opens through the macro?

enter image description here

Any help greatly appreciated!

KDE777,

А вы уверенны, что остаётся именно тот excel, который был запущен этим скриптом?

Полагаю, что все именно так.

Вероятно, в тех случаях когда, процесс не завершается при вызове метода quit() и идет попытка вызова ReleaseComObject, то он возвращает ненулевое значение, соответственно, объект не освобождается.
По какой причине мне сложно сказать, здесь нужны углубленные знания.

У меня есть подобная проблема, я бы тоже не отказался от помощи, но товарищ Убежденный с форума ушел
Если с Excel еще можно привязаться к процессом, костыльным методом, то в моем случае этого не сделать.

Если вкратце я использую 1С-COM соединение, которое коннектится к кластеру на сервере и выполняет определенный код.
Вся проблема в том, что после выполнения кода, сеанс не снять никаким образом…
Штатного метода типа CLOSE(),QUIT(), или DISPOSE() не реализовано.
Выглядит примерно так.

PowerShell
1
2
3
4
5
6
7
8
9
10
$object=new-object -comobject V82.ComConnector
try
{
    $connection=$object.connect("Srvr=srv;Ref=ut;Usr=user;Pwd=123")
}
catch
{
    sleep -Seconds 5
    $connection=$object.connect("Srvr=srv;Ref=ut;Usr=user;Pwd=123")
}

В данном случае создается основоной COM-объект, и объект $connection, который само собой тоже является COM-объектом.
Например, сразу после его создания вызвать

PowerShell
1
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($connection)

То после вызова мы получим 0 — успешное завершение метода ReleaseComObject и сеанс также удалится с кластера.
Однако мне не нужно сразу после создания коннекта сразу его завершать, а надо выполнить код.
Поэтому, далее создается еще цепочка COM-объектов

PowerShell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$object=new-object -comobject V82.ComConnector
try
{
    $connection=$object.connect("Srvr=srv;Ref=ut;Usr=user;Pwd=123")
}
catch
{
    sleep -Seconds 5
    $connection=$object.connect("Srvr=srv;Ref=ut;Usr=user;Pwd=123"
}
$zapros=[System.__comObject].InvokeMember("NewObject",[System.reflection.bindingFlags]::invokemethod,$null,$connection,"Запрос")                  
[system.__comObject].InvokeMember("Text",[System.reflection.bindingFlags]::setproperty,$null,$zapros,$zapros_1)
[system.__comObject].InvokeMember("УстановитьПараметр",[System.reflection.bindingFlags]::invokemethod,$null,$zapros,("Дата",$curDate))
[system.__comObject].InvokeMember("УстановитьПараметр",[System.reflection.bindingFlags]::invokemethod,$null,$zapros,("Дата2",$nextDate))
$res=[system.__comObject].invokeMember("Выполнить",[System.reflection.bindingFlags]::invokemethod,$null,$zapros,$null)
$vigruzka=[system.__comObject].invokeMember("Выгрузить",[System.reflection.bindingFlags]::invokemethod,$null,$res,$null)

В данном случае был выполнен запрос 1С и его результат сохранен в переменную $vigruzka, который тоже является COM-объектом.
Собственно, если после всего этого попытаться освободить все ком-объекты, через ReleaseComObject

PowerShell
1
2
3
4
5
6
7
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($vigruzka)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($EVOTORvigruzka)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($zapros) 
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($res)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($EVOTORzapros)   
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($EVOTORres)              
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($connection)

То все прекрасно отработает, и сеанс удалится, НО
Посколько переменная $vigruzka — хранит все табличные данные обернутые COM-объектом, то ее надо обработать.
Вот тут и начинаются сложности.
Если попытаться подсунуть ее в цикл Foreach, причем в пустой, или даже хотя бы просто вывести ее содежимое

PowerShell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PS C:Usersbafomet> $vigruzka
System.__ComObject
System.__ComObject
System.__ComObject
System.__ComObject
System.__ComObject
System.__ComObject
System.__ComObject
System.__ComObject
System.__ComObject
System.__ComObject
System.__ComObject
System.__ComObject
System.__ComObject
System.__ComObject
System.__ComObject
System.__ComObject
PowerShell
1
2
3
4
foreach($element in $vigruzka)
{
 
}

То последующий вызов ReleaseComObject всегда возвращает — 1, ну и соответственно сеанс не удаляется

Почему так, я не понимаю.

Добавлено через 4 часа 21 минуту
Все, в своей проблеме я разобрался.
Все из-за кривых рук разработчиков 1С.

По поводу Excel, советую понаблюдать за возращаемым значением ReleaseComObject, если он отличен от нуля, то освобождение объекта не происходит.
Можете попробовать использовать FINALReleaseComObject



0



Powershell работа с Excel на примере служб windows

Для работы с Excel в среде powershell нужно использовать COM объект.


#Создаем новый объект
$excel = New-Object -ComObject Excel.Application 

При выполнении данного командлета произойдет запуск приложения Excel в скрытом состоянии, т.е. у нас будет висеть
процесс Excel, но при этом самого приложения как такового мы не увидем. Для того чтобы увидеть работу приложения
нужно обратиться к его свойству Visible и установить его в TRUE т.к. по умолчанию стоит FALSE


#Режим записи видимый
$excel.Visible = $true 

Далее после того как приложение открылось нужно создать книгу воспользовавшись свойством Workbooks и методом Add().


#Создаем книгу Excel
$EWB = $excel.Workbooks.Add() 

После создания книги нужно выбрать лист с которым будем работать в данной книге. Для этого воспользуемся
свойством Worksheet и методом Item()передав ему номер листа с которым будем работать.


#Выбираем первый лист книги
$EWS = $EWB.Worksheets.Item(1) 

Зададим имя нашему листу для этого используем свойство Name и присвоим ему значение нового имени листа.


#Название листа книги
$EWS.Name = 'Service' 

Так как Excel это таблица и мы работаем с ее ячейками то используем свойство Cells и метод Item() для
указания с какими ячейками данного листа мы будем работать. Отсчет начинается с 1.


#Первая строка ячеек
$EWS.Cells.Item(1,1) = 'Status'
$EWS.Cells.Item(1,2) = 'Name'
$EWS.Cells.Item(1,3) = 'DisplayName' 

В данном примере мы устанавливаем названия наших колонок которые в дальнейшем будем заполнять. Первый аргумент
это номер строки с которой работаем, второй аргумент это ячейка в таблице. Перед тем как перебирать массив с
данными и записывать все в таблицу нам нужна вспомогательная переменная которая будет ссылаться на начальную
строку в таблице с дальнейшим увеличением в цикле для перехода на новую строку.


#Перебераем массив и вставляем в нужные ячейки листа
$j = 2
foreach($arr in $srv)
{
   #Если процесс запущен то зеленый иначе красный
   if($arr.Status -eq 4){
    $EWS.Cells.Item($j,1) = 'Запущен'
    $color = 10
   }else{
    $EWS.Cells.Item($j,1) = 'Остановлен'
    $color = 3
   }
   
   
   $EWS.Cells.Item($j,2) = $arr.Name
   $EWS.Cells.Item($j,3) = $arr.DisplayName
   
   $EWS.Cells.Item($j,1).Font.Bold = $true
   $EWS.Cells.Item($j,1).Font.ColorIndex = $color

   $j++
} 

После того как книга заполнена нужными данными ее необходимо сохранить. Для этого используем метод Saveas() и
передаем ему путь и имя файла для сохранения.


#Сохраняем книгу
$EWB.Saveas('C:Excelservices.xlsx') 

После этого закрываем саму книгу воспользовавшить методом Close().


#Закрываем книгу
$EWB.Close() 

После того как закрыли книгу можно завершать работу приложения Excel использую метод Quit(). Тем самым освободив память в системе.


#Закрываем приложение Excel
$excel.Quit() 

У данного способа есть существенный недостаток это очень медленная работа экспорта данных в таблицу Excel, но за то можно создавать красивые отформатированные таблицы для конечного пользователя. Здесь представлен полный скрипт, который получает список всех служб на вашем компьютере и записывает их в ячейки таблицы Excel.



#Получаем список всех процессов
$srv = Get-Service

#Создаем новый объект
$excel = New-Object -ComObject Excel.Application

#Режим записи видимый
$excel.Visible = $true

#Создаем книгу Excel
$EWB = $excel.Workbooks.Add()

#Выбираем первый лист книги
$EWS = $EWB.Worksheets.Item(1)

#Название листа книги
$EWS.Name = 'Service'

#Первая строка ячеек
$EWS.Cells.Item(1,1) = 'Status'
$EWS.Cells.Item(1,2) = 'Name'
$EWS.Cells.Item(1,3) = 'DisplayName'

#Перебераем массив и вставляем в нужные ячейки листа
$j = 2
foreach($arr in $srv)
{
   #Если процесс запущен то зеленый иначе красный
   if($arr.Status -eq 4){
    $EWS.Cells.Item($j,1) = 'Запущен'
    $color = 10
   }else{
    $EWS.Cells.Item($j,1) = 'Остановлен'
    $color = 3
   }
   
   
   $EWS.Cells.Item($j,2) = $arr.Name
   $EWS.Cells.Item($j,3) = $arr.DisplayName
   
   $EWS.Cells.Item($j,1).Font.Bold = $true
   $EWS.Cells.Item($j,1).Font.ColorIndex = $color

   $j++
}

#Сохраняем книгу
$EWB.Saveas('C:Excelservices.xlsx')

#Закрываем книгу
$EWB.Close()

#Закрываем приложение Excel
$excel.Quit()



Содержание

  1. Powershell работа с Excel на примере служб windows
  2. Excel, save and close after run
  3. 6 Answers 6
  4. Closing open instances of excel
  5. Fix it Fast: 6 ways LogicMonitor helps you reduce MTTR
  6. 8 Replies
  7. Read these next.
  8. Uh-Oh! An end user (and his laptop) fell off an oil rig
  9. How do you manage budget for laptop replacement batteries?
  10. Snap! — Counterfeit Drives, Disposable Satellite, AI Hands Fixed, Automated CEO
  11. Nerd Journey # 209 — Meetings with the Chief with Anudeep Parhar (2/2)
  12. Powershell как закрыть excel
  13. Вопрос
  14. Ответы
  15. Все ответы
  16. Закрыть определенный файл Excel с помощью Powershell

Powershell работа с Excel на примере служб windows

Для работы с Excel в среде powershell нужно использовать COM объект.

При выполнении данного командлета произойдет запуск приложения Excel в скрытом состоянии, т.е. у нас будет висеть процесс Excel, но при этом самого приложения как такового мы не увидем. Для того чтобы увидеть работу приложения нужно обратиться к его свойству Visible и установить его в TRUE т.к. по умолчанию стоит FALSE

Далее после того как приложение открылось нужно создать книгу воспользовавшись свойством Workbooks и методом Add().

После создания книги нужно выбрать лист с которым будем работать в данной книге. Для этого воспользуемся свойством Worksheet и методом Item()передав ему номер листа с которым будем работать.

Зададим имя нашему листу для этого используем свойство Name и присвоим ему значение нового имени листа.

Так как Excel это таблица и мы работаем с ее ячейками то используем свойство Cells и метод Item() для указания с какими ячейками данного листа мы будем работать. Отсчет начинается с 1.

В данном примере мы устанавливаем названия наших колонок которые в дальнейшем будем заполнять. Первый аргумент это номер строки с которой работаем, второй аргумент это ячейка в таблице. Перед тем как перебирать массив с данными и записывать все в таблицу нам нужна вспомогательная переменная которая будет ссылаться на начальную строку в таблице с дальнейшим увеличением в цикле для перехода на новую строку.

После того как книга заполнена нужными данными ее необходимо сохранить. Для этого используем метод Saveas() и передаем ему путь и имя файла для сохранения.

После этого закрываем саму книгу воспользовавшить методом Close().

После того как закрыли книгу можно завершать работу приложения Excel использую метод Quit(). Тем самым освободив память в системе.

У данного способа есть существенный недостаток это очень медленная работа экспорта данных в таблицу Excel, но за то можно создавать красивые отформатированные таблицы для конечного пользователя. Здесь представлен полный скрипт, который получает список всех служб на вашем компьютере и записывает их в ячейки таблицы Excel.

Источник

Excel, save and close after run

how can I save the below script after it has run?

6 Answers 6

This worked for me :

To properly and completely close Excel, you also need to release COM references. In my own testing have found removing the variable for Excel also ensures no remaining references exist which will keep Excel.exe open (like if you are debugging in the ISE).

Without performing the above, if you look in Task Manager, you may see Excel still running. in some cases, many copies.

This has to do with how the COM object is wrapped in a “runtime callable wrapper».

Here is the skeleton code that should be used:

Got it working! — Special thanks to @Matt

Complete script that is working:

As mentioned in MSDN documentation here, the ReleaseComObject call only decrements the reference counter of that COM object by 1. If your scripts has multiple references of the same COM object, It will not release the object.

The documentation recommends using FinalReleaseComObject method to completely release the COM object and close the Excel process once in for all.

Just be sure to call this method only when you are done with the COM reference, as not doing so may lead to bugs which are hard to debug.

Источник

Closing open instances of excel

Is there a line of code I can add to a script to close all open instances of Excel?

The open instances of Excel is preventing my folder cleanup scripts to execute fully.

Fix it Fast: 6 ways LogicMonitor helps you reduce MTTR

This should take you in the right direction. =]

CloseExcel
SUB CloseExcel()
on error resume next
set xl =getobject(,»Excel.Application»)
xl.activeworkbook.saved = true
xl.Quit
set xl = nothing
call CloseExcel
End Sub

This closes all open Excel Workbooks. Hope it helps!

  • check 1337 Best Answers
  • thumb_up 3072 Helpful Votes

or just like so

Can you please add comments to the code you provided so I can better understand how it works. I’d appreciate it highly

  • check 1337 Best Answers
  • thumb_up 3072 Helpful Votes

Can you please add comments to the code you provided so I can better understand how it works. I’d appreciate it highly

Nice! Thx Neally!!

The problem with jspr’s link and Neally’s script is that they’re just going to kill any instance of Excel. If there are any open, unsaved workbooks, you’re going to lose all the work since the last save for each workbook. (I believe the default autosave period for Excel is ten minutes, so it may not be that much of a problem, but you’re going to have to deal with recovered files when you reopen Excel.)

I haven’t tried TheEggOnTop’s script, but it appears to be the correct way to go. The only issue I see with it is it’s not PowerShell, it’s either VBScript or VBA. Most VB scripts can be translated into PowerShell though (emphasis on «most»).

  • check 1337 Best Answers
  • thumb_up 3072 Helpful Votes

The problem with jspr’s link and Neally’s script is that they’re just going to kill any instance of Excel. If there are any open, unsaved workbooks, you’re going to lose all the work since the last save for each workbook. (I believe the default autosave period for Excel is ten minutes, so it may not be that much of a problem, but you’re going to have to deal with recovered files when you reopen Excel.)

I haven’t tried TheEggOnTop’s script, but it appears to be the correct way to go. The only issue I see with it is it’s not PowerShell, it’s either VBScript or VBA. Most VB scripts can be translated into PowerShell though (emphasis on «most»).

I’m sure you can mess around with com objects or try the ‘importExcel’ module.

OP didn’t give too much info or follow up though. so ..

This topic has been locked by an administrator and is no longer open for commenting.

To continue this discussion, please ask a new question.

Read these next.

Uh-Oh! An end user (and his laptop) fell off an oil rig

Hi, y’all — Chad here. With all the hustle and bustle of life on Spiceworks, you may not know anything about me. That’s hard for me to imagine, because in my head, I’ve built up quite the fantasy world where I’m good-looking, popular, and well-liked. I me.

How do you manage budget for laptop replacement batteries?

Hi, I have three questions:1. An HP article said laptop battery lifespan is about 2-4 years. I wasn’t able to see other data sources. Does it match what you observe in practice? Our PC replacement cycle is about 4-5 years, so I am planning all batteries w.

Snap! — Counterfeit Drives, Disposable Satellite, AI Hands Fixed, Automated CEO

Your daily dose of tech news, in brief. Welcome to the Snap! Flashback: March 20, 1909: Edmund Berkeley, founder of the Association of Computing Machinery, is born (Read more HERE.) Bonus Flashback: March 20, 1959: Task force formed to pl.

Nerd Journey # 209 — Meetings with the Chief with Anudeep Parhar (2/2)

Last week we shared the first part of Anudep Parhar’s progression from developer to technology executive (link to the episode is here if you missed it).In part 2 of the story, we hear about a moment of ignition Opens a new window that gave Anudeep the mot.

Источник

Powershell как закрыть excel

Вопрос

the correct way to avoid Excel zombies is to release all ComObjects in the powershell environment / script by invoking:

on each posh variable holding a reference to a COM object, and to do so in «reverse» order (suitably defined).

«Correct» means specifically «leaves no invisible lurking Excel apps behind».

The recipe works for me when I invoke my scripts as (say):

But when I invoke the script as:

and then list posh variables:

posh throws an error when it encounters the first variable that (previously) held a ComObject reference:

I tried rv’ing the posh variables, but when I do so prior to the ReleaseComObject, I no longer have a handle to the Com Object; alternatively when I rv the posh variable after ReleaseComObject, posh throws the same error as above.

How to release both the Com Object and the posh variable in the base posh environment (ie . ./script) while avoiding Excel zombies?

Code excerpts below:

TIA for the help.

FWIW, I’m running Win7 and Office 2010 in a Workstation 8 VM.

Ответы

Keep variables in array wwhen you build them,

$xl=New-Object -Com Excel.Application
$vars_to_remove+=’xl’
. use var
. release objects
# now remove vars
$vars_to_remove | | %

Now no more unused variables.

Все ответы

Jim — ThaZt is some writeup but can you just ask one initial question. WHat is it that is not happening.

To release the Excel object just use teh release code after a ‘Quit.’

You must release all objects you have assigned the same way. TO make life easier do tno create interim variables. I usually create only one — the sheet object. I then release it explicitly when done.

Do not create a WB object and you will becloser to a simple usage.

To get the sheet:

To work with ranges work with them in place. You can also add all objects created to an array and then release all in teh array. If you want to reuse the Excel object don’t add it to teh array.

There are two methods given in the thread you caame from. When enumerates teh releaseobject until teh count is zero and teh other does a release all. Us3e either one. Do not use either if you are going to touch the objects after you have released. Removing variables is optional.

If you haev created a visible copy of Excel from PowerSHell and are using it you cannot release it in Excel or shutdown excel. If you want the same copy start excel as an external program and attach to the session from PowerShell. PowerShell will not be able to shut excel down but is can quit and rease any objects you have created and Excell will not shutdown. PowerShell can close and open documents in the users session.

PowerShell is not a good tool for automating interactive Excel. For that use VBA as it has access to parts of Office that PowerSHell has issues with accessing.

Источник

Закрыть определенный файл Excel с помощью Powershell

Какой командлет PowerShell подходит для уничтожения определенного файла Excel. Команда

Закрывает все открытые приложения Excel.

Вот официальное руководство от MS:

Этот способ работает. Но есть ли у вас способ не запускать восстановление документа при повторном открытии файла excel?

На самом деле Excel открывает .xlsx файлы в одном экземпляре excel.exe

Kill excel.exe уничтожит все открытые книги. MainWindowTitle показывает только активную книгу, поэтому вы не сможете сопоставить пассивные книги по свойству.

Лучший способ закрыть конкретную книгу — получить экземпляр excel с помощью объекта com.

Закройте книгу, соответствующую Name (имя файла) или FullName (путь к файлу)

Должно быть, я делаю что-то не так. Кажется, не работает, когда я пытался использовать полное имя.

$ excel = [System.Runtime.InteropServices.Marshal]::GetActiveObject(«E‌​xcel.Application») $excel.DisplayAlerts = $false $excel.Workbooks | %>

Оператор @drdrdr -imatch — это регулярное выражение, если вы не знакомы с ним, вы можете легко заменить -imatch на -like. например $_.FullName -like «M:Items.xlsx» или даже -eq «M:Items.xlsx»

Проблема в том, что окно остается открытым, пока документ закрыт.

Надеюсь, это все еще помогает. БР

Почему это всегда портит мое форматирование xD и удаляет первую строку, которую я пишу.

Источник

  • Remove From My Forums
  • Question

  • Hi, I am trying to write a script which opens each of 31 excel workbooks, retrieves a value from a cell, and closes each workbook after retrieving.

    My code so far:

    $day=1
    $excel = New-Object -ComObject Excel.Application
    $excel.Visible=$True
    $workbook= $excel.Workbooks.Open("C:Path|toFile1_"+$day+"_14.xlsx")
    $workbook.sheets.item(1).activate()
    $WorkbookTotal=$workbook.Worksheets.item(1)
    $value = $WorkbookTotal.Cells.Item(7,13)
    $value.Text #this should give you back the Value in that Cell
    
    $excel.close()

    My problem is that when the code hits the $excel.close() method, the file stays open, and prompts for saving changes.  I don’t know why it thinks there are changes, but what I want is to close the workbook without saving changes.

    I’ve just closed 44 open browser tabs, and have tried many things over the last hour, but can’t seem to get this one.

    If anyone has any advice on how to do this, it would be greatly appreciated!

    Thanks,

    Kevin

Answers

  • Hi,

    Please replace below code:

    $excel.close()

    with

    $workbook.close($false)

    $excel.quit()

    Hope this helps.

    Regards,

    Yan Li


    Regards, Yan Li

    • Edited by

      Friday, March 7, 2014 6:28 AM
      edit

    • Marked as answer by
      ktgeil
      Friday, March 7, 2014 3:33 PM

Понравилась статья? Поделить с друзьями:
  • Power quick excel 2016
  • Powershell выгрузка в excel
  • Powerpoint не открывает excel
  • Power query это надстройка excel
  • Powershell вывод в таблицу excel