2017-12-01 12 views
0

特定の位置に見出しを挿入しようとしましたが、ドキュメントの最後に常に追加されます。どこに私のミスは?:選択でWord見出しを挿入する

foreach (Publish p in tempstructure) 
{ 
    if (p.element_type_id == 3) 
    { 
     Word.Paragraph par = selection.Paragraphs.Add(); 
     par.Range.Text = "test"; 
     par.Range.set_Style(Word.WdBuiltinStyle.wdStyleHeading2); 
     par.Range.InsertParagraphAfter(); 
     selection.TypeParagraph(); 
    } 
    else 
    { 
     if (File.Exists(@Properties.Settings.Default.documentsPath + p.filename + "_" + language + ".docx")) 
     { 
      selection.InsertFile(@Properties.Settings.Default.documentsPath + p.filename + "_" + language + ".docx"); 
      selection = word.Selection; 
     } 
     else 
     { 
      selection.TypeText("Missing file: " + p.filename + "_" + language + ".docx"); 
      selection.TypeParagraph(); 
     } 
    } 
    selection = word.Selection; 
} 

よろしくです...

+0

使用しましたか..?あなたがforeachループの内側にいることに気付いています。あなたが期待しているものがわからない..見出しはループの外側に割り当てられるべきです – MethodMan

+0

こんにちは、別の単語の文書を追加しています1つの文書。これらの文書の間にはいくつかの見出しがあるはずです。デバッガは正しい位置でブレークしますが、見出しはドキュメントの最下部に追加されます - 私はそれを必要とするので、私はforeachループにあります – fillibuster

答えて

0

シンプルなソリューションです:あなたは、デバッガを

foreach (Publish p in tempstructure) 
{ 
    if (p.element_type_id == 3) 
    {     
     selection.set_Style(Word.WdBuiltinStyle.wdStyleHeading2); 
     selection.TypeText(p.name); 
     selection.TypeParagraph(); 
    } 
    else 
    { 
     if (File.Exists(@Properties.Settings.Default.documentsPath + p.filename + "_" + language + ".docx")) 
     { 
      selection.InsertFile(@Properties.Settings.Default.documentsPath + p.filename + "_" + language + ".docx"); 
      selection = word.Selection; 
     } 
     else 
     { 
      selection.TypeText("Missing file: " + p.filename + "_" + language + ".docx"); 
      selection.TypeParagraph(); 
     } 
    } 
    selection = word.Selection; 
} 
関連する問題