Running Excel VBA/Macro in PowerShell ISE
-
- 3StarLounger
- Posts: 203
- Joined: 20 Oct 2015, 09:06
Running Excel VBA/Macro in PowerShell ISE
Hi. Can an excel vba/macro be executed using powershell?
Thanks & Regards,
Alice
Alice
-
- cheese lizard
- Posts: 6241
- Joined: 16 Jan 2010, 00:14
- Location: Sydney Australia
-
- Administrator
- Posts: 78556
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Running Excel VBA/Macro in PowerShell ISE
When I follow Claude's link (thanks), the first hit is Hey, Scripting Guy! How Do I Run an Office Excel Macro on Multiple Workbooks?. This tells you everything you need to know, with examples.
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 203
- Joined: 20 Oct 2015, 09:06
Re: Running Excel VBA/Macro in PowerShell ISE
Hi. Thanks for all the reference links! I've used one of the scripts that I found from the links from the website. It worked but an error occurred to the part where the 3rd module from my excel macro needs to be executed.
The error occurred here:
And the error shows:
What could the problem be?
PS: When I run the whole macro via Excel it works just how it supposed to.
The error occurred here:
Code: Select all
Set olFld = olApp.Session.GetDefaultFolder(16) ' olFolderDrafts ---- ERROR STARTS HERE
On Error Resume Next
strMonth = Format(Date - Day(Date), "mmmm yyyy")
Set olFl2 = olFld.Parent.Folders("[Artists] " & strMonth)
On Error GoTo 0
If olFl2 Is Nothing Then
Set olFl2 = olFld.Parent.Folders.Add("[Artists] " & strMonth)
End If
PS: When I run the whole macro via Excel it works just how it supposed to.
Thanks & Regards,
Alice
Alice
-
- gamma jay
- Posts: 25455
- Joined: 17 Mar 2010, 17:33
- Location: Cape Town
Re: Running Excel VBA/Macro in PowerShell ISE
I cannot see any errors in that block of ode you posted.
VBA does not always highlight the exact line where the error occurs, so the issue might be a few lines above or below the highlighted line.
-- Have you double checked that you have ended a code block with End If, or End Select, or End With?
-- Have you ensure that all your object variables have been 'Set'?
You could post a larger portion of the code so we can attempt to assist if you cannot find anything yourself.
VBA does not always highlight the exact line where the error occurs, so the issue might be a few lines above or below the highlighted line.
-- Have you double checked that you have ended a code block with End If, or End Select, or End With?
-- Have you ensure that all your object variables have been 'Set'?
You could post a larger portion of the code so we can attempt to assist if you cannot find anything yourself.
Regards,
Rudi
If your absence does not affect them, your presence didn't matter.
Rudi
If your absence does not affect them, your presence didn't matter.
-
- 3StarLounger
- Posts: 203
- Joined: 20 Oct 2015, 09:06
Re: Running Excel VBA/Macro in PowerShell ISE
The problem is the code works without executing it on powershell.. I did a breakpoint to this macro but it only stops at
What else could I've been missing?Set olFld = olApp.Session.GetDefaultFolder(16)
Thanks & Regards,
Alice
Alice
-
- 3StarLounger
- Posts: 203
- Joined: 20 Oct 2015, 09:06
Re: Running Excel VBA/Macro in PowerShell ISE
uhm I think the problem might be the microsoft exchange server is not connected yet..
Thanks & Regards,
Alice
Alice
-
- Administrator
- Posts: 78556
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Running Excel VBA/Macro in PowerShell ISE
You could insert a line
above the line that causes the problem, to see if that solves it.
Code: Select all
olApp.Session.Logon
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 203
- Joined: 20 Oct 2015, 09:06
Re: Running Excel VBA/Macro in PowerShell ISE
Now the problem is within that line of code. I will check with my partner first if the problem is coming from the administrator account that I'm using. I will give y'all an update.
Thanks & Regards,
Alice
Alice
-
- Administrator
- Posts: 78556
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Running Excel VBA/Macro in PowerShell ISE
You could test whether it makes a difference if Outlook is already running before you execute the PowerShell script.
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 203
- Joined: 20 Oct 2015, 09:06
Re: Running Excel VBA/Macro in PowerShell ISE
The code stopped at the newly added line of code.
Thanks & Regards,
Alice
Alice
-
- Administrator
- Posts: 78556
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Running Excel VBA/Macro in PowerShell ISE
I'm starting to suspect that this two-stage automation - using PowerShell to control Excel, and using Excel to control Outlook - won't work. Perhaps the code should be rewritten entirely in PowerShell, so that both Excel and Outlook are controlled from PowerShell. However, I won't be able to help you with that, I'm not familiar with PowerShell.
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 203
- Joined: 20 Oct 2015, 09:06
-
- Administrator
- Posts: 78556
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Running Excel VBA/Macro in PowerShell ISE
If I were you, I would consult someone who is familiar with both PowerShell and VBA. Such a person would be in a much better position to tell you what is possible and what isn't.
Since I don't know PowerShell, I'm not in a position to make authoritative statements about it.
Since I don't know PowerShell, I'm not in a position to make authoritative statements about it.
Best wishes,
Hans
Hans
-
- 3StarLounger
- Posts: 203
- Joined: 20 Oct 2015, 09:06