мне нужно создать таблицу в нижнем колонтитуле .docx с помощью excel vba. У меня есть моя ячейка, но знаете ли вы, есть ли способ изменить ее размер и положение? Спасибо
Редактировать: я нашел это: https://docs.microsoft.com/en-us/office/vba/api/word.tables.add
Но не знаю, что добавить в DefaultTableBehavior и AutoFitBehavior.
Option Explicit
Sub test()
'Déclaration des variables
Dim I As Integer
Dim Fichier As String
Dim MaFeuille As Worksheet
Dim ListeBordures As Variant
Dim word_app As Word.Application, word_fichier As Word.Document, tbl As Word.Table, rngCell As Word.Range
'On récupère le fichier test
Fichier = ActiveWorkbook.Path & "\" & "test.docx"
'Ouverture de word
Set word_app = CreateObject("Word.Application")
With word_app
.Visible = True
.WindowState = 1
Set word_fichier = .Documents.Open(Fichier)
End With
'Test tableau bas de page
With word_fichier
Set tbl = .Tables.Add(.Sections(1).Footers(wdHeaderFooterPrimary).Range, 1, 1)
ListeBordures = Array(-1, -2, -3, -4)
With tbl
.Cell(1, 1).Range.Text = "test"
For I = LBound(ListeBordures) To UBound(ListeBordures)
With .Borders(ListeBordures(I))
.LineStyle = wdLineStyleDot
.LineWidth = wdLineWidth100pt
End With
Next I
End With
End With
Set tbl = Nothing: Set word_fichier = Nothing: Set word_app = Nothing
End Sub
После нескольких часов исследований я нашел эти строки:
Sub test()
'Déclaration des variables
Dim I As Integer
Dim Fichier As String
Dim MaFeuille As Worksheet
Dim ListeBordures As Variant
Dim word_app As Word.Application, word_fichier As Word.Document, tbl As Word.Table, rngCell As Word.Range
'On récupère le fichier test
Fichier = ActiveWorkbook.Path & "\" & "test.docx"
'Ouverture de word
Set word_app = CreateObject("Word.Application")
With word_app
.Visible = True
.WindowState = 1
Set word_fichier = .Documents.Open(Fichier)
End With
'Test tableau bas de page
With word_fichier
Set tbl = .Tables.Add(.Sections(1).Footers(wdHeaderFooterPrimary).Range, 1, 1, wdWord8TableBehavior)
ListeBordures = Array(-1, -2, -3, -4)
With tbl
.Cell(1, 1).Range.Text = "test"
.Rows.HorizontalPosition = InchesToPoints(1)
.Rows.VerticalPosition = InchesToPoints(-2)
.Columns(1).SetWidth ColumnWidth:=310.7, RulerStyle:=1
For I = LBound(ListeBordures) To UBound(ListeBordures)
With .Borders(ListeBordures(I))
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth225pt
.Color = RGB(255, 0, 0)
End With
Next I
End With
End With
Set tbl = Nothing: Set word_fichier = Nothing: Set word_app = Nothing
End Sub
С .Rows.HorizontalPosition = InchesToPoints(1)
и .Rows.VerticalPosition = InchesToPoints(-2)
для позиционирования
И .Columns(1).SetWidth ColumnWidth:=310.7, RulerStyle:=1
за размер