简体   繁体   English

Powershell发送密钥Excel

[英]Powershell Send Key Excel

I would like to write a script in Powershell which does the following: 我想在Powershell中编写一个执行以下操作的脚本:

  1. Opens Excel Workbook. 打开Excel工作簿。
  2. Click two times enter on the 2 appearing DialogBoxes. 单击两次,在出现的2个对话框中输入。
  3. Runs a macro, saves workbook and quits. 运行宏,保存工作簿并退出。

I have a problem with the 2nd step. 我对第二步有疑问。 In my script enter is sent to Powershell window and I see two lines added to the code. 在我的脚本中,将回车发送到Powershell窗口,我看到在代码中添加了两行。

Could anyone help me? 有人可以帮我吗? Here is my code: 这是我的代码:

$p1 = "C:\"
$eo = new-object -comobject excel.application; 
$eoc = dir $p1 -Include *.xlsm -Recurse              
Foreach ($f1 in $eoc)
{
   $wb = $eo.workbooks.open($f1.fullname)
   while (!$eo.Ready){sleep 0.1}
   $eo.Application.SendKeys('~')
   while (!$eo.Ready){sleep 0.1}
   $eo.Application.SendKeys('~');
   $eo.Run("macro_name"); 
   $eo.Application.CalculateFull; 
   while (!$eo.Ready){sleep 0.1};
   $wb.save(); $wb.close()
}
$eo.quit()

You need something like this. 您需要这样的东西。

$wshell = New-Object -ComObject wscript.shell;

$wshell.AppActivate('window name of your excel file') | Out-Null

This will focus your excel workbook and execute your send key commands. 这将集中您的excel工作簿并执行您的发送键盘命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM