2017-06-25 57 views
2

也許有人可以幫我創建一個使用VBA的ExcelVBA SAP了createSession

一些代碼來理解這個問題

If Not IsObject(sap) Then 
    Set SapGuiAuto = GetObject("SAPGUI") 
    Set sap = SapGuiAuto.GetScriptingEngine 
End If 
If Not IsObject(Connection) Then 
    Set Connection = sap.Connections.Item(0) 
End If 
If Not IsObject(session) Then 
    Set session = Connection.Children(0) 
End If 

大多數情況下,這一個做工精細的新的SAP會話。但有時這部分不工作

Set session = Connection.Children(0) 

當SAP超時發生(一些空閒時間後自動註銷),它發生的例子。 在這種情況下我有

sap.Connections.Count 

= 2

Connection.Sessions.Count 

= 0
貌似timeouted連接仍然在SAP掛起某處。因此,當我嘗試連接到第一個連接的第一個會話時,出現錯誤,第一個連接中沒有會話。

我想要做的是創建新的會話。 我可以

Dim sapSession As SAPFEWSELib.GuiSession 
Dim sapCon As SAPFEWSELib.GuiConnection 
Set sapCon = sap.Connections.Item(0) 
Set sapSession = Connection.sessions.Item(0) 

sapsession.createsession 

做這一個工作正常,但它並不能幫助職高我還需要設置會話第一

有沒有辦法設置連接後創建會話?
類似於sapCon.createsession

有沒有人知道我如何使用特定的會話使用變量?

Set sapSession = Connection.sessions.Item(0) 

這工作正常,但是當我嘗試

Dim SessionNumber as integer 
.... 
SessionNumber = 0 
Set sapSession = Connection.sessions.Item(SessionNumber) 

它拋出

回答

1

Excel需要會話數是一個整數的錯誤「壞指數型收集訪問」,這樣你就可以使用Cint()中的類型轉換。奇怪的是,即使SessionNumber被定義爲一個整數,這也是被建議/需要的。

Dim SessionNumber 
.... 
SessionNumber = 0 
Set sapSession = Connection.sessions.Item(Cint(SessionNumber)) 
+1

剛回來這個解決的辦法是 '設置sapSession = Connection.sessions.Item(CINT(SessionNumber))' – Sphinx

0

CreateSession命令執行迅速並返回到Excel,但SAP需要一段時間才能完成打開新會話。因此,你需要讓你的Excel代碼等待。

下面是一個例子:

Const ms as Double = 1.15740741E-08 ' one millisecond, in days = 1/(1000*24*60*60) 

Dim sapCon As SAPFEWSELib.GuiConnection 
Dim sapSession As SAPFEWSELib.GuiSession 
Dim nSessions As Integer 

Set sapCon = sap.Connections(0) 
Set sapSession = sapCon.Sessions(0) 

nSessions = sapCon.Sessions.Count 

sapSession.createsession 

Do 
    Application.Wait (Now() + 500*ms) 
    If sapCon.Sessions.Count > nSessions Then Exit Do 
Loop 

Set sapSession = sapCon.Sessions.Item(CInt(sapCon.Sessions.Count - 1))