VBA AppActivate with SAP T-Code

plwhittington

Board Regular
Joined
May 10, 2007
Messages
132
I've written VBA code in Excel to (1)run some t-codes in SAP, (2)capture screenshots, and (3)paste in MS Word.

Everything works, but I have to hardcode the name of the SAP screen when it's time to switch focus from Excel to SAP in order to do the screen capture. I can hardcode every SAP Screen to capture in the code, but that is very tedious. I would like for the program to be able to get SAP to the correct screen, identify the screen name it is on, then use AppActivate to set the focus on that screen for screen capture.

Currently, I call the following code in a subroutine, which executes the SM35 T-code in SAP:

session.findbyId("wnd[0]").maximize
session.findbyId("wnd[0]/tbar[0]/okcd").Text = "/nsm35"
session.findbyId("wnd[0]").sendVKey 0
session.findbyId("wnd[0]/usr/subD1000_HEADER:SAPMSBDC_CC:1005/txtD0100-MAPN").Text = "FI_TRA*"
session.findbyId("wnd[0]/usr/subD1000_HEADER:SAPMSBDC_CC:1005/ctxtD0100-VON").Text = "11/10/2014"
session.findbyId("wnd[0]/usr/subD1000_HEADER:SAPMSBDC_CC:1005/ctxtD0100-BIS").Text = "11/10/2014"
session.findbyId("wnd[0]/usr/subD1000_HEADER:SAPMSBDC_CC:1005/txtD0100-CREATOR").Text = "*"
session.findbyId("wnd[0]/usr/subD1000_HEADER:SAPMSBDC_CC:1005/ctxtD0100-BIS").SetFocus
session.findbyId("wnd[0]/usr/subD1000_HEADER:SAPMSBDC_CC:1005/ctxtD0100-BIS").caretPosition = 10
session.findbyId("wnd[0]").sendVKey 0

once the code runs, the correct SAP screen is present, but not active. I can activate it by using the following code:

AppActivate "Batch Session: Input Overview"


"Batch Session: Input Overview" is the name of the Task in Windows Task Manager (not the process name). I need to be able to use a variable in place of it so I don't have to look up and hard code every screen in SAP.

Thanks for any help.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi,

Sorry, but I didn't spot the actual question.

Using a variable with AppActivate should be easy. E.G: AppActivate myVariable
but I am guessing that is not the question.

If you are using SM35 then won't the current screen always be something like: "Batch Session: Input Overview"?
Or is it the screen that the SM35 processing creates that you are trying to find so that you can put it into an AppActivate command?
That may be difficult because your VBScript session in SAP only knows about the current session, I think.
But again, if you always run that script won't it always end up at the same screen?

I think you will need to supply some more info please.

Incidentally, I suspect that the SetFocus and caretPosition commands will not actually be required. They just tell SAP that you left the cursor in the "To" date field when you made your recording.

Out of interest, is there any reason why you use SM35 and not get the script to run the other transaction directly?

You certainly can populate variables in these scripts. I run mine from Excel so I can automate some repetitive tasks.
 
Upvote 0
Sorry- it's difficult to explain this clearly.....
When running an SAP session, the screen name/caption changes, depending on which t-code you are using and what you are doing within it. Therefore, the only way to be able to use the AppActivate command is to know exactly what the caption is. However, it is always displayed in Task Manager, on the APPLICATIONS tab under the TASK column. I can find lots of code to show me the PROCESSES running, but that is not what I need.
In my code, VBA is calling SAP, executing some processes and ending on some screens (which I get screenshots of). I keep thinking there should be a way for VBA to know what screen it is on, even if I have to use an API call....
Hope this clears it up a little. Thanks for the help.
 
Upvote 0
trying Z=Session.ActiveWindow.Name and it returns the value "wnd[0]". I actually trying to set the focus on the SAP screen and the only way I know to do that is by using the AppActivate command. If there is another way to activate the SAP screen using the Session.ActiveWindow command, I'm open to suggestions. Thanks.
 
Upvote 0
Just figured it out- Z = session.findById("wnd[0]").Text returns the Text Name of the session. Thanks again for the help.
 
Upvote 0
Hi all

Years late, i know, but I thinks this relates to a problem I'm facing.

Just figured it out- Z = session.findById("wnd[0]").Text returns the Text Name of the session. Thanks again for the help.

plwhittington - did you figure out how to re-activate a SAP window so you can go back and forth between Excel and the one SAP window?

If so, could you share please? Like, as in a little macro that will reactivate "session.findById("wnd[0]")" so it will accept more input from Excel\VBA? :)


Thanks
Chris
 
Upvote 0
Hello Rapton6L, years late, i thinks to a problem, the solution is

DisplaySAP = session.findById("wnd[0]").Text
AppActivate DisplaySAP
 
Upvote 0
Thanks CB. Not quite how I got it done, but that way is probably the most correct way to do it.

I got it sorted with just AppActivate ("insert screen name here")
I found when you're running through a macro where it changes SAP transactions, the screen name remains the first transaction opened.
E.g. MMBE screen name is "Stock Overview". In the same macro if MMBE is changed to MN02, the windows screen name remains "Stock Overview", so AppActivate ("Stock Overview") successfully reactivates the SAP screen throughout the entire macro.

Cheers
Chris
 
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top