cancel
Showing results for 
Search instead for 
Did you mean: 

Vba Work in diferent SAP mods

Former Member
0 Kudos

Hello, please i need help with a vba code, im currently working with iw32 transaction to count materials in te cost centers of the diferent areas, and ive developed a code to simplify this process; however i want to be able to open an amount of sap mods and be able to work with each of them at once, but evry time i open a new mod, the macro keeps on working with the previous one, can anybody help me with this!

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos


Thanks Holger you understood what i meant to explain, and also thanks for replying again Sayuti, Ive managed to run the macro without having any error messags but it still doesnt work as i want to, it just work with one session,  how can i make this work for evry session that I open?

If Not IsObject(App1) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set App1 = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = App1.Children(0)
End If
If Not IsObject(session) Then
   Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session, "on"
   WScript.ConnectObject App1, "on"
End If
session.findById("wnd[0]").maximize
session.createSession

  

Dim i As Integer
For i = 0 To Connection.Children.Count

     Set sapSession = Connection.Children(CLng(i))


session.findById("wnd[0]/tbar[0]/okcd").Text = "/n iw32"
session.findById("wnd[0]").sendVKey 0

Next i

holger_khn
Contributor
0 Kudos

Dim i As Integer
For i = 0 To Connection.Children.Count
     Set sapSession = Connection.Children(CLng(i))

sapsession.findById("wnd[0]/tbar[0]/okcd").Text = "/n iw33"
sapsession.findById("wnd[0]").sendVKey 0
Next i

you have to use the correct object

sapSession instead of session

Former Member
0 Kudos

It works!! now i just must adapt it for my idea... Thanks again experts.

Former Member
0 Kudos

It worked for me only if I use the statement as such:

For i = 0To Connection.Children.Count - 1

Without the minus 1, I got the "enumerator of collection cannot find an element with the specified  index" error.

Regards

Umur

former_member213011
Participant
0 Kudos

Dear Umur,

The Children collections enumerator starts with 0 and should end with Count - 1. Else, the loop will be trying to access a non-existent object given by the last number of the FOR counter.

Thanks,

Sayuti

Former Member
0 Kudos

Hello Sayuti. Great Tips!!

I have a similar problem, but I couldn´t fix it with this material.

I my Windows 8 MicrosofOffice2010 (Excel) it is working, on the user Windows 7 Office 2013 Doesn´t work. (I try to run it on the Win7 Office 2010 and doesn´t work too)

The code below sent me the  error:

The enumerator of the collection cannot find en element with the specified index.

     Dim VerItem As Integer

     For Each linha In session.findById("wnd[0]/usr/subHEADER_AND_ITEMS:SAPLMR1M:6005/subITEMS:SAPLMR1M:6010/tabsITEMTAB/tabpITEMS_PO/ssubTABS:SAPLMR1M:6020/subITEM:SAPLMR1M:6310/tblSAPLMR1MTC_MR1M").Rows

       VerItem = Trim(linha(6).displayedText)

       ref = Trim(linha(0).Text)

     

           For Each lingrid In session.findById("wnd[0]/usr/subHEADER_AND_ITEMS:SAPLMR1M:6005/subITEMS:SAPLMR1M:6010/tabsITEMTAB/tabpITEMS_PO/ssubTABS:SAPLMR1M:6020/subITEM:SAPLMR1M:6310/tblSAPLMR1MTC_MR1M").Rows

            

               If ref <> lingrid(0).Text Then

                  

                   If VerItem = Trim(lingrid(6).Text) Then

                  

                    MIRO = "Miro contém itens repetidos, favor verificar"

                    Exit Function

                  

                   End If

                  

               End If

          

           Next lingrid

           

     Next linha

Thanks in advance

Best Regards,

Marcus.

Answers (3)

Answers (3)

Former Member
0 Kudos

One more question, hw can I find an empty position in a sap transaction, lets say iw32, to load the material I need to loop between full psitions util i find an empty one, how can i do this?

a = session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/ctxtRESBD-MATNR[1,4]").Value

session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/ctxtRESBD-MATNR[1,5]").Text = "1250927"

session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/txtRESBD-MENGE[4,4]").Text = "2"

session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/txtRESBD-MENGE[4,5]").Text = "2"

session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/txtRESBD-MENGE[4,5]").SetFocus

session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/txtRESBD-MENGE[4,5]").caretPosition = 18

holger_khn
Contributor
0 Kudos

Hello.

There are many ways how to solve it. One way to get a specific line is to check line by line until you have automatically scrolled to the row which you want to update.

.

    Do Until Session.FindById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/ctxtRESBD-MATNR[1,0]").Text = strMATNR

        If Session.FindById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/ctxtRESBD-MATNR[1,0]").Text <> strMATNR Then

            Session.FindById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020").VerticalScrollbar.Position = iScroll

            iScroll = iScroll + 1

        End If

    Loop

The other Option is to scroll whole Screen down and check if the first line visible is empty or filled. Scroll so Long as the first line is not free to use.

Former Member
0 Kudos

Great!! thnks Holger it works now perfet I used this code:

If Not IsObject(App1) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set App1 = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = App1.Children(0)
End If
If Not IsObject(Session) Then
   Set Session = Connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject Session, "on"
   WScript.ConnectObject App1, "on"
End If
  
  
  
Session.FindById("wnd[0]").maximize
Session.FindById("wnd[0]/tbar[0]/okcd").Text = "/n iw32"
Session.FindById("wnd[0]").sendVKey 0
Session.FindById("wnd[0]/usr/ctxtCAUFVD-AUFNR").Text = "100357211"
Session.FindById("wnd[0]/usr/ctxtCAUFVD-AUFNR").caretPosition = 9
Session.FindById("wnd[0]").sendVKey 0
Session.FindById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1100/tabsTS_1100/tabpMUEB").Select

iscroll = 0

 
  Do Until Session.FindById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/ctxtRESBD-MATNR[1," & iscroll & "]").Text = strMATNR

        If Session.FindById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/ctxtRESBD-MATNR[1," & iscroll & "]").Text <> strMATNR Then

            Session.FindById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020").VerticalScrollbar.Position = iscroll

            iscroll = iscroll + 1

        End If

    Loop

 


Session.FindById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/ctxtRESBD-MATNR[1," & iscroll - 1 & "]").Text = "1250927"

holger_khn
Contributor
0 Kudos

Hello. May I misunderstand your request, but you can adopt VBA coding to more than one session at a time. Therefore you can Loop over all open sessions. During this you just Need to skip a session which is busy. This can be checked with 'Busy'-property on a session.

The only additional requirement is to have an seperate Excel instance run. As you can´t run both in one instance.

If you can give me more Details about your expectations I can ggive some more Detail Information how this can be done.

Holger

former_member213011
Participant
0 Kudos

Agree with Holger.

You can open multiple sessions of SAP and run the script on each session sequentially, but not all at once. You can probably use a FOR loop to go through each open session.

Example if multiple sessions on a single connection:

Dim i As Integer

For i = 0 to SAPConnection(0).Children.Count - 1

     Set SAPSession = SAPConnection(0).Children(CLng(i))

     .

     ..

     (Your material counts procedure here)

     ..

     .

Next i

The Busy property is pretty useful if you need to wait for a particular respond from the SAP server, but frankly, I have not been successful in using it lol.

former_member213011
Participant
0 Kudos

AFAIK, we can only execute one session at a time.

From the SAP Scripting API Help File:


The GuiApplication represents the process in which all SAP GUI activity takes place. If the scripting component is accessed by attaching to a SAPlogon process, then GuiApplication will represent SAPlogon. GuiApplication is a creatable class. However, there must be only one component of this type in any process.

Though, you can switch between different sessions by assigning the ID of a GuiSession windows to a the GuiSession object variable in VBA.

Former Member
0 Kudos

Thanks a lot Sayuti, I guess ill have to manage.