I do not see a problem with your approach. You do not have using in VBA, so you have to simulate it
- using an error handler and
- very carefully so as not to leave the method without using TransactionScope in one way or another.
For example, let's say that in your C # application you provide the following methods:
public TransactionScope BeginTransactionScope() { return new TransactionScope(); } public void CommitTransactionScope(TransactionScope scope) { scope.Complete(); scope.Dispose();
You compile your C # DLL and expose it to Excel VBA through COM interoperability. In VBA, you run the methods as follows:
Private Sub MySub() On Error Goto MyErrorHandler ... Dim scope As Object Set scope = myCsObject.BeginTransactionScope() ... myCsObject.DoSomeWork1 ... myCsObject.DoSomeWork2 ... myCsObject.DoSomeWork3 ... myCsObject.CommitTransactionScope scope Set scope = Nothing ... Exit Sub MyErrorHandler: If Not (scope Is Nothing) Then myCsObject.RollbackTransactionScope scope End If ' Remainder of your error handler goes here ... End Sub
Note that any data access that you perform with VBA code (between DoSomeWork calls) is outside the scope of the transaction, because TransactionScope is not compatible with classic ADO or DAO.
Heinzi
source share