2009-12-21 67 views
2

我正在使用Interop.Microsoft.Office.Interop.Word.dll在C#中動態構建Word文檔。Microsoft Word中的編號列表

有沒有人有代碼示例來創建一個編號列表?

+3

我沒有一個例子,但是找到這種東西的一個非常簡單的方法是錄製一個宏,然後查看錄製的宏代碼。通常有很多無關的東西,你不需要,但它的肉通常很明顯,並指出你在正確的方向... – 2009-12-21 13:17:06

回答

5

試試這個...它假設你有一個對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); 

edit:formatting。

+0

我知道這是一個古老的問題,但我遇到它時,尋找如何使用Interop.Word重新啓動列表編號。你知道我將如何部分[適應](http://stackoverflow.com/q/11674952/869912)此代碼爲Office 2003? – 2012-07-26 19:34:49

+0

嗨吉姆你可以檢查這個問題http://stackoverflow.com/questions/23380676/format-numbered-list-with-styles,你能幫我找到一個答案? – 2014-04-30 11:49:41