使用 excel VBA 在字尾添加 table
Adding a table in a word footer using excel VBA
我想使用 excel vba 在 .docx 的页脚中添加 table。
最后,宏会更改我的 .docx 中的一些标记,页脚可能会有所不同。这就是为什么我想使用 excel.
这是我的代码,但出现 91 错误,我不知道为什么。欢迎任何建议:)
Option Explicit
Private Sub test()
'Déclaration des variables
Dim MaFeuille As Worksheet
Dim word_app As Word.Application
Dim word_fichier As Word.Document
Dim tbl As Word.Table
Dim fichier As String
'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
End With
'Test tableau bas de page
With word_fichier
Set tbl = .Tables.Add(word_fichier.Sections(1).Footers(wdHeaderFooterPrimary).Range, 2, 2)
End With
With tbl
.Cell(1, 1).Range.Text = "test"
End With
End Sub
wdHeaderFooterPrimary
是一个 Word 常量(参见 WdHeaderFooterIndex enumeration (Word))并且不存在于 Excel 中。所以你需要改用它的值。
Set tbl = .Tables.Add(word_fichier.Sections(1).Footers(1).Range, 2, 2)
或者您需要在使用前定义它:
Const wdHeaderFooterPrimary As Long = 1
Set tbl = .Tables.Add(word_fichier.Sections(1).Footers(wdHeaderFooterPrimary).Range, 2, 2)
忘记打开word文档了,这里是新代码
Option Explicit
Private Sub test()
'Déclaration des variables
Dim MaFeuille As Worksheet
Dim word_app As Word.Application
Dim word_fichier As Word.Document
Dim tbl As Word.Table
Dim fichier As String
Dim ListeBordures As Variant
Dim I As Integer
'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)
With tbl
.Cell(1, 1).Range.Text = "test"
End With
End With
End Sub
它正在“工作”,但我想要像这样在 table 中进行“测试”:
而不是这个(我想要的,我用油漆来做):
我想使用 excel vba 在 .docx 的页脚中添加 table。
最后,宏会更改我的 .docx 中的一些标记,页脚可能会有所不同。这就是为什么我想使用 excel.
这是我的代码,但出现 91 错误,我不知道为什么。欢迎任何建议:)
Option Explicit
Private Sub test()
'Déclaration des variables
Dim MaFeuille As Worksheet
Dim word_app As Word.Application
Dim word_fichier As Word.Document
Dim tbl As Word.Table
Dim fichier As String
'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
End With
'Test tableau bas de page
With word_fichier
Set tbl = .Tables.Add(word_fichier.Sections(1).Footers(wdHeaderFooterPrimary).Range, 2, 2)
End With
With tbl
.Cell(1, 1).Range.Text = "test"
End With
End Sub
wdHeaderFooterPrimary
是一个 Word 常量(参见 WdHeaderFooterIndex enumeration (Word))并且不存在于 Excel 中。所以你需要改用它的值。
Set tbl = .Tables.Add(word_fichier.Sections(1).Footers(1).Range, 2, 2)
或者您需要在使用前定义它:
Const wdHeaderFooterPrimary As Long = 1
Set tbl = .Tables.Add(word_fichier.Sections(1).Footers(wdHeaderFooterPrimary).Range, 2, 2)
忘记打开word文档了,这里是新代码
Option Explicit
Private Sub test()
'Déclaration des variables
Dim MaFeuille As Worksheet
Dim word_app As Word.Application
Dim word_fichier As Word.Document
Dim tbl As Word.Table
Dim fichier As String
Dim ListeBordures As Variant
Dim I As Integer
'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)
With tbl
.Cell(1, 1).Range.Text = "test"
End With
End With
End Sub
它正在“工作”,但我想要像这样在 table 中进行“测试”:
而不是这个(我想要的,我用油漆来做):