+ Reply to Thread
Results 1 to 8 of 8

How to pass a parameter from PowerShell to Excel

  1. #1
    Registered User
    Join Date
    08-10-2012
    Location
    Washington
    MS-Off Ver
    Excel 2003
    Posts
    28

    How to pass a parameter from PowerShell to Excel

    I have an excel macro-enabled worksheet that I am using. Excel is primarily being used to store all the macros and VBA scripts, to process another file (CSV). That CSV fileName is currently hardcoded into an Excel variable, for test. The fileName will not be known at runtime. It's being generated by a PowerSheet script, which will then open the Excel file.

    I need to open that worksheet from a PowerShell script and pass the (CSV) fileName parameter to the Excel worksheet. I can open the Excel worksheet just fine, and because the test fileName is hardcoded, for test, that works too. But I need to have the PowerShell script pass a fileName parameter to Excel, so the file doesn't have to be hardcoded.

    What is the easiest way to do that?
    Last edited by MikeSD; 04-30-2021 at 09:50 PM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: How to pass a parameter from PowerShell to Excel

    You could use your script to create a small txt file with the file name written in it, in the same directory as the excel file, and use the fileopen event of the Excel file to read the txt file to get the filename.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    08-10-2012
    Location
    Washington
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: How to pass a parameter from PowerShell to Excel

    Quote Originally Posted by Bernie Deitrick View Post
    You could use your script to create a small txt file with the file name written in it, in the same directory as the excel file, and use the fileopen event of the Excel file to read the txt file to get the filename.
    Clever but not direct. Does that mean there is no way directly to pass a variable?

    I tried the file trick and that worked, but if there is a better way, I'd like to know.
    Last edited by MikeSD; 04-30-2021 at 01:49 PM.

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: How to pass a parameter from PowerShell to Excel

    Of course you can - if you can't think of a dozen ways to use Excel to do the same thing, you're not really trying

    You need a function or macro inside your Excel workbook that can be passed a variable as an input - and that you can run by using the run method - lots of example code out there. Or you could embed the same logic into your macro to figure out the correct parameter... or use an inputbox if a user needs to be involved... or....

  5. #5
    Registered User
    Join Date
    08-10-2012
    Location
    Washington
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: How to pass a parameter from PowerShell to Excel

    Quote Originally Posted by Bernie Deitrick View Post
    Of course you can - if you can't think of a dozen ways to use Excel to do the same thing, you're not really trying

    You need a function or macro inside your Excel workbook that can be passed a variable as an input - and that you can run by using the run method - lots of example code out there. Or you could embed the same logic into your macro to figure out the correct parameter... or use an inputbox if a user needs to be involved... or....
    That's a lot of words to say "figure it out yourself", not that that's not the best way.

    None of what you have said seems obvious to a non-expert like myself. But maybe there are enough words to give me something to search for.

    Currently my parameter is hard coded. I have all the functions to make use of it, when I figure out how to pass the parameter. I've tried accessing the parameter from Excel but no-go. I will figure it out eventually, just trying to save some time. I've searched for this and all the examples I come up with are for ways of passing a parameter to PowerShell from VBA. The other way.

    I did try the file trick above and that does work. But that seems more like a crutch than a solution. But a crutch is better than no solution.

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: How to pass a parameter from PowerShell to Excel

    Sorry - my assumption is that anyone automating Excel using anything other than Excel Macros has a higher level understanding of coding.

    The first result for "passing variable to excel from powershell"

    Example:

    Please Login or Register  to view this content.

    Since you want to pass a string

    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 04-30-2021 at 02:19 PM.

  7. #7
    Registered User
    Join Date
    08-10-2012
    Location
    Washington
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: How to pass a parameter from PowerShell to Excel

    Quote Originally Posted by Bernie Deitrick View Post
    Example:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

    Since you want to pass a string

    Please Login or Register  to view this content.
    I did find that example on the internet, but I was confused by the "GenerateString" function inside Excel. The way that appears to work is PowerShell will open the *.xlsm file and call the GenerateString, in the Excel file. I couldn't see how it would give be useful for what I was wanting to do. In fact, the first example doesn't even pass a variable.

    But your second example makes it perfectly clear. In fact, that's how I would have expected it to pass a variable. I guess I just didn't try the obvious. I'll give it a try.

    Thanks,
    Last edited by MikeSD; 04-30-2021 at 03:11 PM.

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: How to pass a parameter from PowerShell to Excel

    The first example does pass a variable - the integer specifying the length of the string to be returned.

    Just be careful - I did not run my code using powershell, but I think I got the syntax correct: I've use VB to control Excel extensively, so the structures should be similar - I hope!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Hyperlink to local Excel document and pass parameter
    By antoineburdett in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-01-2018, 08:50 AM
  2. Replies: 2
    Last Post: 10-06-2014, 08:15 AM
  3. Replies: 0
    Last Post: 10-06-2014, 07:35 AM
  4. Replies: 2
    Last Post: 06-17-2012, 03:13 PM
  5. how to pass parameter to different sub?
    By ccs_1981 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-13-2008, 12:58 PM
  6. PASS down parameter from DOS to Excel
    By brucelim80 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-07-2007, 07:08 PM
  7. Pass down parameter from Web
    By brucelim80 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-14-2006, 09:39 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1