For those who want to Automate the SAP GUI and are only able to do so through VBA and VB.Net. Here is a simple solution to automate SAP using C#:
First add two references:
SAPFEWSELib
SapROTWr <---- This one is located in the folder where SAP was installed.
Then add the using statements:
using SAPFEWSELib;
using SapROTWr;
Once those are added, add the following to a button:
SapROTWr.CSapROTWrapper sapROTWrapper = new SapROTWr.CSapROTWrapper();
object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI");
object engine = SapGuilRot.GetType().InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuilRot, null);
GuiApplication GuiApp = (GuiApplication)engine;
GuiConnection connection = (GuiConnection)GuiApp.Connections.ElementAt(0);
GuiSession session = (GuiSession)connection.Children.ElementAt(0);
GuiFrameWindow frame = (GuiFrameWindow)session.FindById("wnd[0]");
This example works exactly like the Visual Basic Version does.
If you want to enter text into a text field within SAP, you must explicitly cast the object into the correct variable type
an example of entering an order number into CO02 Transaction, would be:
GuiTextField ordNum = (GuiTextField)session.FindById("wnd[0]/usr/ctxtCAUFVD-AUFNR");
ordNum.Text = textBox1.Text;
frame.SendVKey(0);