Results 1 to 7 of 7

Thread: SendKeys Function and Alt

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    2

    SendKeys Function and Alt

    Hello,

    Within Excel I am iterating through a loop trying to perform the equivalent of typing 'Alt+1','Alt+2' etc., using SendKeys function. I'm pretty sure it's a timing issue, ie key release and posting http://www.vbforums.com/showthread.php?t=536446, user anhn explains the differences between special character keys and when they are released etc. I have not found a way to get this to work. The code below it simply iterates through spreadsheet and nothing is populated.

    I've tried probably 30 permuations with SendKeys function, to no avail.

    Many thanks.....Mick

    Sub ASCII()
    Dim iTmp As Long

    Range("A1").Select

    For iTmp = 1 To 15
    SendKeys "%" & iTmp, True
    ActiveCell.Offset(1, 0).Select
    Next iTmp
    End Sub

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: SendKeys Function and Alt

    If you think it's a timing issue, you might try adding a Sleep() API call to your loop.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: SendKeys Function and Alt

    Also, are you positive the Excel sheet has the keyboard focus when you call that loop?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: SendKeys Function and Alt

    i copied and pasted the code into excel, but of course as this is on w7, i just get permission denied for sendkeys

    if this is not resolved i will test in the morning on xp

    also note: that sending some of those alt keystrokes may clear the worksheet
    Last edited by westconn1; Jan 19th, 2011 at 03:17 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SendKeys Function and Alt

    Welcome to VBForums

    Thread moved to 'Office Development/VBA' forum... note that while it certainly isn't made clear, the "VB Editor" in Office programs is actually VBA rather than VB, so the 'VB6' forum is not really apt

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    2

    Re: SendKeys Function and Alt

    First off, thanks for all the responses. Glad to be aboard. Ok I tweaked the code and ran exactly what you see below. Resulted in col A, rows 10-15 populated with integer values 0,1,2,3,4,5; ie each cell has single integer. Curiously, rows 1-9 are blank.

    Sub ASCII()
    Dim iTmp As Long

    Range("A1").Select

    For iTmp = 1 To 15
    SendKeys "%" & iTmp, True
    SendKeys "~", True
    Next iTmp
    End Sub

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: SendKeys Function and Alt

    note: when sending alt & number keys combinations to screen or file, it is specific to number pad keys, not ascii values,
    eg alt 155 from number pad will produce &#162;, alt 0189 like &#189;
    the same combinations from the qwerty keys produce nothing at all
    sendkeys only sends ascii values, so can not differentiate
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width