hosting VSTO document in a WPF control
-
22 iulie 2012 07:41
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.:)
- Mutat de Jason Dot WangMicrosoft Contingent Staff 25 iulie 2012 08:07 (From:Visual C# Language)
- Îmbinat de Cindy Meister MVPMVP, Moderator 25 iulie 2012 09:07 already answered duplicate question in VSTO forum
- Scindat de Cindy Meister MVPMVP, Moderator 25 iulie 2012 15:38 incorrectly merged
Toate mesajele
-
23 iulie 2012 04:04
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
-
23 iulie 2012 15:10
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)
{
}
}
}
-
25 iulie 2012 04:05
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.
- Îmbinat de Cindy Meister MVPMVP, Moderator 25 iulie 2012 15:40 same question
-
25 iulie 2012 06:57
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
-
25 iulie 2012 08:49Moderator
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
- Marcat ca răspuns de Nganou 31 iulie 2012 02:26
-
25 iulie 2012 14:48
Thank you Jason,
I'm patiently waiting.
-
31 iulie 2012 02:27OK Cindy, What are my options.