|
gongda2006 发表于 2014-11-3 09:22
期待大家的帮助呀
一看就是新手提问,连附件都不上。一般我不会回答,不过出于对域的兴趣,就回答下,给你个参考,能否看懂及如何变化,自己去想:
Public WithEvents app As Application
Private Sub Document_New()
Set app = Application
End Sub
Private Sub Document_Open()
zjyty01
Set app = Application
End Sub
Private Sub app_WindowSelectionChange(ByVal Sel As Selection)
zjyty02
End Sub
Sub zjyty01() '增加域通用
Application.ScreenUpdating = False
Dim ta1 As Table, r1() As Range, r2() As Range
Set ta1 = ActiveDocument.Tables(1)
For i = 2 To ta1.Columns(6).Cells.Count
ReDim Preserve r1(i), r2(i)
Set r2(i) = ta1.Columns(6).Cells(i).Range
r2(i).Select
Selection = ""
Selection.MoveLeft
Application.Run "insertfieldchars"
Selection.TypeText "if "
Application.Run "insertfieldchars"
Selection.InsertAfter "_BKta15" & i
Selection.MoveRight , 3
Selection.InsertAfter " = " & Chr(34) & "一" & Chr(34) & " " & Chr(34) & "一" & Chr(34) & " " & """ 河北"""
Next
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
ActiveDocument.Fields.Update
Application.ScreenUpdating = 1
End Sub
Sub zjyty02() '增加域通用
Dim ta1 As Table, r1() As Range, r2() As Range
Set ta1 = ActiveDocument.Tables(1)
For i = 2 To ta1.Columns(6).Cells.Count
ReDim Preserve r1(i), r2(i)
Set r1(i) = ta1.Columns(5).Cells(i).Range
r1(i).SetRange r1(i).Start, r1(i).End - 1
ActiveDocument.Bookmarks.Add "_" & "BK" & "ta15" & i, r1(i)
Next
ActiveDocument.Fields.Update示例附件:
VBA自动填表测试.rar
(13.49 KB, 下载次数: 29)
|
|