Word.Document 内容 ->编辑文本

发布于 2024-11-26 09:23:22 字数 772 浏览 0 评论 0原文

我目前在尝试编辑 Document.Contents.Text 中的文本时遇到问题

以下代码不起作用:

object findingFile = m_TempFileDirectory.FullName + "\\" + formField.Name + ".rtf";
Document tempDoc = wordApp.Documents.Open(ref findingFile, ref m_Missing, 
  ref m_Missing, ref m_Missing, ref m_Missing, ref m_Missing, ref m_Missing, 
  ref m_Missing, ref m_Missing, ref m_Missing, ref m_Missing, ref m_Missing, 
  ref m_Missing, ref m_Missing, ref m_Missing, ref m_Missing);
tempDoc.Content.Select();
String st = tempDoc.Content.Text;
st = st.Replace("\r", "");
tempDoc.Content.Text = st;
tempDoc.Content.Copy();
tempDoc.Close(ref m_DiscardChanges, ref m_Missing, ref m_Missing);
r.Paste();

我的结果仍然是(我正在尝试删除新行的 \r)

Lipid level: not specified\r

I'm currently have an issue trying to edit the Text inside my Document.Contents.Text

The following code does not work:

object findingFile = m_TempFileDirectory.FullName + "\\" + formField.Name + ".rtf";
Document tempDoc = wordApp.Documents.Open(ref findingFile, ref m_Missing, 
  ref m_Missing, ref m_Missing, ref m_Missing, ref m_Missing, ref m_Missing, 
  ref m_Missing, ref m_Missing, ref m_Missing, ref m_Missing, ref m_Missing, 
  ref m_Missing, ref m_Missing, ref m_Missing, ref m_Missing);
tempDoc.Content.Select();
String st = tempDoc.Content.Text;
st = st.Replace("\r", "");
tempDoc.Content.Text = st;
tempDoc.Content.Copy();
tempDoc.Close(ref m_DiscardChanges, ref m_Missing, ref m_Missing);
r.Paste();

My outcome is still (I'm trying to remove the \r for New Line)

Lipid level: not specified\r

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

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

发布评论

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

评论(2

二智少女猫性小仙女 2024-12-03 09:23:22

编辑

我对转义 \r 的做法是错误的,因为您当然要替换实际的控制字符 (\r),而不是文字“\r”。对不起。

您的评论是正确的,当您执行 tempDoc.Content.Text = st; 时,会附加新的换行符 (\r)。在实际使用该值时,您始终需要删除或更换。

看起来您想将文本复制到剪贴板?如果是这样,为什么不直接从字符串 (st) 中执行:

        String st = tempDoc.Content.Text.Replace("\r", "\n");
        System.Windows.Forms.Clipboard.SetText(st);

我还用回车符 (\n) 替换了换行符,因为如果不这样做,则不会有换行符文本(如果有多行文本就会出现问题)。

(原答案已删除)

EDIT

I was wrong about escaping the \r, because of course you are replacing the actual control character (\r), not the literal "\r"'. Sorry.

You are correct in your comment, when you do tempDoc.Content.Text = st; a new linefeed (\r) gets appended. You will always need to remove or replace at the time that you actually use the value.

It looks like you want to copy the text to the clipboard? If so, why not just do it from the string (st):

        String st = tempDoc.Content.Text.Replace("\r", "\n");
        System.Windows.Forms.Clipboard.SetText(st);

I also replaced the linefeeds with carriage-returns (\n), because if you don't do that there will be no linefeeds in the text at all (a problem if you have multiple lines of text).

(Original answer deleted)

初见 2024-12-03 09:23:22

尝试:

st = st.Replace("\n", "").Replace("\r", "").Replace("\n\r", "");

旁注:

\n 在 Unix 中用作换行符。

\r 在 Mac 中用作换行符。

\n\r 在 Windows 中用作换行符。

Try:

st = st.Replace("\n", "").Replace("\r", "").Replace("\n\r", "");

Side Note:

\n Used as a new line character in Unix.

\r Used as a new line character in Mac.

\n\r Used as a new line character in Windows.

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