Updating the For Next AutoHotkey in the VBE
Last month I posted about some AHK scripts I was starting to use to make the VBE a little less gross every day. There were some awesome comments. I took Hubisan’s comment and ran with it through a few iterations. First, let’s go to the video.
I use a program called CamStudio. For some reason it’s blurry for the first 30 seconds. I really need to get Techsmith’s Camtasia. But it gets the point across for now.
I’m seriously digging the AHK stuff. Here’s the script:
;when you type for{space}, replace it with caps so you know you’re in AHK mode
SendInput FOR{Space}
;wait for the next word and store it in counter
Input, counter,I V T10,{Space}{Escape}
;finish with ESC and you thwart AHK
;but finish with a space and more stuff happens
if (ErrorLevel = "EndKey:Space")
{
;if the next word is each, it’s a for each loop
if (counter = "each")
{
;wait for the next word and store it in eachctr
Input, eachctr, I V T10,{Space}{Escape}
if (ErrorLevel = "EndKey:Space")
{
;Once you know eachctr, fill in the Next line and go back up to the For line
SendInput +{HOME}{DELETE}{Enter}Next %eachctr%{Up}For Each %eachctr%{Space}
}
}
;if the next word is one of these, you’re opening a text file
else if (counter = "Append" or counter = "Binary" or counter = "Input" or counter = "Output" or counter = "Random")
{
;get the next word – it really should only be ‘As’
Input, askeyword, I V T10,{Space}{Escape}
if (ErrorLevel = "EndKey:Space")
{
if (askeyword = "As")
{
;the word after ‘As’ is the file number
Input, filenum, I V T10,{Enter}{Escape}
if (ErrorLevel = "EndKey:Enter")
{
;complete the close statement, because I always forget that.
SendInput {Enter}Close{Space}
;you got to send this part raw because there may be a # in there and that’s special
SendRaw %filenum%
SendInput {Up}
}
}
}
}
else
{
;and finally if it’s not all that special stuff, it’s just a for next
SendInput +{HOME}{DELETE}{Enter}Next %counter%{Up}For %counter%{Space}
}
}
Return
I put in comments so hopefully you can follow along. All I’ve done is copy Hubisan’s code, so if I took something nice and made it total crap it’s because I don’t know what I’m doing.
Bob Phillips made a good point in the last post about how he doesn’t prefer the automation. The automation gets in the way sometimes and typing the code slows things down so you can use your brain a little more. Good points, I thought, but I still like the automation. I can relate to the point that it gets in the way sometimes. In a previous iteration, I would type For i
and it would put Next i, plus a blank line, plus a tab. That means when I’m done with the For statement, I have to arrow down. I don’t want to arrow down. I want to hit enter, then tab. So I made the automation fit the way I want to work and now I’m very happy with it.