2012-12-07 1 views
-1

J'ai créé une table. La table commence à la première ligne du document. Mon problème est que je ne peux pas insérer une ligne au-dessus de la table. Chaque fois que j'essaie d'ajouter un paragraphe, le paragraphe est inséré après la dernière table. Y a-t-il un moyen d'insérer le lin au-dessus de la première table?Insérer une ligne au-dessus d'un tableau Word en haut de la page

Pour illustrer mon problème:

http://www.techrepublic.com/blog/msoffice/insert-a-line-above-a-word-table-at-the-top-of-the-page/846

Mon code à ce jour:

Dim oApp As Word.Application 
    Dim oDoc As Word.Document 


    oApp = CType(CreateObject("Word.Application"), Word.Application) 
    oDoc = oApp.Documents.Add() 


    Dim rng As Word.Range = oDoc.Range(0, 0) 
    rng.Font.Name = "Verdana" 
    rng.Font.Size = 16 


    Dim para As Word.Paragraph = oDoc.Paragraphs.Add() 
    para.Range.Text = "Factsheet" 



    Dim tlb6 As Word.Table = oDoc.Tables.Add(Range:=rng, NumRows:=1, NumColumns:=4) 


    Dim CurrentDateTime As Date = Date.Now 
    Dim CurrentDate As Date = New Date(CurrentDateTime.Year, CurrentDateTime.Month, CurrentDateTime.Day) 

    tlb6.Cell(1, 1).Range.Text = "Date" 
    tlb6.Cell(1, 1).Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20 
    tlb6.Cell(1, 1).Borders.OutsideLineStyle = 1 
    tlb6.Cell(1, 2).Range.Text = CurrentDate 
    tlb6.Cell(1, 2).Borders.OutsideLineStyle = 1 
    tlb6.Cell(1, 3).Range.Text = "Issued by" 
    tlb6.Cell(1, 3).Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20 
    tlb6.Cell(1, 3).Borders.OutsideLineStyle = 1 
    tlb6.Cell(1, 4).Range.Text = "" 
    tlb6.Cell(1, 4).Borders.OutsideLineStyle = 1 

Répondre

2

Si vous utilisez Word 2010, cela devrait faire le travail (bien qu'il soit laid et utilisations Select and ActiveDocument):

If ActiveDocument.Paragraphs(1).Range.Information(wdWithInTable) = True Then 
    tbl6.Rows(1).Cells(1).Range.Collapse direction:=wdLeft 
    Selection.SplitTable 
End If 

Il vérifie que Le premier objet Paragraphe est à l'intérieur d'une table, et si c'est le cas, il ajoute une ligne de texte au-dessus.

+0

Merci pour votre aide. Cela a inséré une ligne au-dessus de la table. Mais j'ai toujours le problème que le paragraphe est inséré après la dernière table. Je ne sais pas pourquoi ... – Paks

+0

Avoir ce paragraphe supplémentaire un problème? Je ne peux pas reproduire cela de mon côté - il y a toujours une ligne après ma dernière table, et rien n'est ajouté à la fin quand je lance ce code. –

0

Sur la base de la réponse de Kevin un peu plus propre solution:

ActiveDocument.Range(0, 0).Select 
If Selection.Information(wdWithInTable) = True Then 
    Selection.InsertBreak Type:=wdColumnBreak 
End If 
Questions connexes