如何在RT#中将文本从RTF传输到Word文档

时间:2009-12-19 16:07:58

标签: ms-word

h我需要将文本从richTextbox传输到word文档,并在button_click上以纯文本形式传输。我需要帮助。

我有一个vb代码但需要csharp。

Dim wrdApp As Word.Application

Private Sub Form_Load()
    Set wrdApp = New Word.Application
End Sub

Private Sub Command2_Click()
    Clipboard.SetText RichTextBox1.TextRTF, vbCFRTF
    With wrdApp
        .Documents.Add
        .Selection.Paste
        .ActiveDocument.SaveAs App.Path & "RTFDOC2.doc", _
                wdFormatDocument
        .Visible = True
        .Activate
    End With
End Sub 

2 个答案:

答案 0 :(得分:2)

我找到并将以下内容放在一起this site

object missing = System.Reflection.Missing.Value;
object Visible = true;
object start1 = 0;
object end1 = 0;
Word.Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
Word.Range rng = adoc.Range(ref start1, ref missing);

try
{              
    Clipboard.SetText(richTextBox.Text, TextDataFormat.Rtf);
    WordApp.Selection.Paste();
    string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    object filename = path + @"\MyWord.doc";

    adoc.SaveAs(ref filename, 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);

    WordApp.Visible = true;
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

答案 1 :(得分:0)

您可以使用msword的activeX-IF调用此VBA代码。