如何通过Word Interop专门对齐每个段落?

时间:2014-05-08 10:03:31

标签: vb.net ms-word alignment office-interop

这是我的代码:

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()

奇怪的是 oPara1 left 处对齐,而 oPara2 中心。但 oPara3 未与 left 对齐 输出结果是 oPara3 跟随上一个 center 对齐。 那么我怎么能申请一个特定的段落自己的对齐或风格?
我发现了一些关于“Style”的讨论,但我很困惑。

5 个答案:

答案 0 :(得分:3)

此C#代码成功对齐整个Word文档:

foreach (Paragraph p in oWord.ActiveDocument.Paragraphs)
{
    p.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
}

答案 1 :(得分:0)

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;
}

答案 2 :(得分:0)

这对我有用

Range content = doc.Content;
Paragraph pText = content.Paragraphs.Add();                    
Text.Range.Text = "sadfsadfsda"
pText.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

答案 3 :(得分:0)

我知道这是一个老问题,但这里提到的解决方案并没有帮助我。 所以我会在这里发布像我这样的ppl的解决方案,希望它能帮到他们。

您需要做的是设置Alignment属性AFTER设置Text属性(请参阅下面的代码)。

'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()

这是我的工作方式。

答案 4 :(得分:0)

这对我有用!

  Range content = doc.ActiveDocument.Content;

  Paragraph pText = content.Paragraphs.Add(); 

  pText.Range.ParagraphFormat.Alignment = 
  WdParagraphAlignment.wdAlignParagraphRight;

  doc.Selection.TypeText("some text");