ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的酷贴 > Word > word表格的操作,合并等

word表格的操作,合并等

作者:绿色风 分类: 时间:2022-08-18 浏览:109
楼主
wqfzqgk
导出所有表格到一表格.rar

  1. Private Sub Document_Open()
  2. Createmenu'调用创建菜单程序
  3. End Sub

  1. Public Sub Createmenu()'创建菜单
  2. Dim ctrl1 As CommandBarControl'定义
  3. Set myMenuBar = CommandBars.ActiveMenuBar'赋值
  4. Set newMenu = myMenuBar.Controls.Add(Type:=msoControlPopup, Temporary:=True)'增加菜单
  5. newMenu.Caption = "表格合并"
  6. Set ctrl1 = newMenu.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
  7. With ctrl1
  8. .Caption = "表格合并"
  9. .TooltipText = "表格合并"
  10. .Style = msoButtonCaption
  11. .OnAction = "表格合并1"
  12. .FaceId = 253
  13. End With
  14. End Sub
  15. Public Sub 表格合并1()
  16. UserForm2.Show
  17. End Sub
  18. Sub 定制表格样式()
  19. Dim i As Table, N As Integer
  20. On Error Resume Next '忽略错误
  21. Application.ScreenUpdating = False '关闭屏幕更新
  22. For Each i In ActiveDocument.Tables '在表格中循环
  23. With i
  24. .Style = "列表型 4" '将所有表格设置为"列表型4"的样式
  25. With .Borders '边框
  26. .InsideLineStyle = wdLineStyleSingle '设置内部边框线条
  27. End With
  28. With .Rows(1).Borders(wdBorderBottom) '第一行的底边框
  29. .LineStyle = wdLineStyleDouble '双线型
  30. .LineWidth = wdLineWidth050pt
  31. .Color = wdColorAutomatic
  32. End With
  33. If .Rows.Count > 1 Then ' 如果表格行数大于1
  34. If Len(.Cell(2, 1).Range) <= 2 Then '如果第二行第一列不为空
  35. With .Rows(2).Shading '设置底纹
  36. .Texture = wdTextureNone '无底底纹
  37. .ForegroundPatternColor = wdColorAutomatic
  38. .BackgroundPatternColor = wdColorGray125
  39. End With
  40. End If
  41. End If
  42. For N = 2 To .Columns.Count '从第二列到最后一列
  43. .Columns(N).Select '单元格对齐方式为中部居中
  44. Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
  45. Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter
  46. Next N
  47. End With
  48. Next i
  49. Application.ScreenUpdating = True
  50. End Sub
  51. Sub 复制表格到新表格中()
  52.     Dim 原表格 As Document'定义
  53.     Dim rngDoc As Range
  54.     Dim tblDoc As Table
  55.     If ActiveDocument.Tables.Count >= 1 Then
  56.         Set 原表格 = ActiveDocument
  57.         Set rngDoc = Documents.Add.Range(Start:=0, End:=0)
  58.         For Each tblDoc In 原表格.Tables'循环表格
  59.             tblDoc.Range.Copy'拷贝表格
  60.             With rngDoc
  61.                 .Paste
  62.                 .Collapse Direction:=wdCollapseEnd
  63.                 .InsertParagraphAfter
  64.                 .Collapse Direction:=wdCollapseEnd
  65.             End With
  66.         Next
  67.     End If
  68. End Sub
  69. Sub 表格排序()
  70. For i = 1 To ActiveDocument.Tables.Count
  71.     ActiveDocument.Tables(i).Sort ExcludeHeader:=True
  72.     Next
  73. End Sub
  74. Sub 表格转换为文本()
  75. For i = 1 To ActiveDocument.Tables.Count
  76. Dim tableTemp As Table
  77. Dim rngTemp As Range
  78. Set tableTemp = ActiveDocument.Tables(1)
  79. Set rngTemp = tableTemp.ConvertToText(Separator:=1)
  80. 'rngTemp.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries(wdBulletGallery).ListTemplates(1)'可以自定义格式
  81. Next
  82. End Sub
  83. Public Sub 合并表格()
  84. Dim adoc As Document
  85. Dim adoca As Document
  86. Set adoc = ActiveDocument
  87. Set adoca = Documents.Add
  88. adoc.Tables(1).Select
  89. Selection.Copy
  90. adoca.Content.Paste
  91. For i = 2 To adoc.Tables.Count
  92. adoc.Tables(i).Select
  93. Selection.Copy
  94. adoca.Tables(1).Select
  95.     Selection.PasteAppendTable'追加表格到表格
  96.   Next
  97. End Sub

  1. Public fils()
  2. Sub 打开文件()
  3.     Dim fd As FileDialog'定义
  4.     Dim cnt As Integer
  5.     Set fd = Application.FileDialog(msoFileDialogFilePicker)'调用文件打开
  6.   With fd
  7.    .Filters.Add "word文件", "*.doc;*.docx", 1'过滤WORD文件
  8.    .FilterIndex = 1'索引第一个
  9.    .AllowMultiSelect = True'多选为真
  10.         If .Show = -1 Then
  11.         Count = 0
  12.             For Each fil In .SelectedItems'所有文件中循环
  13.             Count = Count + 1
  14.             ReDim Preserve fils(1 To Count)
  15.             fils(Count) = fil
  16.             Next
  17.         Else
  18.         End If
  19.     End With
  20.     Set fd = Nothing
  21. End Sub
  22. Sub 表格合并()
  23. On Error Resume Next
  24. Dim biaoshu()
  25. Call 打开文件
  26. For i = 1 To UBound(fils)
  27. Documents.Open fils(i)
  28. For A = 1 To Documents(fils(i)).Tables.Count'调用打开文件中打开的每一个表格
  29. biaohang = biaohang + Documents(fils(i)).Tables(A).Rows.Count'计算表格的行数及列数
  30. biaolie = Documents(fils(i)).Tables(A).Columns.Count
  31. biaohang1 = Documents(fils(i)).Tables(A).Rows.Count
  32. biaolie1 = Documents(fils(i)).Tables(A).Columns.Count
  33. ReDim Preserve biaoshu(1 To biaohang)
  34. For bb = 1 To biaolie1
  35. For aa = 1 To biaohang1
  36. biaoshu(aa + biaohang - biaohang1) = biaoshu(aa + biaohang - biaohang1) & "♀" & Split(Documents(fils(i)).Tables(A).Cell(aa, bb).Range.Text, Chr(13))(0)'把表格中的文字加入到数组中
  37. Next aa
  38. Next bb
  39. Next A
  40. Documents(fils(i)).Close False'关闭当前文件
  41. Next i
  42. On Error Resume Next
  43. Dim newdoc As Document
  44. Dim mytable As Table
  45. Set newdoc = Documents.Add'新建一个文件
  46. 行数 = UBound(biaoshu)
  47. 列数 = biaolie
  48. If 行数 <= 0 Or 列数 <= 0 Then MsgBox "无法建立表格": Exit Sub
  49. Set mytable = newdoc.Tables.Add(Selection.Range, 行数, 列数)'建立一个新的表格
  50. mytable.Select'选择表格
  51. Call 表格线'增加表格线
  52. mytable.AllowAutoFit = True
  53. For i = 1 To 行数
  54. For A = 1 To 列数
  55. mytable.Cell(i, A).Range.Text = Split(biaoshu(i), "♀")(A)'表格中写入文字
  56. Next
  57. Next
  58. End Sub
  59. Sub 表格线()
  60. With Selection
  61.         .Borders(wdBorderTop).LineStyle = Options.DefaultBorderLineStyle
  62.         .Borders(wdBorderLeft).LineStyle = Options.DefaultBorderLineStyle
  63.         .Borders(wdBorderBottom).LineStyle = Options.DefaultBorderLineStyle
  64.         .Borders(wdBorderRight).LineStyle = Options.DefaultBorderLineStyle
  65.        .Borders(wdBorderHorizontal).LineStyle = Options.DefaultBorderLineStyle
  66.         .Borders(wdBorderVertical).LineStyle = Options.DefaultBorderLineStyle
  67.     End With
  68. End Sub
  69. Public Sub 多文档合并表格()
  70. Call 打开文件
  71. Dim adoc As Document
  72. Dim adoca As Document
  73. Set adoca = Documents.Add'增加新文件
  74. adoca.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=9, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
  75. For A = 1 To UBound(fils)
  76. Documents.Open fils(A)
  77. For i = 1 To ActiveDocument.Tables.Count
  78. ActiveDocument.Tables(i).Select'选择表格
  79. Selection.Copy'拷贝
  80. adoca.Tables(1).Select
  81. Selection.PasteAppendTable'追加表格
  82.   Next
  83.   ActiveDocument.Close False'关闭不保存
  84.   Next
  85. End Sub

  1. Private Sub CommandButton1_Click()
  2. On Error GoTo l
  3. Dim i As Table
  4. Label1.Caption = "曾用过的表格样式:"
  5. For Each i In ActiveDocument.Tables
  6. With i
  7. .Style = ComboBox1.Value'选择的样式
  8. End With
  9. i.Select
  10. Next
  11. ListBox1.AddItem i.Style
  12. Exit Sub
  13. l:
  14. MsgBox "此种样式不能用于表格中!"
  15. End Sub
  16. Private Sub CommandButton2_Click()
  17. Call 复制表格到新表格中
  18. End Sub
  19. Private Sub CommandButton3_Click()
  20. On Error GoTo l
  21. Dim i As Table
  22. Label1.Caption = "曾用过的表格字体:"
  23. For Each i In ActiveDocument.Tables
  24. With i
  25. i.Select
  26. Selection.Font.Name = ComboBox3.Value
  27. End With
  28. ListBox1.AddItem ComboBox3.Text
  29. Exit Sub
  30. Next
  31. l:
  32. MsgBox "此种字体不能用于表格中!"
  33. End Sub
  34. Private Sub CommandButton4_Click()
  35. 表格合并
  36. End Sub

  37. Private Sub CommandButton5_Click()
  38. 合并表格
  39. End Sub
  40. Private Sub CommandButton6_Click()
  41. 表格排序
  42. End Sub
  43. Private Sub CommandButton7_Click()
  44. 表格转换为文本
  45. End Sub
  46. Private Sub CommandButton8_Click()
  47. 定制表格样式
  48. End Sub
  49. Private Sub CommandButton9_Click()
  50. 多文档合并表格
  51. End Sub
  52. Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  53. Dim i As Integer
  54. On Error GoTo l:
  55. Select Case Label1.Caption
  56. Case "曾用过的表格样式:"
  57. For i = 0 To ListBox1.ListCount
  58. MsgBox ListBox1.List(i)
  59.         If ListBox1.Selected(i) = True Then
  60.             ComboBox2.AddItem ListBox1.List(i)
  61.         End If
  62.     Next i
  63. Case "曾用过的表格字体:"
  64. Exit Sub
  65. End Select
  66. l:
  67. MsgBox "您没有选择,请选择后再双击鼠标!"
  68. End Sub
  69. Private Sub UserForm_Initialize()
  70. Dim afont
  71. For Each st In ActiveDocument.Styles
  72. ComboBox1.AddItem st
  73. Next
  74. ComboBox1.ListIndex = 1
  75. Label1.Caption = "操作项目:"
  76. Label2.Caption = "表格样式:"
  77. Label3.Caption = "表格字体:"
  78. 'Label4.Caption = "表格边框:"
  79. For Each afont In FontNames
  80. ComboBox3.AddItem afont
  81. Next
  82. ComboBox3.ListIndex = 1
  83. ComboBox3.Font.Italic = True
  84. ListBox1.MultiSelect = fmMultiSelectSingle
  85. TextBox1.Value = ActiveDocument.Tables.Count
  86. End Sub

  1. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long) As Long
  2. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  4. 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
  5. 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
  6. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  7. Private Declare Function SetWindowRgn Lib "user32" (ByVal Hwnd As Long, ByVal A As Long, ByVal bRedraw As Long) As Long
  8. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '延迟时间
  9. Dim Hwnd As Long, A As Long, B As Long
  10. Dim flag As Boolean

  11. Private Sub UserForm_Initialize() '去除标题区与边框
  12. TextBox1.Top = Me.Height - 10
  13.     Hwnd = FindWindow("ThunderDFrame", Me.Caption)
  14.     SetWindowLong Hwnd, -16, GetWindowLong(Hwnd, -16) And Not &HC00000
  15. End Sub
  16. Private Sub UserForm_Activate()
  17. 'Application.Visible = False
  18.     For X = 0 To 40 '其修改40可以百叶窗的数量,从而改变每叶的宽度
  19.         A = CreateRectRgn(0, 0, 0, 0)
  20.         For Y = 0 To Me.Width / 40
  21.             B = CreateRectRgn(Y * 40, 0, Y * 40 + X, Me.Height)
  22.             CombineRgn A, A, B, 3
  23.             DeleteObject B
  24.         Next Y
  25.         SetWindowRgn Hwnd, A, True
  26.         DoEvents
  27.         Sleep 30 '延迟时间,用它决定速度
  28.     Next X
  29.     On Error Resume Next
  30.     flag = True
  31.     Do  '开始循环
  32.         DoEvents '转换控制权,从而展现动画效果
  33.         Sleep 30   '以毫秒为单位延时30个单位
  34.         With TextBox1
  35.             .Top = .Top - 1 '将文字框的上边距减1
  36.             If .Top < -(Me.Height) Then .Top = Me.Height '如果消失的窗体框外就返回重新开始
  37.         End With
  38.     Loop While flag
  39. End Sub
  40. Private Sub UserForm_Click() '单击关闭窗体
  41. Unload Me
  42. Application.Visible = True
  43. UserForm1.Show
  44. End Sub
2楼
biaotiger1
替老大哥补充一个截图。
2012-2-17 20-50-47.jpg
 
3楼
俟人.琳
学习了,谢谢**

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap