Microsoft Word 中的编号列表

发布于 2024-08-16 05:02:07 字数 99 浏览 3 评论 0原文

我正在使用 Interop.Microsoft.Office.Interop.Word.dll 在 C# 中动态构建 Word 文档。

有人有创建编号列表的代码示例吗?

I am using Interop.Microsoft.Office.Interop.Word.dll to dynamically build a Word document in C#.

Does anyone have a code example to create a a numbered list?

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

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

发布评论

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

评论(1

吻泪 2024-08-23 05:02:07

试试这个...它假设您有对 Word10 的引用(您可以使用其他版本,您必须更改常量)。不要忘记using Microsoft.Office.Interop.Word;

// setup
object missing = System.Reflection.Missing.Value;
ApplicationClass app = new ApplicationClass();
Document doc = app.Documents.Add(ref missing, ref missing, 
    ref missing, ref missing);
app.Visible = true;

// whatever is selected will be turned into a numbered list.
object n = 1;
ListTemplate template = 
    app.ListGalleries[WdListGalleryType.wdNumberGallery].ListTemplates.get_Item(ref n);
ListLevel level = template.ListLevels[1];
level.NumberFormat = "%1.";
level.TrailingCharacter = WdTrailingCharacter.wdTrailingTab;
level.NumberStyle = WdListNumberStyle.wdListNumberStyleArabic;
level.NumberPosition = app.InchesToPoints(0.25f);
level.Alignment = WdListLevelAlignment.wdListLevelAlignLeft;
level.TextPosition = app.InchesToPoints(0.5f);
level.TabPosition = (float)WdConstants.wdUndefined;
level.ResetOnHigher = 0;
level.StartAt = 1;

level.Font.Bold = (int)WdConstants.wdUndefined;
level.Font.Italic = (int)WdConstants.wdUndefined;
level.Font.StrikeThrough = (int)WdConstants.wdUndefined;
level.Font.Subscript = (int)WdConstants.wdUndefined;
level.Font.Superscript = (int)WdConstants.wdUndefined;
level.Font.Shadow = (int)WdConstants.wdUndefined;
level.Font.Outline = (int)WdConstants.wdUndefined;
level.Font.Emboss = (int)WdConstants.wdUndefined;
level.Font.Engrave = (int)WdConstants.wdUndefined;
level.Font.AllCaps = (int)WdConstants.wdUndefined;
level.Font.Hidden = (int)WdConstants.wdUndefined;
level.Font.Underline = WdUnderline.wdUnderlineNone;
level.Font.Color = WdColor.wdColorAutomatic;
level.Font.Size = (int)WdConstants.wdUndefined;
level.Font.Animation = WdAnimation.wdAnimationNone;
level.Font.DoubleStrikeThrough = (int)WdConstants.wdUndefined;

level.LinkedStyle = "";

template.Name = "";
object bContinuePrevList = false;
object applyTo = WdListApplyTo.wdListApplyToWholeList;
object defBehavior = WdDefaultListBehavior.wdWord10ListBehavior;

app.Selection.Range.ListFormat.ApplyListTemplateWithLevel(
    template, ref bContinuePrevList, 
    ref applyTo, ref defBehavior, ref missing);

编辑:格式设置。

Try this... it assumes you have a reference to Word10 (you can use other versions, you'll have to change the constants). Don't forget the using Microsoft.Office.Interop.Word;

// setup
object missing = System.Reflection.Missing.Value;
ApplicationClass app = new ApplicationClass();
Document doc = app.Documents.Add(ref missing, ref missing, 
    ref missing, ref missing);
app.Visible = true;

// whatever is selected will be turned into a numbered list.
object n = 1;
ListTemplate template = 
    app.ListGalleries[WdListGalleryType.wdNumberGallery].ListTemplates.get_Item(ref n);
ListLevel level = template.ListLevels[1];
level.NumberFormat = "%1.";
level.TrailingCharacter = WdTrailingCharacter.wdTrailingTab;
level.NumberStyle = WdListNumberStyle.wdListNumberStyleArabic;
level.NumberPosition = app.InchesToPoints(0.25f);
level.Alignment = WdListLevelAlignment.wdListLevelAlignLeft;
level.TextPosition = app.InchesToPoints(0.5f);
level.TabPosition = (float)WdConstants.wdUndefined;
level.ResetOnHigher = 0;
level.StartAt = 1;

level.Font.Bold = (int)WdConstants.wdUndefined;
level.Font.Italic = (int)WdConstants.wdUndefined;
level.Font.StrikeThrough = (int)WdConstants.wdUndefined;
level.Font.Subscript = (int)WdConstants.wdUndefined;
level.Font.Superscript = (int)WdConstants.wdUndefined;
level.Font.Shadow = (int)WdConstants.wdUndefined;
level.Font.Outline = (int)WdConstants.wdUndefined;
level.Font.Emboss = (int)WdConstants.wdUndefined;
level.Font.Engrave = (int)WdConstants.wdUndefined;
level.Font.AllCaps = (int)WdConstants.wdUndefined;
level.Font.Hidden = (int)WdConstants.wdUndefined;
level.Font.Underline = WdUnderline.wdUnderlineNone;
level.Font.Color = WdColor.wdColorAutomatic;
level.Font.Size = (int)WdConstants.wdUndefined;
level.Font.Animation = WdAnimation.wdAnimationNone;
level.Font.DoubleStrikeThrough = (int)WdConstants.wdUndefined;

level.LinkedStyle = "";

template.Name = "";
object bContinuePrevList = false;
object applyTo = WdListApplyTo.wdListApplyToWholeList;
object defBehavior = WdDefaultListBehavior.wdWord10ListBehavior;

app.Selection.Range.ListFormat.ApplyListTemplateWithLevel(
    template, ref bContinuePrevList, 
    ref applyTo, ref defBehavior, ref missing);

edit: formatting.

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