楼主 wqfzqgk |
导出所有表格到一表格.rar
- Private Sub Document_Open()
- Createmenu'调用创建菜单程序
- End Sub
- Public Sub Createmenu()'创建菜单
- Dim ctrl1 As CommandBarControl'定义
- Set myMenuBar = CommandBars.ActiveMenuBar'赋值
- Set newMenu = myMenuBar.Controls.Add(Type:=msoControlPopup, Temporary:=True)'增加菜单
- newMenu.Caption = "表格合并"
- Set ctrl1 = newMenu.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
- With ctrl1
- .Caption = "表格合并"
- .TooltipText = "表格合并"
- .Style = msoButtonCaption
- .OnAction = "表格合并1"
- .FaceId = 253
- End With
- End Sub
- Public Sub 表格合并1()
- UserForm2.Show
- End Sub
- Sub 定制表格样式()
- Dim i As Table, N As Integer
- On Error Resume Next '忽略错误
- Application.ScreenUpdating = False '关闭屏幕更新
- For Each i In ActiveDocument.Tables '在表格中循环
- With i
- .Style = "列表型 4" '将所有表格设置为"列表型4"的样式
- With .Borders '边框
- .InsideLineStyle = wdLineStyleSingle '设置内部边框线条
- End With
- With .Rows(1).Borders(wdBorderBottom) '第一行的底边框
- .LineStyle = wdLineStyleDouble '双线型
- .LineWidth = wdLineWidth050pt
- .Color = wdColorAutomatic
- End With
- If .Rows.Count > 1 Then ' 如果表格行数大于1
- If Len(.Cell(2, 1).Range) <= 2 Then '如果第二行第一列不为空
- With .Rows(2).Shading '设置底纹
- .Texture = wdTextureNone '无底底纹
- .ForegroundPatternColor = wdColorAutomatic
- .BackgroundPatternColor = wdColorGray125
- End With
- End If
- End If
- For N = 2 To .Columns.Count '从第二列到最后一列
- .Columns(N).Select '单元格对齐方式为中部居中
- Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
- Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter
- Next N
- End With
- Next i
- Application.ScreenUpdating = True
- End Sub
- Sub 复制表格到新表格中()
- Dim 原表格 As Document'定义
- Dim rngDoc As Range
- Dim tblDoc As Table
- If ActiveDocument.Tables.Count >= 1 Then
- Set 原表格 = ActiveDocument
- Set rngDoc = Documents.Add.Range(Start:=0, End:=0)
- For Each tblDoc In 原表格.Tables'循环表格
- tblDoc.Range.Copy'拷贝表格
- With rngDoc
- .Paste
- .Collapse Direction:=wdCollapseEnd
- .InsertParagraphAfter
- .Collapse Direction:=wdCollapseEnd
- End With
- Next
- End If
- End Sub
- Sub 表格排序()
- For i = 1 To ActiveDocument.Tables.Count
- ActiveDocument.Tables(i).Sort ExcludeHeader:=True
- Next
- End Sub
- Sub 表格转换为文本()
- For i = 1 To ActiveDocument.Tables.Count
- Dim tableTemp As Table
- Dim rngTemp As Range
- Set tableTemp = ActiveDocument.Tables(1)
- Set rngTemp = tableTemp.ConvertToText(Separator:=1)
- 'rngTemp.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries(wdBulletGallery).ListTemplates(1)'可以自定义格式
- Next
- End Sub
- Public Sub 合并表格()
- Dim adoc As Document
- Dim adoca As Document
- Set adoc = ActiveDocument
- Set adoca = Documents.Add
- adoc.Tables(1).Select
- Selection.Copy
- adoca.Content.Paste
- For i = 2 To adoc.Tables.Count
- adoc.Tables(i).Select
- Selection.Copy
- adoca.Tables(1).Select
- Selection.PasteAppendTable'追加表格到表格
- Next
- End Sub
- Public fils()
- Sub 打开文件()
- Dim fd As FileDialog'定义
- Dim cnt As Integer
- Set fd = Application.FileDialog(msoFileDialogFilePicker)'调用文件打开
- With fd
- .Filters.Add "word文件", "*.doc;*.docx", 1'过滤WORD文件
- .FilterIndex = 1'索引第一个
- .AllowMultiSelect = True'多选为真
- If .Show = -1 Then
- Count = 0
- For Each fil In .SelectedItems'所有文件中循环
- Count = Count + 1
- ReDim Preserve fils(1 To Count)
- fils(Count) = fil
- Next
- Else
- End If
- End With
- Set fd = Nothing
- End Sub
- Sub 表格合并()
- On Error Resume Next
- Dim biaoshu()
- Call 打开文件
- For i = 1 To UBound(fils)
- Documents.Open fils(i)
- For A = 1 To Documents(fils(i)).Tables.Count'调用打开文件中打开的每一个表格
- biaohang = biaohang + Documents(fils(i)).Tables(A).Rows.Count'计算表格的行数及列数
- biaolie = Documents(fils(i)).Tables(A).Columns.Count
- biaohang1 = Documents(fils(i)).Tables(A).Rows.Count
- biaolie1 = Documents(fils(i)).Tables(A).Columns.Count
- ReDim Preserve biaoshu(1 To biaohang)
- For bb = 1 To biaolie1
- For aa = 1 To biaohang1
- biaoshu(aa + biaohang - biaohang1) = biaoshu(aa + biaohang - biaohang1) & "♀" & Split(Documents(fils(i)).Tables(A).Cell(aa, bb).Range.Text, Chr(13))(0)'把表格中的文字加入到数组中
- Next aa
- Next bb
- Next A
- Documents(fils(i)).Close False'关闭当前文件
- Next i
- On Error Resume Next
- Dim newdoc As Document
- Dim mytable As Table
- Set newdoc = Documents.Add'新建一个文件
- 行数 = UBound(biaoshu)
- 列数 = biaolie
- If 行数 <= 0 Or 列数 <= 0 Then MsgBox "无法建立表格": Exit Sub
- Set mytable = newdoc.Tables.Add(Selection.Range, 行数, 列数)'建立一个新的表格
- mytable.Select'选择表格
- Call 表格线'增加表格线
- mytable.AllowAutoFit = True
- For i = 1 To 行数
- For A = 1 To 列数
- mytable.Cell(i, A).Range.Text = Split(biaoshu(i), "♀")(A)'表格中写入文字
- Next
- Next
- End Sub
- Sub 表格线()
- With Selection
- .Borders(wdBorderTop).LineStyle = Options.DefaultBorderLineStyle
- .Borders(wdBorderLeft).LineStyle = Options.DefaultBorderLineStyle
- .Borders(wdBorderBottom).LineStyle = Options.DefaultBorderLineStyle
- .Borders(wdBorderRight).LineStyle = Options.DefaultBorderLineStyle
- .Borders(wdBorderHorizontal).LineStyle = Options.DefaultBorderLineStyle
- .Borders(wdBorderVertical).LineStyle = Options.DefaultBorderLineStyle
- End With
- End Sub
- Public Sub 多文档合并表格()
- Call 打开文件
- Dim adoc As Document
- Dim adoca As Document
- Set adoca = Documents.Add'增加新文件
- adoca.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=9, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
- For A = 1 To UBound(fils)
- Documents.Open fils(A)
- For i = 1 To ActiveDocument.Tables.Count
- ActiveDocument.Tables(i).Select'选择表格
- Selection.Copy'拷贝
- adoca.Tables(1).Select
- Selection.PasteAppendTable'追加表格
- Next
- ActiveDocument.Close False'关闭不保存
- Next
- End Sub
- Private Sub CommandButton1_Click()
- On Error GoTo l
- Dim i As Table
- Label1.Caption = "曾用过的表格样式:"
- For Each i In ActiveDocument.Tables
- With i
- .Style = ComboBox1.Value'选择的样式
- End With
- i.Select
- Next
- ListBox1.AddItem i.Style
- Exit Sub
- l:
- MsgBox "此种样式不能用于表格中!"
- End Sub
- Private Sub CommandButton2_Click()
- Call 复制表格到新表格中
- End Sub
- Private Sub CommandButton3_Click()
- On Error GoTo l
- Dim i As Table
- Label1.Caption = "曾用过的表格字体:"
- For Each i In ActiveDocument.Tables
- With i
- i.Select
- Selection.Font.Name = ComboBox3.Value
- End With
- ListBox1.AddItem ComboBox3.Text
- Exit Sub
- Next
- l:
- MsgBox "此种字体不能用于表格中!"
- End Sub
- Private Sub CommandButton4_Click()
- 表格合并
- End Sub
- Private Sub CommandButton5_Click()
- 合并表格
- End Sub
- Private Sub CommandButton6_Click()
- 表格排序
- End Sub
- Private Sub CommandButton7_Click()
- 表格转换为文本
- End Sub
- Private Sub CommandButton8_Click()
- 定制表格样式
- End Sub
- Private Sub CommandButton9_Click()
- 多文档合并表格
- End Sub
- Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
- Dim i As Integer
- On Error GoTo l:
- Select Case Label1.Caption
- Case "曾用过的表格样式:"
- For i = 0 To ListBox1.ListCount
- MsgBox ListBox1.List(i)
- If ListBox1.Selected(i) = True Then
- ComboBox2.AddItem ListBox1.List(i)
- End If
- Next i
- Case "曾用过的表格字体:"
- Exit Sub
- End Select
- l:
- MsgBox "您没有选择,请选择后再双击鼠标!"
- End Sub
- Private Sub UserForm_Initialize()
- Dim afont
- For Each st In ActiveDocument.Styles
- ComboBox1.AddItem st
- Next
- ComboBox1.ListIndex = 1
- Label1.Caption = "操作项目:"
- Label2.Caption = "表格样式:"
- Label3.Caption = "表格字体:"
- 'Label4.Caption = "表格边框:"
- For Each afont In FontNames
- ComboBox3.AddItem afont
- Next
- ComboBox3.ListIndex = 1
- ComboBox3.Font.Italic = True
- ListBox1.MultiSelect = fmMultiSelectSingle
- TextBox1.Value = ActiveDocument.Tables.Count
- End Sub
- Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long) As Long
- Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
- Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
- Private Declare Function CreateRectRgn Lib "gdi32.dll" (ByVal X As Long, ByVal Y As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
- Private Declare Function CombineRgn Lib "gdi32.dll" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
- Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
- Private Declare Function SetWindowRgn Lib "user32" (ByVal Hwnd As Long, ByVal A As Long, ByVal bRedraw As Long) As Long
- Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '延迟时间
- Dim Hwnd As Long, A As Long, B As Long
- Dim flag As Boolean
- Private Sub UserForm_Initialize() '去除标题区与边框
- TextBox1.Top = Me.Height - 10
- Hwnd = FindWindow("ThunderDFrame", Me.Caption)
- SetWindowLong Hwnd, -16, GetWindowLong(Hwnd, -16) And Not &HC00000
- End Sub
- Private Sub UserForm_Activate()
- 'Application.Visible = False
- For X = 0 To 40 '其修改40可以百叶窗的数量,从而改变每叶的宽度
- A = CreateRectRgn(0, 0, 0, 0)
- For Y = 0 To Me.Width / 40
- B = CreateRectRgn(Y * 40, 0, Y * 40 + X, Me.Height)
- CombineRgn A, A, B, 3
- DeleteObject B
- Next Y
- SetWindowRgn Hwnd, A, True
- DoEvents
- Sleep 30 '延迟时间,用它决定速度
- Next X
- On Error Resume Next
- flag = True
- Do '开始循环
- DoEvents '转换控制权,从而展现动画效果
- Sleep 30 '以毫秒为单位延时30个单位
- With TextBox1
- .Top = .Top - 1 '将文字框的上边距减1
- If .Top < -(Me.Height) Then .Top = Me.Height '如果消失的窗体框外就返回重新开始
- End With
- Loop While flag
- End Sub
- Private Sub UserForm_Click() '单击关闭窗体
- Unload Me
- Application.Visible = True
- UserForm1.Show
- End Sub
|