Search code examples
c#silverlightms-wordoffice-interopsavefiledialog

Can I use the Microsoft.Office.Interop.Word .dll in Silverlight to manipulate the SaveDialogFile of MSWord?


I have problem in making silverlight application, I need to create a Word Document using silverlight and the Save it directly to the database using the Default Save button in MSWord, but then I cannot use the "Microsoft.Office.Interop.Word .dll" in silverlight to manipulate the SaveFileDialog so that I can set the Default path when saving..

Another question is, can I hide or set the MSWord SaveFileDialog = false in Silverlight using Microsoft.Office.Interop.Word .dll? because my other plan is that to create a custom savefiledialog box in Silverlight and not to use the MSWord SaveFileDialog box..?

I use the Silverlight 5 Beta, is there any compatibility issue in using other version of MS Office.?

 public partial class MainPage : UserControl
 {
     dynamic objWord;
     dynamic document;
     dynamic range;
     static bool saveDoc = false;

     public MainPage()
     {
         InitializeComponent();
         objWord = AutomationFactory.CreateObject("Word.Application"); 
         AutomationEvent saveEvent = AutomationFactory.GetEvent(objWord, "DocumentBeforeSave");
         saveEvent.EventRaised += (s, args) =>
         {
             saveDoc = true;

             if (saveDoc == true)
             {
                 SaveFileDialog dlg = new SaveFileDialog();
                 dlg.DefaultExt = ".doc"; // Default file extension
                 dlg.Filter = "Word documents (.doc)|*.doc"; // Filter files by extension
                 Nullable<bool> result = dlg.ShowDialog();

                 if (result == true)
                 {
                     string filename = dlg.SafeFileName;
                     FileInfo aD = new FileInfo(filename);
                     string pathDoc = aD.DirectoryName.ToString();
                     MessageBox.Show(pathDoc); //trying to get the path so that i can flush it to memory stream
                 }
             }
         };
     }

     private void Button_Click(object sender, RoutedEventArgs e)
     {
         if (AutomationFactory.IsAvailable)
         {
             try
             {
                 document = objWord.Documents.Add();
                 object startIndex = 0;
                 range = document.Range(ref startIndex);
                 objWord.Visible = true;
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }
         }
     }
}

Thanks in Advance :) God Bless


Solution

  • You can access Word/Excel/Outlook if your Silverlight App. is in FullTrust and OutOfBrowser.

    Here you have a good example with Excel