| View previous topic :: View next topic |
| Author |
Message |
Andrew
Joined: 10 Oct 2006 Posts: 16
|
Posted: Mon Oct 16, 2006 12:19 am Post subject: Passing a Variable to Excel |
|
|
| Is it possible to pass a variable to Excel so it can be used by a VBA subroutine? Thanks. |
|
| Back to top |
|
 |
slomz
Joined: 03 Sep 2006 Posts: 608 Location: Iowa, U.S.
|
Posted: Mon Oct 16, 2006 12:36 am Post subject: |
|
|
| Yes it is. |
|
| Back to top |
|
 |
Andrew
Joined: 10 Oct 2006 Posts: 16
|
Posted: Mon Oct 16, 2006 12:40 am Post subject: |
|
|
OK, I'll bite. How do I do it?
I'm writing a VBA subroutine to process a bunch of files in Excel. The quantity of files and file names change daily. VBA doesn't appear to have an "ifexists" function or the ability to open files using wildcards. Therefore, I would like to save all the file names I need to process in AutoHotkey and pass them to Excel. |
|
| Back to top |
|
 |
slomz
Joined: 03 Sep 2006 Posts: 608 Location: Iowa, U.S.
|
Posted: Mon Oct 16, 2006 1:34 am Post subject: |
|
|
| I know it is possible and may deal with dll calls, but I have not done anything with dll calls before so your not in luck. |
|
| Back to top |
|
 |
Andrew
Joined: 10 Oct 2006 Posts: 16
|
Posted: Mon Oct 16, 2006 2:10 pm Post subject: |
|
|
| Can anyone tell me how it's done or point me to some documentation on the subject? Thanks. |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Tue Oct 17, 2006 11:33 am Post subject: |
|
|
Open a new sheet and put the needed info in cells of this sheet... Then VBA can access this info easily. _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
Andrew
Joined: 10 Oct 2006 Posts: 16
|
Posted: Tue Oct 17, 2006 2:43 pm Post subject: |
|
|
Thanks, PhiLho. That's what I don't know how to do... "put the needed info in cells of this sheet". The variables have been assigned a value in AutoHotkey and I want Excel to read the values into a spreadsheet.
I think I need to use the EnvSet command and open Excel with the Run command. But I don't know exactly where to put the EnvSet command. Am I on the right track here? |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Wed Oct 18, 2006 9:26 am Post subject: |
|
|
I don't see how it relates to EnvSet.
Just Run Excel, wait for it, then Send the values separated by {Enter} or {Right}, they should go in separate cells. _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
nick
Joined: 24 Aug 2005 Posts: 345 Location: Berlin / Germany
|
Posted: Wed Oct 18, 2006 12:32 pm Post subject: |
|
|
| Andrew wrote: | | I think I need to use the EnvSet command and open Excel with the Run command. But I don't know exactly where to put the EnvSet command. Am I on the right track here? |
Passing the values via environment vars could be a quick and simple solution. But I don't understand your problem with "where to put the EnvSet command". Has to be before starting Excel, hasn't it?
Another option could be to put the values into a file (FileAppend). It's not as nifty as sending some mouseclicks to open a new sheet, putting the values into the cells, but it would do it, too.
Everyone to his taste! _________________ nick
denick @ http://de.autohotkey.com/forum/ |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Wed Oct 18, 2006 12:45 pm Post subject: |
|
|
I am a bit rusty on VBA. Does it has methods to get environment variables? And to read a file on disk? If so, these might be interesting alternatives, indeed. _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
Andrew
Joined: 10 Oct 2006 Posts: 16
|
Posted: Wed Oct 18, 2006 1:14 pm Post subject: |
|
|
OK, maybe I just don't know how to do it. My AutoHotkey script contains:
INTMX=value1
EnvSet,INTMX
Run, Excel.exe
Once Excel is open, I run a VBA script that contains:
Sub Macro1()
MsgBox INTMX
End Sub
And Excel displays an empty message box. The variable doesn't appear to have been passed. This seems really simple. What am I doing wrong?
Thanks. |
|
| Back to top |
|
 |
BoBo Guest
|
Posted: Wed Oct 18, 2006 1:38 pm Post subject: |
|
|
I doubt that you can use AHK's EnvSet to set a system environment variable. Even Run, %comspec% set won't work ... AFAIK  |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Wed Oct 18, 2006 2:19 pm Post subject: |
|
|
BoBo, I had same doubts, so I read the manual:
| AHK's Manual wrote: | | An environment variable created or changed with this command will be accessible only to programs the script launches via Run or RunWait. See environment variables for more details. | So it is OK in his use case, except that I doubt Excel / VBA sees environment variables as plain variables. At best, it can access them using some function. Andrew, you better hit the VBA manual... _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
Andrew
Joined: 10 Oct 2006 Posts: 16
|
Posted: Wed Oct 18, 2006 2:38 pm Post subject: |
|
|
| Thanks for your replies. In case it's not obvious, I'm very much a noobie to all of this. I'm not even a programmer by trade. I don't necessarily need to use the EnvSet command. I just want to pass some variables with their values from AutoHotkey to Excel. Has anyone done this before? |
|
| Back to top |
|
 |
nick
Joined: 24 Aug 2005 Posts: 345 Location: Berlin / Germany
|
Posted: Wed Oct 18, 2006 3:59 pm Post subject: |
|
|
| Andrew wrote: | | Thanks for your replies. In case it's not obvious, I'm very much a noobie to all of this. I'm not even a programmer by trade. I don't necessarily need to use the EnvSet command. I just want to pass some variables with their values from AutoHotkey to Excel. Has anyone done this before? |
To access the environment in VB\VBA you have to use the Environ\Environ$ function. To access files you have to use Open/Get/Close instructions. You should find the descriptions in the Excel help in the help menu. Reading helpfiles really can help! _________________ nick
denick @ http://de.autohotkey.com/forum/ |
|
| Back to top |
|
 |
|