Windows Phone Developers

Tuesday, July 29, 2008

Reference to a non-shared member requires an object reference


How to Reference Word/Excel application in a UserControl/TaskPane



After creating taskpane (through user control, you will have necessity to process the Word document/ Excel workbook based on events)

Imports Word = Microsoft.Office.Interop.Word

Public Class UserControl1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Word.Selection.Text = ""

End Sub

End Class

The above code would cause Reference to a non-shared member required an object reference error. Instead declare a Shared Variable in the Addin class and use the same from usercontrol

Public Class ThisAddIn

Public Shared oWA As Word.Application

..

End Class

And in the UserControl reference it with the object name

Public Class UserControl1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

ThisAddIn.oWA.ActiveDocument.Range.Text = "Sample"

End Sub

End Class

You have referenced a non-shared member within your code and failed to supply an object reference. You cannot use the class name itself to qualify a member that is not shared. The instance must first be declared as an object variable and then referenced by the variable name.

Error ID: BC30469

To correct this error

1. Declare the instance as an object variable.

2. Reference the instance by the variable name.

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment

kbAlertz.com :: Visual Studio 2005

kbAlertz.com :: Visual Studio 2008

kbAlertz.com :: Visual Basic 2005