vb.net 如何通过 Word Interop 专门对齐每个段落?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23538624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to align each paragraph specifically by Word Interop?
提问by gumuruh
This is my code:
这是我的代码:
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
Dim oRng As Word.Range
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add
oPara1 = oDoc.Content.Paragraphs.Add
oPara1.Range.InlineShapes.AddPicture(sNewData.Picture)
oPara1.Format.SpaceAfter = 24
oPara1.Range.InsertParagraphAfter()
oPara2 = oDoc.Content.Paragraphs.Add
oPara2.Range.Text = nTP.Nama
oPara2.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
oPara2.Range.Font.Bold = True
oPara2.Range.Font.Size = 18
oPara2.Range.Font.Color = Word.WdColor.wdColorBlue
oPara2.Format.SpaceAfter = 6
oPara2.Range.InsertParagraphAfter()
oPara3 = oDoc.Content.Paragraphs.Add
oPara3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
oPara3.Range.Text = "Special arrangement for Mr/Mrs. " & customer
oPara3.Range.Font.Color = Word.WdColor.wdColorOliveGreen
oPara3.Format.SpaceAfter = 3
oPara3.Range.InsertParagraphAfter()
The strange thing is the oPara1is aligned at left, while oPara2is aligned at center. But the oPara3is not aligned at left.
The output is that oPara3is following the previous alignment which is center.
So how could I apply for a specific paragraph its own alignment or style?
I found some discussion about "Style" but I confused.
奇怪的是oPara1在对准左边,而oPara2在对准中心。但是oPara3未在left对齐。
输出是oPara3跟随先前的对齐方式,即center。那么我如何才能为特定段落申请其自己的对齐方式或样式?
我发现了一些关于“ Style”的讨论,但我很困惑。
回答by user3165438
This C# code aligns the whole Word document successfully:
此 C# 代码成功对齐整个 Word 文档:
foreach (Paragraph p in oWord.ActiveDocument.Paragraphs)
{
p.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
}
回答by Antonio
Your proposal was right, BUT: When you you modify the property TEXT in the code, the Alignment property restart to Left. You can try this inserting several times this code to "debug" the status of the Alignment property. One before you set the alignment, one after you modify the alignment, one after you change the "Text" property, then run the program.
您的建议是正确的,但是:当您修改代码中的属性 TEXT 时,Alignment 属性会重新启动到 Left。您可以尝试多次插入此代码以“调试”对齐属性的状态。一种是在设置对齐方式之前,一种是在修改对齐方式之后,一种是在更改“文本”属性之后,然后运行程序。
Msgbox(oPara3.Range.ParagraphFormat.Alignment)
So the solution is to modify the Alignment just before inserting the Paragraph:
所以解决方案是在插入段落之前修改对齐方式:
oPara3 = oDoc.Content.Paragraphs.Add
/*oPara3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft*/
oPara3.Range.Text = "Special arrangement for Mr/Mrs. " & customer
oPara3.Range.Font.Color = Word.WdColor.wdColorOliveGreen
oPara3.Format.SpaceAfter = 3
oPara3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
oPara3.Range.InsertParagraphAfter()
回答by shA.t
AFAIK, Adding a new paragraph is like pressing Enter / Return, so at first format of a new paragraph sets based on Normal Stylethen after changing any property of current paragraph, it stored as current format that will apply to new paragraphs from now on.
So to change this behavior that makes all new paragraphs to use same format instead of some situation I can suggest to use a method like this - but in C# -:
AFAIK,添加新段落就像按Enter / Return,因此首先基于普通样式设置新段落的格式,然后在更改当前段落的任何属性后,将其存储为当前格式,从现在开始将应用于新段落。
因此,要更改使所有新段落都使用相同格式而不是某些情况的这种行为,我可以建议使用这样的方法 - 但在 C# 中 -:
public static void AddParagraph(ref Document doc, string text,
WdParagraphAlignment align = WdParagraphAlignment.wdAlignParagraphLeft, ...)
// ... means for other format specifications you can do it same
{
var para = doc.Content.Paragraphs.Add();
para.Alignment = align;
para.Range.Text = text;
}
回答by Lord Darth Vader
This is what worked for me
这对我有用
Range content = doc.Content;
Paragraph pText = content.Paragraphs.Add();
Text.Range.Text = "sadfsadfsda"
pText.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
回答by eren
I know this is old question, but the solutions mentioned here didn't help me. So i'll post the solution here for ppl like me in hope it will help them.
我知道这是个老问题,但这里提到的解决方案对我没有帮助。因此,我将在此处为像我这样的人发布解决方案,希望对他们有所帮助。
The thing you need to do is set the Alignment property AFTER setting the Text property (see the code below).
您需要做的是在设置 Text 属性之后设置 Alignment 属性(请参阅下面的代码)。
'p1 is going to be right aligned
var p1 = document.Paragraphs.Add(System.Reflection.Missing.Value)
p1.Range.Font.Name = "Calibri"
p1.Range.Font.Size = 18
p1.Range.Text = "right"
p1.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
p1.Range.InsertParagraphAfter()
'p2 is going to be center aligned
var p2 = document.Paragraphs.Add(System.Reflection.Missing.Value)
p2.Range.Font.Name = "Calibri"
p2.Range.Font.Size = 16
p2.Range.Text = "center"
p2.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
p2.Range.InsertParagraphAfter()
'p3 is going to be left aligned
var p3 = document.Paragraphs.Add(System.Reflection.Missing.Value)
p3.Range.Font.Name = "Calibri"
p3.Range.Font.Size = 14;
p3.Range.Text = "left"
p3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
p3.Range.InsertParagraphAfter()
This is the way it worked for me.
这就是它对我来说有效的方式。
回答by Bahodir
This is worked for me!
这对我有用!
Range content = doc.ActiveDocument.Content;
Paragraph pText = content.Paragraphs.Add();
pText.Range.ParagraphFormat.Alignment =
WdParagraphAlignment.wdAlignParagraphRight;
doc.Selection.TypeText("some text");

