Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
965 views
in Technique[技术] by (71.8m points)

c# - How to convert System.Drawing.Color to Microsoft Interop WdColorIndex

How can I convert System.Drawing.Color to Microsoft.Office.Interop.Word.WdColorIndex?

I have done the code so far, but it's showing the error "overflow".

Here is the code which I have done

Color bgcolor = Color.FromArgb(Convert.ToInt32(innerText));
Microsoft.Office.Interop.Word.WdColorIndex wbgc = (Microsoft.Office.Interop.Word.WdColorIndex)(bgcolor.R + 0x100 * bgcolor.G + 0x10000 * bgcolor.B);
doc.Range(iRangeStart, iRangeEnd).HighlightColorIndex = wbgc;

How can I achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

WdColorIndex is an enumeration, not an object that defines a color system. This means that the value you can assign is limited by the enumeration elements, e.g. wdBlack or wdBlue and their underlying integer values.

The technique you are using is to be applied to a WdColor object instead of a WdColorIndex enumeration:

var wordColor = (Microsoft.Office.Interop.Word.WdColor)(bgcolor.R + 0x100 * bgcolor.G + 0x10000 * bgcolor.B);

Highlighting in a Word document is limited to a number of colors, as defined in the WdColorIndex enumeration. Therefore, you cannot simply convert any color to a Word color for highlighting. You have to pick one of the available values. See MSDN for WdColorIndex for possible values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...