C#如何自定义word文档的文字颜色

xjj493663804 2009-04-14 03:33:12
C#如何自定义word文档的文字颜色,如要定义word文档中文字颜色的值为204,代码该如何写
...全文
191 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
BitCoffee 2009-09-09
  • 打赏
  • 举报
回复

//参考,下面的是将粗体改为正常的字体
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
object fileName = System.IO.Directory.GetCurrentDirectory() + "\\test.doc";
oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

for (int i = 0; i < oDoc.Content.Text.Length; i++)
{
try
{
if (oDoc.Content.Text.ToString().Trim().Substring(i, 1) != " ")
{
oWord.Selection.MoveRight(ref oMissing, ref oMissing, ref oMissing);
if (oWord.Selection.Font.Bold == -1)
{
this.richTextBox1.Text += oDoc.Content.Text.ToString().Substring(i, 1) + "\n\r";
//这里可以改颜色的.
oWord.Selection.Font.Bold = 0;
}
}
}
catch
{

}
}

if (oDoc != null)
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
if (oWord != null)
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
cpeng123 2009-09-09
  • 打赏
  • 举报
回复
c#中现在不用font了,在你写的文档的框体那,<p style="宋体" color="#ccc" >你的文档</p>
崔鹏飞 2009-09-09
  • 打赏
  • 举报
回复
选中部分.Font.Color=需要的颜色
CeShenBeiWang 2009-04-14
  • 打赏
  • 举报
回复
up

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧