我有一个系统可以为我生成测试结果报告。我设法为该系统创建了正确的表格模板。但是,由于某种原因,该报告是在将表按降序排序时生成的,该报告长约950页,充满了表格和图表。我试图使自动报告以不成功的升序输出表。
然后,我开始寻找解决此问题的方法。我拥有的解决方案之一是下面的VBA代码。但是,当我将其应用于整个报表时,它会卡住,Word会变成“无响应”。我对VBA完全陌生,不知道原因。你能告诉我为什么吗?
Attribute VB_Name = "SortTable_Ascend"
Sub Find_Text_in_table()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Step"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
If Selection.Information(wdWithInTable) Then
Selection.Tables(1).SortAscending
End If
Loop
End Sub
顺便说一句,我只查找某些表(其中有一个包含字符串“ Step”的列的表)并对其进行排序。当我只花100页这份文档并应用此脚本时,它就完成了工作,而且没有卡住。
答案 0 :(得分:1)
下面的处理应该更快一些,并将遍历所有表。
Sub Find_Text_in_table()
Dim rng As word.Range, tbl As word.Table
ActiveDocument.ActiveWindow.View.Type = word.WdViewType.wdNormalView
Application.Options.Pagination = False
For Each tbl In ActiveDocument.Tables
Set rng = tbl.Range
rng.Find.ClearFormatting
With rng.Find
.Text = "Step"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
If .found Then
If rng.InRange(tbl.Range) Then
tbl.SortAscending
End If
End If
End With
Next
ActiveDocument.ActiveWindow.View.Type = word.WdViewType.wdPrintView
Application.Options.Pagination = True
End Sub
答案 1 :(得分:1)
这里是一种避免查找和替换单词的替代方法。它还使用更通用的“排序”方法,该方法将复制您在进行单词排序时获得的对话框。如果您想对多列进行排序,这可能会有所帮助。
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button>Go</button>
</form>
<?php
$C = new mysqli("localhost","root","","img");
if(!$C->error) {
echo "Connected";
} else {
echo $C->error;
}
if(isset($_FILES['file'])) {
$F = file_get_contents($_FILES['file']['tmp_name']);
$Q = "insert into image (img) values('$F')";
$R = $C->query($Q);
if($R == true) {
echo "ok";
} else {
echo $C->error;
}
}
?>
经过编辑以修改该子记录,以包括有关屏幕更新,事件和分页的建议(其他人不赞成我)。我还包含了将消息显示在状态栏中(单词窗口的左下角)的代码,该消息将显示进度(y的表x)。我在包含125个表格的文档上测试了以上内容,并且(不对表格进行排序)在大约5秒钟内完成了该操作。
我还纠正了我犯的一个错误
Option Explicit
Sub test()
SortTables_WithKey "Step"
End Sub
Sub SortTables_WithKey(this_key As String)
Dim myIndex As Long
Dim myLastTable As Long
myLastTable = ActiveDocument.Tables.Count
Application.ScreenUpdating = False
Application.Options.Pagination = False
For myIndex = 1 To myLastTable
' MS have deprecated the use of statusbar so if this line
' but it still appears to work in Word 2016
Application.StatusBar = "Table " & CStr(myIndex) & " of " & CStr(myLastTable)
If InStr(ActiveDocument.Tables(myIndex).Range.text, this_key) > 0 Then
' https://docs.microsoft.com/en-us/office/vba/api/word.table.sort
' Replicates the type of sort when done using Word
ActiveDocument.Tables(myIndex).Sort _
excludeheader:=True, _
fieldnumber:=1, _
sortfieldtype:=wdSortFieldAlphanumeric, _
sortorder:=wdSortOrderAscending
End If
DoEvents
Next
Application.ScreenUpdating = True
Application.Options.Pagination = True
End Sub
应该是
sortorder:=wdSortAscending
因此在代码开头添加了“显式选项”。
答案 2 :(得分:1)
尝试:
Sub SortTables()
Application.ScreenUpdating = False
Dim t As Long, bfit As Boolean
With ActiveDocument
For t = 1 To .Tables.Count
With .Tables(t)
If InStr(1, .Range.Text, "Step", 0) > 0 Then
bfit = .AllowAutoFit
If bfit = True Then .AllowAutoFit = False
.SortAscending
If bfit = True Then .AllowAutoFit = True
End If
End With
If t Mod 100 = 0 Then DoEvents
Next
End With
Application.ScreenUpdating = True
End Sub
关闭屏幕更新和表自动调整属性都将提高性能。定期在长时间操作下运行DoEvents也为Word提供了一些喘息的空间。