如何用VBA 向Word表格中插入值, 又如何判断表格中的值是否为空呢? wj-wangjun
图片 |
文件名称: |
||||||
密级: |
|
版次/ 修订状态: |
A/0 |
文件编号: |
Page 1 of 3 |
========要判断 以上表格中如果有空时,则弹出对话框页面==========
'当打开Word时弹出对话框 要用户输入内容
Private Sub Document_Open()
Dim flag As Boolean
flag = True
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1).Range
For i = 2 To 9 Step 1
If Len(.Cells(i).Range.Text) = 2 Then
flag = False
End If
Next
If flag = False Then
AddHeader.Show
End If
End With
End Sub
Private Sub Document_Open()
Dim flag As Boolean
flag = True
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1).Range
For i = 2 To 9 Step 1
If Len(.Cells(i).Range.Text) = 2 Then
flag = False
End If
Next
If flag = False Then
AddHeader.Show
End If
End With
End Sub
=====点确定后将页面对话框中的值 插入到word 的页眉表格中====
Private Sub CommandButton1_Click()
With ActiveDocument.Sections(1)
'Cell(4, 2).Range.InsertBefore (TextBox4.value)
' .Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 2).Range.InsertAfter ("aaa")
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 3).Range.Delete
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 3).Range.InsertAfter (TxtFileName.Value)
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 2).Range.Delete
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 2).Range.InsertAfter (TxtSecrecy.Value)
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 4).Range.Delete
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 4).Range.InsertAfter (TxtRev.Value)
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 6).Range.Delete
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 6).Range.InsertAfter (TxtID.Value)
'页尾的做法
' .Footers(wdHeaderFooterPrimary).Range.Text = "Copyright BYD Company Limited"
End With
AddHeader.Hide
End Sub
With ActiveDocument.Sections(1)
'Cell(4, 2).Range.InsertBefore (TextBox4.value)
' .Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 2).Range.InsertAfter ("aaa")
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 3).Range.Delete
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 3).Range.InsertAfter (TxtFileName.Value)
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 2).Range.Delete
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 2).Range.InsertAfter (TxtSecrecy.Value)
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 4).Range.Delete
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 4).Range.InsertAfter (TxtRev.Value)
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 6).Range.Delete
.Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 6).Range.InsertAfter (TxtID.Value)
'页尾的做法
' .Footers(wdHeaderFooterPrimary).Range.Text = "Copyright BYD Company Limited"
End With
AddHeader.Hide
End Sub
========提问 =================
比如说sheet1 中A1:A10不为空,用代码判断不为空后,光标移向A11,并且A11=11,B11=22
再录入数据后光标又光标移向A12,并且A12=22,B12=23,依此类推
========解决方法:===============================
Sub Text()
If IsEmpty([A1]) Then
[A1] = "你的数据"
Else
[A65536].End(xlUp).Offset(1, 0).Value = "你的数据"
End If
End Sub
========提问 =================
判断word中表格是否为空
========解决方法:===============================
'a.Cells.Count=41,46,41+5*7
Dim list%
'你的要求可能是这样
With ActiveDocument.Tables(1).Range
For i = 41 To 41 + 5 * 7 Step 5 '测得第一项的后面对应的是第41个单元格,相差是5。所以,step=5
list = list + 1 '利用list来得到是第几项没有加项
If Len(.Cells(i).Range.Text) = 2 Then '因为类回车加+竖线加在一起是2个长度。所以。。这样判断
'n = .Cells(i - 4).Range.Text '取得单元格的内容+回车+竖线
'n = Mid(n, 1, Len(n) - 2) '去掉回车+竖线的,即我们所看到的内容
MsgBox "提示:没有给第" & list & "项打分!", vbOKOnly, "www.excelhome.net"
.Cells(i).Range.Select '选中所在的单元格
Exit Sub
End If
Next
End With
Dim list%
'你的要求可能是这样
With ActiveDocument.Tables(1).Range
For i = 41 To 41 + 5 * 7 Step 5 '测得第一项的后面对应的是第41个单元格,相差是5。所以,step=5
list = list + 1 '利用list来得到是第几项没有加项
If Len(.Cells(i).Range.Text) = 2 Then '因为类回车加+竖线加在一起是2个长度。所以。。这样判断
'n = .Cells(i - 4).Range.Text '取得单元格的内容+回车+竖线
'n = Mid(n, 1, Len(n) - 2) '去掉回车+竖线的,即我们所看到的内容
MsgBox "提示:没有给第" & list & "项打分!", vbOKOnly, "www.excelhome.net"
.Cells(i).Range.Select '选中所在的单元格
Exit Sub
End If
Next
End With