2016-06-12 83 views
0

我正在尋找如何使用已將數據存儲到SQL CE數據庫的c#應用程序中的數據填充單詞.doc文件。複製文本文件並用輸出文本填充副本

所有approuches我發現「到現在,都是使用源.doc文件進行搜索,並用值替換變量,並將其保存在原來的,這似乎是偉大的,但我需要:

1 °使用savefiledialog將源.doc文件複製到用戶需要的位置。

2°然後用我想要的數據完成複製,然後保存。

因爲.doc文件稍後將成爲很多配置文件的模型,所以我無法編輯原始文件,用戶必須選擇他想要保存的文件的位置。

或可能:

1°編輯.DOC模型,然後savefiledialog(不改變模型的.doc)

編輯:可以解決它,任何有興趣的未來:

private void CreateWordDocument(object fileName, 
      object saveAs) 
     { 
      //Set Missing Value parameter - used to represent 
      // a missing value when calling methods through 
      // interop. 
      object missing = System.Reflection.Missing.Value; 

      //Setup the Word.Application class. 
      Word.Application wordApp = 
       new Word.Application(); 

      //Setup our Word.Document class we'll use. 
      Word.Document aDoc = null; 

      // Check to see that file exists 
      if (File.Exists((string)fileName)) 
      { 
       DateTime today = DateTime.Now; 

       object readOnly = false; 
       object isVisible = false; 

       //Set Word to be not visible. 
       wordApp.Visible = false; 

       //Open the word document 
       aDoc = wordApp.Documents.Open(ref fileName, ref missing, 
        ref readOnly, ref missing, ref missing, ref missing, 
        ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref isVisible, ref missing, ref missing, 
        ref missing, ref missing); 

       // Activate the document 
       aDoc.Activate(); 

       // Find Place Holders and Replace them with Values. 
       this.FindAndReplace(wordApp, "$$name$$", "Zach Smith"); 


      } 
      else 
      { 
       MessageBox.Show("Arquivo faltando."); 
       return; 
      } 


      SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
      saveFileDialog1.Filter = "Documento do Word|*.doc"; 
      saveFileDialog1.Title = "Salvar"; 
      saveFileDialog1.FileName = "Ficha Reg"; 
      if (DialogResult.OK == saveFileDialog1.ShowDialog()) 
      { 
       string docName = saveFileDialog1.FileName; 
       if (docName.Length > 0) 
       { 


        saveAs = (object)docName; 
        //Save the document as the correct file name. 
        aDoc.SaveAs(ref saveAs, ref missing, ref missing, ref missing, 
          ref missing, ref missing, ref missing, ref missing, 
          ref missing, ref missing, ref missing, ref missing, 
          ref missing, ref missing, ref missing, ref missing); 





       } 
      } 


      //Close the document - you have to do this. 
      aDoc.Close(ref missing, ref missing, ref missing); 

      MessageBox.Show("File created."); 
     } 

     /// <summary> 
     /// This is simply a helper method to find/replace 
     /// text. 
     /// </summary> 
     /// <param name="WordApp">Word Application to use</param> 
     /// <param name="findText">Text to find</param> 
     /// <param name="replaceWithText">Replacement text</param> 
     private void FindAndReplace(Word.Application WordApp, 
            object findText, 
            object replaceWithText) 
     { 
      object matchCase = true; 
      object matchWholeWord = true; 
      object matchWildCards = false; 
      object matchSoundsLike = false; 
      object nmatchAllWordForms = false; 
      object forward = true; 
      object format = false; 
      object matchKashida = false; 
      object matchDiacritics = false; 
      object matchAlefHamza = false; 
      object matchControl = false; 
      object read_only = false; 
      object visible = true; 
      object replace = 2; 
      object wrap = 1; 

      WordApp.Selection.Find.Execute(ref findText, 
       ref matchCase, ref matchWholeWord, 
       ref matchWildCards, ref matchSoundsLike, 
       ref nmatchAllWordForms, ref forward, 
       ref wrap, ref format, ref replaceWithText, 
       ref replace, ref matchKashida, 
       ref matchDiacritics, ref matchAlefHamza, 
       ref matchControl); 
     } 


     private void button1_Click(object sender, EventArgs e) 
     { 


      CreateWordDocument(@"C:\Users\Blind\Desktop\FICHA.doc", ""); 
     } 

回答

0

解決此問題的正確方法是將* .doc文件另存爲模板(* .dot),然後使用Documents.Add方法從模板創建副本。