I am trying to open my .xlsm file with a Userform that is in a workbook Event OPEN and create an ENTER keypress simulation so the MACRO starts. I know that I can call the macro directly but we are using the Userform to show some information while the code is running.
I have built a Powershell script but as soon as I open my excel file my code stops there and does not execute the rest needed until I close the excel file. Is it a way to force my code to jump to the next execution or a different solution. thank you advance
$excel = New-Object -comobject Excel.Application
#open file
$FilePath = 'Q:\Detail\DT_HAL_TMPC\App\POFI_Planificateur_Taches_Exec.xlsm'
$workbook = $excel.Workbooks.Open($FilePath)
#make it visible (just to check what is happening)
$excel.Visible = $true
$workSheet = $workBook.Worksheets.Item(1)
$wshell = New-Object -ComObject wscript.shell;
sleep 10
$wshell.SendKeys('~')
as soon as I close my excel file the .sendKeys works but I want to do it while my excel file is open, not close
Related
Double Clicking on Pivot tables GrandTotal data creates a separate sheet with entire data source. Would like to get this done through powershell. Below is the code I tried with Powershell 5.1.
$excelFile = "C:\test\Testfile.xlsb"
$Excel = New-Object -ComObject Excel.Application
$wb = $Excel.Workbooks.Open($excelFile)
$s=$wb.worksheets(1).range("C7").select
$s.showdetail
$wb.saveas("C:\test\Testfile_modified.xlsb")
$wb.close()
$Excel.quit()
More direct - no need for select or intermediate variable $s:
$excelFile = "C:\tester\Testfile.xlsb"
$Excel = New-Object -ComObject Excel.Application
$wb = $Excel.Workbooks.Open($excelFile)
$wb.worksheets(1).range("C7").showdetail = $true
$wb.saveas("C:\tester\Testfile_modified.xlsb")
$wb.Close()
$Excel.Quit()
#make sure Excel process is ended (or it may persist)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
Remove-Variable Excel
in a script file called from cmd using:
powershell -noexit "& ""C:\Tester\psExcelTest.ps1"""
I have a VB.NET application that uses Microsoft.Office.Interop.Excel to simply open up a spreadsheet, pump over some data to it, refresh the data in the pivot tables and then present the user with the spreadsheet.
The code is pretty simple (error handling not shown):
' Start Excel Application
xlApp = New Excel.ApplicationClass
' Get Excel Workbooks
xlWorkBooks = xlApp.Workbooks
' Open workbook
xlWorkBook = xlWorkBooks.Open(Filename:=sExcelDocFullPath, IgnoreReadOnlyRecommended:=blnIgnoreReadOnly)
If Not xlWorkBook Is Nothing Then
' Pump some data over (code not shown)
' Make the first worksheet in the document active
CType(xlWorkBook.Worksheets(1), Excel.Worksheet).Activate()
'Return control of Excel to the user
xlApp.Visible = True
xlApp.UserControl = True
' Refresh workbooks (with alerts off, so as not to hassle user )
xlApp.DisplayAlerts = False
xlWorkBook.RefreshAll()
End If
What happens when this code is run, is that Excel is opened up behind the calling app and because we are refreshing the data connections, the small Excel "SQL Server Login" dialog is also displayed behind the calling app and thus the user needs to ALT+TAB to Excel or click on Excel in the Task Bar - neither is a great experience for the user. We'd like the Login dialog to appear infront of the calling app.
Can anyone suggest how this could be achieved ? I've tried moving the setting of the Visible & UserControl properties to after the RefreshAll, but that doesn't make any difference.
Cheers,
Chris.
Sorry for C#, I don't know VB, so I cannot translate myself, but the main idea is to use WinAPI:
[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(IntPtr hWnd);
You can get hwnd from xlApp:
BringWindowToTop((INtPtr)XlApp.Hwnd);
I have written a macro that calls three other macros inside of it in order to use a public variable in the 3 macros . The macro runs perfectly from excel but when I try and call the macro in power shell I receive an error message.
Here is the macro that works perfectly in excel:
Public Fnum As Integer
Sub master_macro()
combine_workbooks
automate_search
delete_duplicate
End Sub
here is the powershell code:
#PowerShellScript to Test automation of 80% test
# start Excel
$excel = New-Object -comobject Excel.Application
#open file
$FilePath = 'C:\File Build Project\!Notes for FileBuild Project\PF PM Data Collection Workbook-template Our Macro'
$workbook = $excel.Workbooks.Open($FilePath)
#no point to see excel since everything else is shutdown in vba
$excel.Visible = $False
$excel.DisplayAlerts = $False
#run macro save workbook close excel
$excel.Run("master_macro")
$workbook.Save()
$excel.quit()
finally here is the error message i receive when the powershell is run:
Exception calling "Run" with "31" argument(s): "Cannot run the macro 'delete_duplicates'. The macro may not be availabl
e in this workbook or all macros may be disabled."
At C:\File Build Project\Testing\PowerShellScript_Test_automation.ps1:17 char:11
+ $excel.Run <<<< ("delete_duplicates")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Any Help would be appreciated
I have a VS2010 Vb.net program that creates a Word 2007 file.
My Normal.dot file is customised to give me a new Tab with Buttons in that do specific things via VBA in the Normal.dot program when those Buttons are pressed.
This all works fine, however, I now want to add some functionality whereas as soon as the new Word document is created, it edits a Task in Outlook.
I have edited the 2 "This Document" Procedures and you can see my Normal.Dot file in the attached Screenshot.
When I run my VB.Net program to create a brand new Word 2007 document, the program does NOT stop on either of the message boxes, it just continues and opens the Word document as before, my code is below, what am I doing wrong ?!?
'Open or Create Word document for Editing
myNewsLetter = myFolder + myLeague + "News" + mySession + ".doc"
If File.Exists(myNewsLetter) Then
'do nothing
Else
myTemplate = myTempFolder + "NL Skeleton.doc"
File.Copy(myTemplate, myNewsLetter)
Create_Blank_Newsletter()
End If
'Open Word Newsletter, or switch to it if it's already open
Dim myFileOpen As Boolean
myFileOpen = IsFileOpen(myNewsLetter)
If myFileOpen = False Then
MSDoc = MSWord.Documents.Open(myNewsLetter)
End If
MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
MSWord.Visible = True
MSWord.ActiveDocument.Bookmarks("\StartOfDoc").Select()
OK, sorted this, the full discussion can be found here ... http://www.vbaexpress.com/forum/showthread.php?p=286771#post286771
Basically, I'm not creating a NEW document, I am creating a new document via a Copy and then opening that existing document !!!
I am using the following Powershell script to run some macro's on worksheets within a workbook. What I want to do is open a workbook, run a certain macro on specific worksheets, and save the workbook to a different location. For testing purposes, I am just running it on all the worksheets.
Here is what I have so far:
$excel = New-Object -ComObject excel.application
$xlfileformat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbook
$workbook = $excel.Workbooks.Open($(join-path -path R:\TestOutput\Nick -childpath tmp.xlsm))
$worksheets = $Workbook.Worksheets | ForEach-Object {$_.name}
ForEach ($Worksheet in $Worksheets) {
$WS = $Workbook.Worksheets.Item($Worksheet)
$excel.Run("RunMacro")
$workbook.save()
} #End ForEach
$workbook.saveas($(join-path -path "R:\TestOutput\Nick" -childpath "Test_Output-post"), $xlfileformat)
$excel.quit()
With this script I would expect the macro to run on all the worksheets within the workbook, however, only the last worksheet has the macro applied to it. At first I didn't have the $workbook.save() in there. I added it because I though it was reinitialize the workbook every time it loaded a new worksheet, therefor losing the changes from the last run. This didn't fix the issue though.
I know I could modify the VBA script itself, but I want to eventually turn this into a function with the macro and worksheet(s) as variable inputs.
As I was typing this out, I found the answer. The problem was, I never activated the worksheet I selected. The following code fixed the problem:
$excel = New-Object -ComObject excel.application
$excel.DisplayAlerts = $false
$xlfileformat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbook
$workbook = $excel.Workbooks.Open($(join-path -path R:\TestOutput\Nick -childpath tmp.xlsm))
$worksheets = $Workbook.Worksheets | ForEach-Object {$_.name}
ForEach ($Worksheet in $Worksheets) {
$WS = $Workbook.Worksheets.Item($Worksheet)
$WS.Activate()
$excel.Run("RunMacro")
} #End ForEach
$workbook.saveas($(join-path -path "R:\TestOutput\Nick" -childpath "Test_Output-post"), $xlfileformat)
$excel.quit()
The fix was adding the $WS.Activate() command in the loop.