2013-01-12 73 views
0

我創建了Office Add-In項目,併爲應用程序添加了功能區菜單。當我建立我的項目Word文檔有我的絲帶沒有問題。Word插件功能區

如何使用StreamReader在使用下面的按鈕點擊事件單擊功能區菜單中的按鈕時將活動文檔另存爲文件?

private void btnsavefile_Click(object sender, RibbonControlEventArgs e) 
{ 
    //Getting FileStream here. 

} 
+0

什麼FILESTREAM ? System.IO.StreamReader可以正常工作Office Addins –

+0

@John Koerner如何使用Ribbon菜單中的StreamReader讀取活動文檔? – user1624185

+0

你想要保存什麼?電子郵件的正文?整個電子郵件使用相當於文件保存爲.msg擴展名? – Magnum

回答

0

我在堆棧溢出中找到了以下解決方案。希望它與你有關。

Serialize current ActiveDocument from office 2007 add-in

個人而言,當我處理這種情況下我也做了相同的。我已將文件副本保存到臨時位置,並將副本推送到服務器。在這種情況下,活動文檔將保持原樣。

Excel.Workbook xlb = Globals.ThisAddIn.Application.ActiveWorkbook; 
xlb.SaveCopyAs(filePath); 

希望這會有所幫助!

0

創建Word Addin項目 - >添加Ribbon添加新項目的可視化設計器。

添加菜單功能區設計和ribbonsample.cs下面寫代碼

public partial class RibbonSample 
{ 
    private void RibbonSample_Load(object sender, RibbonUIEventArgs e) 
    { 
    // Initialise log4net 
    } 
    //Adding items in menu from DB 
    public RibbonSample() 
     : base(Globals.Factory.GetRibbonFactory()) 
    { 
     InitializeComponent(); 
     try 
     { 
      System.Data.DataTable dt = new DataAcces().GetData(); 
      if (dt.Rows.Count > 0) 
      { 
       for (int i = 0; i < dt.Rows.Count; i++) 
       { 
        RibbonButton Field = this.Factory.CreateRibbonButton(); 
        Field.Label = dt.Rows[i][1].ToString(); 
        Field.Tag = i; 
        Field.ControlSize = 
         Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge; 
        Field.Click += Field_Click; 
        menu1.Items.Add(Field); 
       } 
      } 
      else 
      { 
       System.Windows.Forms.MessageBox.Show("No Fields are available in database"); 
      } 
     } 
     catch (Exception exception) 
     { 
      //thrw exception 
     } 
    } 

//Select menu item text in word 
void Field_Click(object sender, RibbonControlEventArgs e) 
{ 
    try 
    { 
     Microsoft.Office.Interop.Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range; 
     currentRange.Text = (sender as RibbonButton).Label; 
    } 
    catch (Exception exception) 
    { 
     log.Error(friendlyErrorMessage + " Field_Click Details:" + exception.Message, exception); 
    } 
    } 
} 
0

無效Application_DocumentBeforeClose(Word.Document文件,REF布爾取消) { 嘗試 {

 string filePath = this.Application.ActiveDocument.FullName.ToString(); 
     string fileName = this.Application.ActiveDocument.Name; 

     //dialogFilePath = filePath; 
     dialogFileName = fileName; 


     string tempFile; 
     string tempPath; 


     if (true) 
     { 

      var confirmResult = System.Windows.Forms.MessageBox.Show("Are you sure to save this document ??", 
        "Confirm Save!!", 
        System.Windows.Forms.MessageBoxButtons.YesNo); 
      if (confirmResult == System.Windows.Forms.DialogResult.Yes) 
      { 
       //document.Save(); 
       var iPersistFile = (IPersistFile)document; 
       iPersistFile.Save(tempPath, false); 

       //Do some action here 
      } 

      Word._Document wDocument = Application.Documents[fileName] as Word._Document; 
      //wDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges); 
      ThisAddIn.doc.Close(Word.WdSaveOptions.wdDoNotSaveChanges); 
     } 

    } 
    catch (Exception exception) 
    { 

    } 

}