2016-06-12 153 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方法从模板创建副本。