When I insert a WinForm control in Word Document, I get this exception.

Hi All,
Sometimes, when I insert a general Custom WinForm UserControl in Word Document, I receive this exception. It occurred as accidental. I traced this issue. And debug it. It occurred in this code snippet:
var cs = vstoDoc.Controls.AddControl(CustomControl, range, width, height, controlName);
I checked CustomControl,range,width,height,controlName, vstoDoc, vstoDoc.Controls, all of them are not null object. I am confused.
The Exception:
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.Control.ActiveXImpl.InPlaceActivate(Int32 verb)
   at System.Windows.Forms.Control.ActiveXImpl.OnFocus(Boolean focus)
   at System.Windows.Forms.Control.OnGotFocus(EventArgs e)
   at System.Windows.Forms.Control.WmSetFocus(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WmSetFocus(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.UserControl.WmSetFocus(Message& m)
   at System.Windows.Forms.UserControl.WndProc(Message& m)
   at Microsoft.VisualStudio.OfficeTools.Controls.VSTOContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ActiveXImpl.System.Windows.Forms.IWindowTarget.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
Microsoft.VisualStudio.Tools.Office.Runtime
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.50903.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Office.Runtime/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Office.Runtime.dll
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34238 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
System.Security
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Security/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Security.dll
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34234 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
Microsoft.VisualStudio.Tools.Applications.Hosting
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.50903.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Hosting/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Hosting.dll
Microsoft.VisualStudio.Tools.Applications.Runtime
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.50903.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Runtime/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Runtime.dll
System.Deployment
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34244 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Deployment/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Deployment.dll
Microsoft.VisualStudio.Tools.Applications.ServerDocument
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.50903.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.ServerDocument/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
System.Xml.Linq
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml.Linq/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.Linq.dll
Microsoft.Office.Tools
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.50903.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.dll
Microsoft.Office.Tools.Word.Implementation
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.50903.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Word.Implementation/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Word.Implementation.dll
Microsoft.Office.Tools.Common.Implementation
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.50903.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Common.Implementation/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Common.Implementation.dll
Microsoft.Office.Tools.Common
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.50903.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Common/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Common.dll
Microsoft.Office.Tools.Word
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.50903.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.Word/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Word.dll
FClassroom.Addin.Presentation
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///D:/Projects/Development/EduDiagnosis-net/FClassroom.Presentation/bin/Debug/FClassroom.Addin.Presentation.DLL
Microsoft.Office.Tools.Common.v4.0.Utilities
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.30319.1
    CodeBase: file:///D:/Projects/Development/EduDiagnosis-net/FClassroom.Presentation/bin/Debug/Microsoft.Office.Tools.Common.v4.0.Utilities.DLL
Microsoft.Office.Interop.Word
    Assembly Version: 12.0.0.0
    Win32 Version: 12.0.6612.1000
    CodeBase: file:///C:/Windows/assembly/GAC/Microsoft.Office.Interop.Word/12.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Word.dll
office
    Assembly Version: 12.0.0.0
    Win32 Version: 12.0.4518.1014
    CodeBase: file:///C:/Windows/assembly/GAC/office/12.0.0.0__71e9bce111e9429c/office.dll
FBD
    Assembly Version: 1.13.10.780
    Win32 Version: 1.13.10.780
    CodeBase: file:///D:/Projects/Development/EduDiagnosis-net/FClassroom.Presentation/bin/Debug/FBD.DLL
FAC
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///D:/Projects/Development/EduDiagnosis-net/FClassroom.Presentation/bin/Debug/FAC.DLL
FFK
    Assembly Version: 1.15.9.975
    Win32 Version: 1.15.9.975
    CodeBase: file:///D:/Projects/Development/EduDiagnosis-net/FClassroom.Presentation/bin/Debug/FFK.DLL
FRP
    Assembly Version: 1.14.3.518
    Win32 Version: 1.14.3.518
    CodeBase: file:///D:/Projects/Development/EduDiagnosis-net/FClassroom.Presentation/bin/Debug/FRP.DLL
System.Data.Entity
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Data.Entity/v4.0_4.0.0.0__b77a5c561934e089/System.Data.Entity.dll
System.Data
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
PresentationFramework
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.dll
WindowsBase
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/WindowsBase/v4.0_4.0.0.0__31bf3856ad364e35/WindowsBase.dll
PresentationCore
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/PresentationCore/v4.0_4.0.0.0__31bf3856ad364e35/PresentationCore.dll
System.Xaml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xaml/v4.0_4.0.0.0__b77a5c561934e089/System.Xaml.dll
FSV
    Assembly Version: 1.15.9.236
    Win32 Version: 1.15.9.236
    CodeBase: file:///D:/Projects/Development/EduDiagnosis-net/FClassroom.Presentation/bin/Debug/FSV.DLL
WindowsFormsIntegration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/WindowsFormsIntegration/v4.0_4.0.0.0__31bf3856ad364e35/WindowsFormsIntegration.dll
PresentationFramework.Aero
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework.Aero/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.Aero.dll
PresentationFramework-SystemCore
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemCore/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemCore.dll
PresentationFramework-SystemXml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemXml/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemXml.dll
UIAutomationTypes
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationTypes/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationTypes.dll
log4net
    Assembly Version: 1.2.13.0
    Win32 Version: 1.2.13.0
    CodeBase: file:///D:/Projects/Development/EduDiagnosis-net/FClassroom.Presentation/bin/Debug/log4net.DLL
Accessibility
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
PresentationFramework-SystemXmlLinq
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemXmlLinq/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemXmlLinq.dll
System.Data.SQLite
    Assembly Version: 1.0.94.0
    Win32 Version: 1.0.94.0
    CodeBase: file:///D:/Projects/Development/EduDiagnosis-net/FClassroom.Presentation/bin/Debug/System.Data.SQLite.DLL
System.Transactions
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Transactions/v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll
System.Runtime.Serialization
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34234 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Serialization/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll
System.EnterpriseServices
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.EnterpriseServices/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
System.Numerics
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
System.Data.OracleClient
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data.OracleClient/v4.0_4.0.0.0__b77a5c561934e089/System.Data.OracleClient.dll
System.Web
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34237 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Web/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Web.dll
System.Data.SQLite.Linq
    Assembly Version: 1.0.94.0
    Win32 Version: 1.0.94.0
    CodeBase: file:///D:/Projects/Development/EduDiagnosis-net/FClassroom.Presentation/bin/Debug/System.Data.SQLite.Linq.DLL
Anonymously Hosted DynamicMethods Assembly
    Assembly Version: 0.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll
Microsoft.CSharp
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.CSharp/v4.0_4.0.0.0__b03f5f7f11d50a3a/Microsoft.CSharp.dll
System.Dynamic
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Dynamic.dll
PresentationFramework-SystemData
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemData/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemData.dll
Microsoft.VisualStudio.DebuggerVisualizers
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.30319.1
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.VisualStudio.DebuggerVisualizers/10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.DebuggerVisualizers.dll
Newtonsoft.Json
    Assembly Version: 6.0.0.0
    Win32 Version: 6.0.6.17820
    CodeBase: file:///D:/Projects/Development/EduDiagnosis-net/FClassroom.Presentation/bin/Debug/Newtonsoft.Json.DLL
CustomMarshalers
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/CustomMarshalers/v4.0_4.0.0.0__b03f5f7f11d50a3a/CustomMarshalers.dll
Microsoft.Office.Tools.v4.0.Framework
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.50903.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Office.Tools.v4.0.Framework/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.v4.0.Framework.dll
Microsoft.VisualStudio.Tools.Office.ContainerControl
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.50903.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Office.ContainerControl/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Office.ContainerControl.dll
System.Dynamic.DynamicAssembly
    Assembly Version: 0.0.0.0
    Win32 Version: 4.0.30319.34209
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Dynamic.dll
Thanks,

Hi Yang,
Base on my test in Office 2007, it works fine, this is my code below:
internal void AddUserControl()
UserControl1 u = new UserControl1();
var vstoDoc = this.Application.ActiveDocument.GetVstoObject();
vstoDoc.Controls.AddControl(u, this.Application.Selection.Range, 150, 150, "MyUserControl");
Could you reproduce that issue in other machine and new project?
Regards
Starain
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey.

Similar Messages

Maybe you are looking for

  • Playlist creating options are to limited.

    Why is there no playlist option, when making a playlist? Like to select music from other playlist? Or a way to add music to a certain playlist by clicking on the song? for IOS6 (ipad mini). Those are the only two ways i made playlist in the past... a

  • One of my computers crashed and I lost all my itunes library. how do I get this back?

    My daughter  accidentally downloaded a virus and my computer crashed hard along with the back up hard drive!  I was able to recover just a few item using all the data retrieval software but 90% of my stuff is gone or corrupted including all my itunes

  • How to report in bex

    Hi all, How to prepare reports using bex. please send to: [email protected]

  • How to resolve this Search Problem????

    Hi Every one I have a search module..I've never worked on... the problem is when i run that search module and after selecting date from date picker and clicking the search button i got following error: Error JBO-27122: SQL error during statement prep

  • Audition MIDI tracks and VST scanning not working - what to do?

    Hi - I'm running Audition 3.0.1 as an audio only application very successfully for some time on my Dell Inspiron 1525 (Vista Service Pack 1, all updates applied; 4GB RAM). However, I'm now trying to use it as a MIDI multitrack sequencer and am encoun