Word.Document 内容 ->编辑文本
我目前在尝试编辑 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑
我对转义 \r 的做法是错误的,因为您当然要替换实际的控制字符 (\r),而不是文字“\r”。对不起。
您的评论是正确的,当您执行
tempDoc.Content.Text = st;
时,会附加新的换行符 (\r)。在实际使用该值时,您始终需要删除或更换。看起来您想将文本复制到剪贴板?如果是这样,为什么不直接从字符串 (
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
):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)
尝试:
旁注:
\n
在 Unix 中用作换行符。\r
在 Mac 中用作换行符。\n\r
在 Windows 中用作换行符。Try:
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.