Read Write Error

147 views
Skip to first unread message

Jon49

unread,
Feb 6, 2012, 5:58:38 PM2/6/12
to Excel-DNA
I get the following error when running my code.

"Attempted to read or write protected memory. This is often an
indication that other memory is corrupt."

The first time through it works fine, the second time it throws the
above error.

Here's my code:

Public Shared Function RangeExists(ByVal sName As String) As
ExcelReference

Dim rRng As ExcelReference

Try
rRng = XlCall.Excel(XlCall.xlfEvaluate, sName) '<<This is
where the error occurs (like I said, 1st time find, 2nd time throws
error>>
Catch
Try
rRng = XlCall.Excel(XlCall.xlfEvaluate, "!" & sName)
Catch
rRng = Nothing
End Try
End Try

Return rRng

End Function

Govert van Drimmelen

unread,
Feb 7, 2012, 5:29:17 PM2/7/12
to Excel-DNA
Hi,

In what context are you calling this function - is it perhaps from a
ribbon event handler perhaps?
The Excel C API (XlCall etc.) should not be used directly from a
ribbon event handler, while the COM automation interface should be
fine. If you do want to call the C API from a ribbon event handler,
you need to put it in a macro that you call via
Application.Run("myMacro",...)

-Govert

Jon49

unread,
Feb 7, 2012, 5:53:55 PM2/7/12
to Excel-DNA
I am calling it from a form. The button on the form is clicked and
then it calls to get the range. When I open the form it gets this same
range in the same manner and has no problem, it is only the second
time it is called that it has a problem. The formula that it is called
in resides in a class that is shared.

Govert van Drimmelen

unread,
Feb 7, 2012, 6:10:29 PM2/7/12
to Excel-DNA
OK, could you try this instead:

* Make a macro ( Public Shared Sub MyWork ) that does the work you
want to do in the button press.

* From the button handler, call
ExcelDnaUtil.Application.Run("MyWork")
instead of doing the work directly.

I suspect Excel is not in a context to handle the C API from the
form's message loop.

(I presume there are no other threads involved - you definitely can't
use the C API from another thread).

-Govert

Govert van Drimmelen

unread,
Feb 7, 2012, 6:16:08 PM2/7/12
to Excel-DNA
Oh, and you might consider just using the COM automation API in that
context anyway.
So rewrite your function as something like:

Public Shared Function RangeExists(ByVal sName As String) As
Object
Dim excelApp As Object
Dim rRng As Object
Try
excelApp = ExcelDnaUtil.Application
rRng = excelApp.Evaluate(sName)

Catch
Try
rRng = excelApp.Evaluate("!" & sName)
Catch
rRng = Nothing
End Try
End Try
Return rRng
End Function

-Govert

Jon49

unread,
Feb 14, 2012, 5:14:49 PM2/14/12
to Excel-DNA
Thanks, I just used the COM way instead, I'll keep that in mind if I
have something that needs more speed. When I used the
ExcelReference.Set() method it also doesn't work, I'm assuming it is
the same reason. It let's me use the "GetValue" method just fine, just
not the "Set" method unfortunately.
Reply all
Reply to author
Forward
0 new messages