无法使用c#以编程方式在Word doc中设置行间距 [英] Unable to set line spacing in Word doc programatically with c#
问题描述
我是c#的新手。我正在尝试使用列表级别以编程方式创建Word文档。我对类,方法和命名空间有基本的了解。
我无法在下面的代码中获得任何格式化(行间距规则或标签缩进)。我试图在Word中的每一行之后删除8行并设置为0.
Microsoft 。 办公室 。 Interop 。 Word 。 段落 para1 = 文件 。 内容 。 段落 。 添加 ( ref 缺少 );
para1 。 范围 。 ParagraphFormat 。 LineSpacingRule = Microsoft 。 办公室 。 Interop 。 Word 。 WdLineSpacing 。 wdLineSpaceExactly ;
para1 。 范围 。 ParagraphFormat 。 SpaceAfter = 0.0f ;
para1 。 范围 。 段落 。 LineSpacingRule = Microsoft 。 办公室 。 Interop 。 Word 。 WdLineSpacing 。 wdLineSpaceExactly ;
para1 。 范围 。 段落 。 SpaceAfter = 0.0f ;
para1 。 范围 。 ParagraphFormat 。 TabHangingIndent ( 1 );
para1 。 范围 。 文字 = " 1.测试" + 环境 。 NewLine + " a.Line1" + 环境 。 NewLine + " b.Line2" ;
para1 。 范围 。 InsertParagraphAfter ();
您好,
根据您的描述,您希望使用列表级别,但您使用的代码是设置段落格式。在段落文本中,你添加了三行 Environment.NewLine,它实际上添加了三段。
如果你想添加列表项,请测试下面的代码:
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
Word.Range range = Globals.ThisAddIn.Application.ActiveDocument。范围(0,0);
Word.ListTemplate listTemplate = doc.Application.ListGalleries [Word.WdListGalleryType.wdOutlineNumberGallery] .ListTemplates [1];
listTemplate.ListLevels [1] .NumberFormat ="%1.";
listTemplate.ListLevels [1] .NumberStyle = Word.WdListNumberSty le.wdListNumberStyleArabic;
listTemplate.ListLevels [2] .NumberFormat ="%2。" ;;
listTemplate.ListLevels [2] .NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;
range.ListFormat.ApplyListTemplateWithLevel(listTemplate,true,Word.WdListApplyTo.wdListApplyToWholeList,Word.WdDefaultListBehavior.wdWord10ListBehavior,1);
int rangeLength = range.StoryLength - 1;
range.Text =" Test1" ;;
range.InsertParagraphAfter();
Word.Range subRange = doc.Range(range.StoryLength - 1);
subRange.Text =" Test2" ;;
subRange.InsertParagraphAfter();
subRange.SetListLevel(2);
Word.Range subRange2 = doc.Range(subRange.StoryLength - 1);
subRange2.Text =" Test3" ;;
subRange2.InsertParagraphAfter();
Word.Range range2 = doc.Range(range.StoryLength - 1);
range2.Text =" Test4" ;;
range2.InsertParagraphAfter();
range2.SetListLevel(1);结果:
我建议您在此处分享您的预期结果。你可以在创建时记录一个宏,这样你就可以检查你应该使用哪种属性和方法。
问候,
Celeste
I am new to c#. I'm trying to create a Word document programatically with list level. I have a rudimentary understanding of classes, methods, and namespaces. I can't get any formatting whatsoever in the code below to apply (the linespacing rules or tab indent). I'm trying to remove the 8 lines after each line in Word and set to 0.
Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
para1.Range.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceExactly;
para1.Range.ParagraphFormat.SpaceAfter = 0.0f;
para1.Range.Paragraphs.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceExactly;
para1.Range.Paragraphs.SpaceAfter = 0.0f;
para1.Range.ParagraphFormat.TabHangingIndent(1);
para1.Range.Text = "1. TESTING" + Environment.NewLine + "a. Line1" + Environment.NewLine + "b. Line2";
para1.Range.InsertParagraphAfter();
Hello,
According to your description, you want to play with list level, but the code you are using is setting the paragraph format. And in the paragraph text, you add three line with Environment.NewLine, it actually add three paragraphs.
If you want to add list item, please test the code below:
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument; Word.Range range = Globals.ThisAddIn.Application.ActiveDocument.Range(0, 0); Word.ListTemplate listTemplate = doc.Application.ListGalleries[Word.WdListGalleryType.wdOutlineNumberGallery].ListTemplates[1]; listTemplate.ListLevels[1].NumberFormat = "%1."; listTemplate.ListLevels[1].NumberStyle =Word.WdListNumberStyle.wdListNumberStyleArabic; listTemplate.ListLevels[2].NumberFormat = "%2."; listTemplate.ListLevels[2].NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic; range.ListFormat.ApplyListTemplateWithLevel(listTemplate, true, Word.WdListApplyTo.wdListApplyToWholeList, Word.WdDefaultListBehavior.wdWord10ListBehavior, 1); int rangeLength = range.StoryLength - 1; range.Text = "Test1"; range.InsertParagraphAfter(); Word.Range subRange = doc.Range(range.StoryLength - 1); subRange.Text = "Test2"; subRange.InsertParagraphAfter(); subRange.SetListLevel(2); Word.Range subRange2 = doc.Range(subRange.StoryLength - 1); subRange2.Text = "Test3"; subRange2.InsertParagraphAfter(); Word.Range range2 = doc.Range(range.StoryLength - 1); range2.Text = "Test4"; range2.InsertParagraphAfter(); range2.SetListLevel(1);The result:
I would suggest you share your expected result here. And you could record a macro when creating so that you could check what property and method you should use.
Regards,
Celeste
这篇关于无法使用c#以编程方式在Word doc中设置行间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!