我可以使用Silverlight中的Microsoft.Office.Interop.Word .dll来操作MSWord的SaveDialogFile吗?

时间:2011-11-18 08:39:27

标签: c# silverlight ms-word office-interop savefiledialog

我在制作Silverlight应用程序时遇到问题,我需要使用silverlight 创建一个Word文档,并使用MSWord中的默认保存按钮将其直接保存到数据库中,但是我不能使用silverlight中的“Microsoft.Office.Interop.Word .dll”来操作SaveFileDialog,以便我可以在保存时设置默认路径。

另一个问题是,我可以使用Microsoft.Office.Interop.Word .dll吗?在Silverlight中隐藏或设置MSWord SaveFileDialog = false,因为我的另一个计划是创建一个自定义的savefiledialog框Silverlight并没有使用MSWord SaveFileDialog框..?

我使用Silverlight 5 Beta,使用其他版本的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);
             }
         }
     }
}

先谢谢你:)上帝保佑

1 个答案:

答案 0 :(得分:1)

如果您的Silverlight应用程序可以访问Word / Excel / Outlook。在FullTrust和OutOfBrowser中。

Here you have a good example with Excel