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

发布于 2024-08-15 08:33:30 字数 534 浏览 3 评论 0原文

我需要将文本从 richTextbox 传输到 Word 文档,并且还需要在单击按钮时以纯文本形式传输。我需要这方面的帮助。

我有一个 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 

hI I need to transfer text from richTextbox to word document and also in plain text on button_click. I need assistance on this please.

I got a vb code but need in 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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守不住的情 2024-08-22 08:33:30

我根据 此网站< /a>:

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);
}

I found and put the following together based off 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);
}
我不是你的备胎 2024-08-22 08:33:30

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

You can call this VBA code using the activeX-IF of msword.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文