Word VSTO and label
- Can i add a label in a VSTO Word document?I've tried and i get an error ( Element not found).
Answers
Hi Mihai
It appears Word/VSTO doesn't support inserting a WinForms control into the Header/Footer region of a Word document. I've tried every way I can think of, including trying to use a Pane in the Normal view. But I always get the error. It's also not allowed working in the VSTO Designer.
Why do you want to insert a Label control into the document's header? Perhaps there's an alternate approach you can use, such as inserting a graphics object...
All Replies
Could you please provide more information
1. The Version of VSTO (2005 or 2008)
2. The version of Word (2003 or 2007)
3. What kind of label (one would assume a WinForms label, but it's always dangerous to assume)
4. The code that's causing the error (and any supporting code to help us understand what's going on)
- I'm using VSTO 2005 for developing a Word 2003 document project.
Code BlockWord.Range ft = this.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
Microsoft.Office.Tools.Word.Controls.Label lbl = this.Controls.AddLabel(ft, 100, 100, "label1");
and i get the error:
Code BlockElement not found. (Exception from HRESULT: 0x80070490)
************** Exception Text **************
System.Runtime.InteropServices.COMException (0x80070490): Element not found. (Exception from HRESULT: 0x80070490)
at Microsoft.VisualStudio.Tools.Applications.Runtime.IHostItemProvider.GetHostExtender(String extenderType, String extenderCookie, Object& extenderObject)
at Microsoft.Office.Tools.Word.ControlCollection.InternalInlineAdd[T](Range range, Single width, Single height, String name)
at Microsoft.Office.Tools.Word.ControlCollection.AddLabel(Range range, Single width, Single height, String name)
at CustomDocument.ThisDocument.ThisDocument_Startup(Object sender, EventArgs e) in C:\Documents and Settings\user\My Documents\Visual Studio 2005\Projects\CustomDocument\CustomDocument\ThisDocument.cs:line 465
at Microsoft.Office.Tools.Word.Document.OnStartup()
at CustomDocument.ThisDocument.FinishInitialization() in C:\Documents and Settings\user\My Documents\Visual Studio 2005\Projects\CustomDocument\CustomDocument\ThisDocument.Designer.cs:line 66
at Microsoft.VisualStudio.Tools.Applications.Runtime.AppDomainManagerInternal.ExecutePhase(String methodName)
at Microsoft.VisualStudio.Tools.Applications.Runtime.AppDomainManagerInternal.ExecuteEntryPointsHelper()
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
Microsoft.VisualStudio.Tools.Applications.Runtime
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.816
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Runtime/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Runtime.dll
----------------------------------------
Microsoft.Office.Tools.Common
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.816
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.Office.Tools.Common/8.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Common.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
CustomDocument
Assembly Version: 1.4.0.0
Win32 Version: 1.4.0.0
CodeBase: file:///C:/Documents%20and%20Settings/user/My%20Documents/Visual%20Studio%202005/Projects/CustomDocument/CustomDocument/bin/Release/CustomDocument.DLL
----------------------------------------
Microsoft.Office.Tools.Word
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.816
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.Office.Tools.Word/8.0.0.0__b03f5f7f11d50a3a/Microsoft.Office.Tools.Word.dll
----------------------------------------
Microsoft.Office.Interop.Word
Assembly Version: 11.0.0.0
Win32 Version: 11.0.5530
CodeBase: file:///C:/WINDOWS/assembly/GAC/Microsoft.Office.Interop.Word/11.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Word.dll
----------------------------------------
office
Assembly Version: 11.0.0.0
Win32 Version: 11.0.5530
CodeBase: file:///C:/WINDOWS/assembly/GAC/office/11.0.0.0__71e9bce111e9429c/office.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Data
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
Accessibility
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Accessibility/2.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Transactions
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
Microsoft.VisualStudio.OfficeTools.Controls.ManagedWrapper
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.42
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualStudio.OfficeTools.Controls.ManagedWrapper/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.OfficeTools.Controls.ManagedWrapper.dll
---------------------------------------- Hi Mihai
It appears Word/VSTO doesn't support inserting a WinForms control into the Header/Footer region of a Word document. I've tried every way I can think of, including trying to use a Pane in the Normal view. But I always get the error. It's also not allowed working in the VSTO Designer.
Why do you want to insert a Label control into the document's header? Perhaps there's an alternate approach you can use, such as inserting a graphics object...
Hi Mihai,
I agree with Cindy, VSTO does not support to insert winform control into these three places:
-
header
-
footer
-
subdocument
You can get official document about this in the following link: (The Note in section of Adding Controls at Design Time)
http://msdn2.microsoft.com/en-us/library/0x6hstz2(VS.80).aspx
I am marking Cindy's post as answer, for I also think the right direction you need to go is use text or inlineshap instead of winform label control.
Thanks
Ji
-

