+ Reply to Thread
Results 1 to 7 of 7

AppActivate Statement syntax won't compile

  1. #1
    Registered User
    Join Date
    08-23-2011
    Location
    Toronto,Ontario
    MS-Off Ver
    Excel 2003
    Posts
    48

    AppActivate Statement syntax won't compile

    I have 3 applications open with the main one running. When an event occurs in the main application it opens Excel with its' own built in command line to open Excel.

    Excel opens a file in the XLSTART folder that has an Auto_Open macro in it.

    The file opens and the macro runs and toggles back and forth between the other 2 open applications using SendKeys commands to enter instructions and data into both the open applications.

    However, when I sometimes toggle back into one of the open applications the focus is not the same as when I toggled out of the application.
    By that I mean the cursor is not where it was when I toggled out of the application.

    I have been using the TaskIDNum1[2] = Shell("C:\Program Files\Application1Folder\Application1FileName.exe",1) format to toggle back and forth between applications but I want to try and use the AppActivate Statement.

    I don't want to use the "AppActivate (Task ID Number, False)" format since this involves an extra step to retrieve the Task ID numbers of the open applications, because they are already both running and I am not opening them.

    I have tried every imaginable AppActivate programming format but nothing will work and I keep getting this compiling error "Expected: = "

    I am looking for a format as such: AppActivate ("C:\Program Files\Application Folder\ApplicationName.exe", False)

    I must be missing something in the syntax and all examples of the AppActivate Statement show the use of the AppActivate(Task ID number,[True][False]) format which I don't want to use.

    I have Microsoft Office 2003 and Windows XP - Home Edition.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: AppActivate Statement syntax won't compile

    Hello HitTheEXCELerator,

    Add a new VBA module to your workbook and paste the code below into it. This will allow you to activate a separate instance of Excel by its workbook name. The macro will even match using a partial workbook name.

    To use the macro call it from your VBA code like this: ActivateWorkbook "Book1.xls"
    Change "Book1" to the workbook name you want activated.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    08-23-2011
    Location
    Toronto,Ontario
    MS-Off Ver
    Excel 2003
    Posts
    48

    Re: AppActivate Statement syntax won't compile

    I don't know what all this code is about. In my explanation I have 3 applications open. The 2 applications that are going to have information processed in them could be Adobe and Notebook. When my Auto_Open Excel file opens it puts data information into these applications and sends SendKeys commands to process that information. The problem is that when the macro toggles out of Notepad and then comes back again the cursor is not where it was when I left the program.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: AppActivate Statement syntax won't compile

    Hello HitTheEXCELerator,

    In the Windows environment, each application is assigned a unique portion of memory in which it runs. These spaces are isolated from one another by the system to prevent one program from changing another programs values. This is also referred to as "apartment" threading. The only way to make these separate applications share information is to "attach" the memory of one to the other. That is what this code does. It will find the application by its Window name and attach it temporarily to the Excel workbook to bring the other application to the top of all other windows and set the focus on it.

    Your problems are related to the "SendKeys" method. This method will send the string to whichever window has the focus. The code posted ensures the application window has the focus so "SendKeys" will work correctly.

  5. #5
    Registered User
    Join Date
    08-23-2011
    Location
    Toronto,Ontario
    MS-Off Ver
    Excel 2003
    Posts
    48

    Re: AppActivate Statement syntax won't compile

    The 2 other applications which are open do not "share" information. Instead they get different information placed into them.

    For example, a Microsoft WORD file(file 1) may be open and my Excel macro adds a predetermined string into a paragraph at a predetermined location in the paragraph and then the cursor scrolls down to the next line in the paragraph. A text file(file 2) is also open and is acting like a log file. The Excel macro goes to the text file and the cursor is positioned on a new line below the last text entry. The information that is entered is simply the number of the line(which is already known) in the WORD document where the string was added. The "ENTER" key is sent by SendKeys method to start a new line in the text file. So you can see it is different data being entered into both files and not being shared.

    The problem is similar to that sometimes when I return to the text file the cursor is not positioned on a new line where I left it so new data should go on a new line, and instead of getting 2 separate string entries of let's say (line)"123" and (line)"129" I get one long string of "123129" because the cursor is positioned at the end of the last entry.

    I hope this explanation will more fully clarify what is happening. I guess perhaps this longer explanation should have been used in my original post for easier understanding.

  6. #6
    Registered User
    Join Date
    08-23-2011
    Location
    Toronto,Ontario
    MS-Off Ver
    Excel 2003
    Posts
    48

    Re: AppActivate Statement syntax won't compile

    "The problem is similar to that sometimes when I return to the text file the cursor is not positioned on a new line where I left it so new data should go on a new line, and instead of getting 2 separate string entries of let's say (line)"123" and (line)"129" I get one long string of "123129" because the cursor is positioned at the end of the last entry."

    This needs to be better explained:

    The problem is similar to that sometimes when I return to the text file the cursor is not positioned on a new line where I left it so new data should go on a new line, and instead of getting 2 separate string entries of let's say (line 1 in the WORD file)"123" and (line 2 in the WORD file)"129" I get one long string of "123129" because the cursor is positioned at the end of the last entry.

    The text logging file could be NOTEPAD. Sorry for not being more accurate in the former description.

    Entries in the text file should look like this:

    123
    129

    not like this

    123129

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Why not use VBA's file I/O methods to write to the text file?
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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