Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9011959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:52:38+00:00 2026-06-16T02:52:38+00:00

I will explain everything that I have found but in short I just want

  • 0

I will explain everything that I have found but in short I just want to set the value of cell A1 in code behind. I have tried Create Excel Add-in – get cell value plus other links and all those techniques work only if I run them on a macro but I want execute them from a function.

So let me start explaining:

I do not understand why I get a wierd behaviour when I run the same code on a function versus a Sub. Take the following example:

Dim TimeToRun

Sub Macro1()

    'Dim addIn As COMAddIn
    'Dim automationObject As Object
    'Set addIn = Application.COMAddIns("ProbeAddIn")
    'Set automationObject = addIn.Object

    'automationObject.ImportData

    MsgBox "Hello world"

End Sub

Sub Macro2()

    TimeToRun = Now + TimeValue("00:00:01")

    Application.OnTime TimeToRun, "Macro1" ' run Macro1 on 1 seconds

End Sub


Function Test()

    TimeToRun = Now + TimeValue("00:00:01")

    Application.OnTime TimeToRun, "Macro1" ' run Macro1 on 1 seconds

End Function

Note that Macro2 and function Test have the same code. Why is it that if I run Macro2 it works ok (message box shows up). But if I go to a a cell and type =Test() then I get an error even though I have the same code !

The reason why am I showing this example is because the code that I have commented out on Macro1 works great if I run it by running the macro directly. If I place that code inside the function Test it does not work. That code is executing the following method in my add-in project on visual studio:

enter image description here

The exception is:

System.Runtime.InteropServices.COMException was unhandled by user code
HResult=-2146827284 Message=Exception from HRESULT: 0x800A03EC
Source=”” ErrorCode=-2146827284 StackTrace:
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes,
MessageData& msgData)
at Microsoft.Office.Interop.Excel.Range.set_Value2(Object value)
at ReadWrite.ImportData() in C:\Users\Antonio\Dropbox_Temp\visual studio\Probe add in
test\ProbeAddIn\ProbeAddIn\Class1.cs:line 82 InnerException:

I got that exception because I ran:

Function Test()

    Dim addIn As COMAddIn
    Dim automationObject As Object
    Set addIn = Application.COMAddIns("ProbeAddIn")
    Set automationObject = addIn.Object                

    automationObject.ImportData

End Function

Instead of

Sub Test()

    Dim addIn As COMAddIn
    Dim automationObject As Object
    Set addIn = Application.COMAddIns("ProbeAddIn")
    Set automationObject = addIn.Object

    automationObject.ImportData

End Function

How can I make the code inside a macro and function run the same way? I want to trigger the execution of that method when user types in a formula not when the user runs a macro. Even if I have the function run the macro I get the same exception The only way I do not get an exception is if the first thing that runs is a macro…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:52:40+00:00Added an answer on June 16, 2026 at 2:52 am

    Not sure, but the issue you have may be due to the fact that when your function is fired the Excel application is in edit mode and so cannot accept any input from outside hence the exception when you try to insert data.

    If you want to update a cell in a continuous way in a clean manner what you need is a RTD server.

    From your cells you will simply call :

    =RTD("my.super.rtdserver",,"mydata")
    

    From time to time your RTD server will notify Excel of new data and lets Excel call-back to retrieve them when it is ready.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to reduce some code here. I will explain how I have
I have found some threads that explain why C++ separates .cpp and .h files
Ok this is strange but I will explain what is going on. In SQL
The question is complicated but I will explain it in details. The goal is
The title is a little confusing so I will explain. I have the following
Here I am facing a problem, I will explain: I set up a calendar
Well, the subject says it all but I will explain a little further. I
I have problem adding arraylist to list view, will explain about my problem here..
I want to use regular expression same way as string.Format. I will explain I
This is a general question of sorts, but to explain it I will use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.