我已经设置了Word VBA用户表单的初始化,该表单由一个列表框和接受/取消按钮组成,因此第一次运行时,Word文件中的内容被加载到一个数组中然后进入列表框名单。随后,使用标志跳过数组创建,并使用现有数组(声明为public)重新填充列表框列表。首次运行时,将显示所有三列数据。但重新运行时,只会显示第一列。非可见列中的数据仍然存在(即宏运行正确),它似乎不会显示。如果将if-then注释掉,那么表单也可以完美地运行。有没有人有想要显示列的想法?
Private Sub UserForm_Initialize()
If QueryArrayFlag = False Then
Dim sourcedoc As Document
Dim i As Integer
Dim j As Integer
Dim myitem As Range
Dim m As Long
Dim n As Long
Dim FilePath As String
Dim FileName As String
FilePath = ActiveDocument.AttachedTemplate.Path & Application.PathSeparator
FileName = "PresetQueries.docx"
Set sourcedoc = Documents.Open(FileName:=FilePath & FileName)
i = sourcedoc.tables(1).Rows.Count - 1
j = 3
QueryListBox.ColumnCount = j
QueryListBox.ColumnWidths = "20;60;750"
ReDim QueryArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.tables(1).cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
QueryArray(m, n) = myitem.Text
Next m
Next n
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
QueryArrayFlag = True
End If
QueryListBox.List() = QueryArray
QueryListBox.ListIndex = 0
End Sub