+ Reply to Thread
Results 1 to 7 of 7

Files do not open until end of program?

  1. #1
    Registered User
    Join Date
    06-08-2010
    Location
    Tennessee
    MS-Off Ver
    Excel 2003
    Posts
    3

    Files do not open until end of program?

    I have written a macro (attached in .txt format) to carry out these actions:

    Copy data from an already opened CSV file.
    Paste data into template file.
    Open next data file (I'm using sendkeys "{down}{enter}")

    I want to loop these commands so I can compile data from many files by only running the macro once. However, no matter where I place the open command (actually a sendkeys statement) the file seems to not open until the program is completed.

    So if I loop it 10 times, it works properly for the first file, then carries out the commands 9 more times, only without opening the actual files I need. After all has been done, the 9 files open, at which point they are no longer useful!

    Is there any way around the issue of the files opening at the very end?
    Also, is there a way to tell Windows Explorer to open the next file without using Sendkeys "{Down}{Enter}" ?

    Any amount of help is greatly appreciated! Thank you,
    Luke
    Attached Files Attached Files
    Last edited by Luke2010; 06-09-2010 at 08:26 AM.

  2. #2
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    re: Files do not open until end of program?

    Luke;
    Quote Originally Posted by Luke2010 View Post
    However, no matter where I place the open command (actually a sendkeys statement) the file seems to not open until the program is completed.
    Sendkeys just puts the keystrokes into your computers buffer. It doesn't actually send them until your computer is at a wait state (like the end of the program).
    You can put a "Stop" in the program, then it will send the keystrokes, but you will also have to Sendkeys "{F5}" to restart the program.
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  3. #3
    Registered User
    Join Date
    06-08-2010
    Location
    Tennessee
    MS-Off Ver
    Excel 2003
    Posts
    3

    re: Files do not open until end of program?

    Quote Originally Posted by foxguy View Post
    Luke;

    Sendkeys just puts the keystrokes into your computers buffer. It doesn't actually send them until your computer is at a wait state (like the end of the program).
    You can put a "Stop" in the program, then it will send the keystrokes, but you will also have to Sendkeys "{F5}" to restart the program.
    Thank you for your input! Now I understand why the macro is responding this way.
    However, placing the stop doesn't seem to be changing anything. With no stop, the keys are sent at the end. With the stop, they don't seem to send at all!

    Are there other methods of pausing to send the keys? I already tried inserting
    Application.Wait Time + TimeSerial(0, 0, 1)
    after the Sendkeys statement, to no avail.

    Thanks again!
    Luke

  4. #4
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    re: Files do not open until end of program?

    Luke;
    However, placing the stop doesn't seem to be changing anything. With no stop, the keys are sent at the end. With the stop, they don't seem to send at all!

    Are there other methods of pausing to send the keys? I already tried inserting
    Application.Wait Time + TimeSerial(0, 0, 1)
    after the Sendkeys statement, to no avail.
    Application.Wait doesn't stop for input from the keyboard. It just stops the macro for the specified amount of time before continuing. You might use it to have a macro wait for a web page to finish loading, or in a trivia app wait for a specified amount of time before automatically displaying the answer, etc.

    I was wrong about using stop
    The keys will be sent to wherever the cursor is when the stop occurs. When your macro hits the "Stop" it places the cursor in the Code Window with the Stop in it, and the keys in the buffer would then be sent there.
    I've only used SendKeys when I'm writing code, not running a macro.

    One of the things I have discovered is that SendKeys is picky about how it works on different computers.
    I have a macro that used SendKeys and it worked fine on my desktop computer, but didn't work at all on my laptop. Both computers are using Vista, so it confuses me why it won't work, but it doesn't. http://www.excelforum.com/excel-prog...ith-vista.html

    These are some things you can try, but if they don't work, you'll have to find someone else to help.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    You can keep it as 2 lines, but each one should have True as second argument.
    You might try Application.Sendkeys instead of Sendkeys, but my gut tells me that won't make a difference, since you're trying to send keys to a directory window and not an Excel window.

    Final Note:
    Your macro is very inefficient. You do not need to keep "Selecting" different things. The only thing you need to "Select" or "Activate" is the window that needs to receive the SendKeys string.
    There are very few things in Excel that require you to "Select" or "Activate" before doing.
    Also, you should not hard code any addresses (Like "B8") in a macro. It may not matter now, but as you develop more macros, you will regret having them hard coded. You should use Range Names, or at the very least assign the Address to a variable and use the variable throughout your code. Then when you need to change the address, you only have to change it in one place.
    Example:
    Please Login or Register  to view this content.


    If you use this approach you won't need to use SendKeys at all.
    Please Login or Register  to view this content.
    Last edited by foxguy; 06-10-2010 at 10:25 PM.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Files do not open until end of program?

    here'ssomecodetoloop through a folder 7 copy the used range to a specific sheet
    Please Login or Register  to view this content.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Registered User
    Join Date
    06-08-2010
    Location
    Tennessee
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Files do not open until end of program?

    Thanks so much for the help!
    I won't be able to work on this project next week, but I'll begin implementing these methods the following week. I'll be sure to rate posts and such and let you guys know how it works.
    Thanks again!
    Luke

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,983

    Re: Files do not open until end of program?

    Just FYI, I'd avoid using FileSearch unless you want to have to rewrite your code when you upgrade to a newer version of Office. You might as well use Dir or the FileSystemObject now and avoid the hassle!
    Everyone who confuses correlation and causation ends up dead.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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