hosting VSTO document in a WPF control
-
Sunday, July 22, 2012 7:41 AM
Hi, In the project in vs2010. My solution has a word document, which i have added a new item, wpf user control to view that document. every this i run this command:
wordDocument1.Globals.ThisDocument.Path or Just wordDocument1.Globals.thisDocument
it gives me a nullReferenceArguement error, saying wordDocument1.Globals.thisDocument has not be initialize or it null.
i will really appreciate some help.:)
- Moved by Jason Dot WangMicrosoft Contingent Staff Wednesday, July 25, 2012 8:07 AM (From:Visual C# Language)
- Merged by Cindy Meister MVPMVP, Moderator Wednesday, July 25, 2012 9:07 AM already answered duplicate question in VSTO forum
- Split by Cindy Meister MVPMVP, Moderator Wednesday, July 25, 2012 3:38 PM incorrectly merged
All Replies
-
Monday, July 23, 2012 4:04 AM
Hi,
To avoid this error check out :
avoiding null reference exceptions
I think that you have not added a reference for this namespace, "Microsoft.Office.Interop.Word." Add a reference of this in your project, then use documents.open method!
I hope this post was helpful to you!
Thanks
-
Monday, July 23, 2012 3:10 PM
Thanks for the reply.
and sorry i probably did not make myself clear. The Document is created in vs, and the reference "Microsoft.Office.Interop.Word." has been added and called. Under my document class, I have added a new class, wpf user control. I would like to display that document inside a documentviewer in my usercontrol. The call worddocument.global.thisdocument give null reference because the factory and the serviceprovider are null when i create a new instance of that document in the usercontrol class.
Sample code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Office.Interop.Word;
using System.Windows.Xps.Packaging;
namespace WordDocument1
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public Microsoft.Office.Tools.Word.Factory factory { get; set; }
public IServiceProvider serviceProvider { get; set; }
public UserControl1()
{
InitializeComponent();
}
private XpsDocument convertToxps()
{
Microsoft.Office.Interop.Word.Document doc = new Document();
doc = WordDocument1.Globals.ThisDocument.Application.ActiveDocument;
string xpsdocname = String.Concat(System.IO.Path.GetDirectoryName(WordDocument1.Globals.ThisDocument.Path), "\\",
System.IO.Path.GetFileNameWithoutExtension(WordDocument1.Globals.ThisDocument.Path), ".xps");
doc.SaveAs(xpsdocname, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXPS);
XpsDocument xpsDoc = new XpsDocument(xpsdocname, System.IO.FileAccess.Read);
return xpsDoc;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
WordDocument1.Globals.ThisDocument.Open();
}
private void documentViewer1_PageViewsChanged(object sender, EventArgs e)
{
}
}
}
-
Wednesday, July 25, 2012 4:05 AM
OK, I am trying to view a document created in vs2010 in a wpf usercontrol document viewer. The ThisDocument class and the UserControl class are both object of the same project Worddocument1.docx.
outside of ThisDocument class, i know i have to use Globals.ThisDocument to get the word document or at least to get the path using Globals.ThisDocument.FullName or FullPath. so that i can convert it to a xps document and view it in my documentviewer usercontrol1.
Here is my problem.
In the Usercontrol1.cs, will try to initialize ThisDocument using the command:
WordDocument1.Globlas.ThisDocument = new ThisDocument(Factory, ServiceProvider);
// This will give me a nullReferenceArguement. In details: Its ThisDocument = null; Factory = null; ServiceProvider = null;
Any Help will be highly appropriated.
- Merged by Cindy Meister MVPMVP, Moderator Wednesday, July 25, 2012 3:40 PM same question
-
Wednesday, July 25, 2012 6:57 AM
Hi Nganou,
Welcome to MSDN Forum Support.
We're doing research on this issue. It might take some time before we get back to you.
Sincerely,
Jason Wang
Jason Wang [MSFT]
MSDN Community Support | Feedback to us
-
Wednesday, July 25, 2012 8:49 AMModerator
If I understand you correctly - "trying to view a document...in a wpf usercontrol document viewer" - what you're trying to do is not possible. VSTO customizations cannot run in an embedded environment (in any kind of control). They only work in a standalone Word (or Excel) application.Cindy Meister, VSTO/Word MVP
- Marked As Answer by Nganou Tuesday, July 31, 2012 2:26 AM
-
Wednesday, July 25, 2012 2:48 PM
Thank you Jason,
I'm patiently waiting.
-
Tuesday, July 31, 2012 2:27 AMOK Cindy, What are my options.

