R
rxs0569
I wrote a macro that retrieves data from a database, populates a table
using bookmarks and then copies this table and pastes it at the end of
the document before moving to the second row of data.
I am looking for improving the efficiency and performance of this
macro.
1. Is there a better copy and paste algo for tables
2. can i clear the clipboard between each copy and paste. will it
improve the performance.
3. any other suggestions
Some pieces of the macro pasted below
Set oWorkSpace = session.GetWorkSpace
Set oQueryDef = oWorkSpace.GetQueryDef(strQueryName)
' REM execute the query
Set oResultSet = currentsession.BuildResultSet(oQueryDef)
oResultSet.Execute
longNumColumns = oResultSet.GetNumberOfColumns ' no of columns
' Start populating the template table
Row = 1
Status = oResultSet.MoveNext
NumRows = 0
Do While Status = 1
'REm Go to the template table
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1,
Name:=""
Row = Row + 1
Dim strBookName As String
For Column = 2 To longNumColumns
'REM Fill it in the bookmark
strBookName = "field" & (Column - 1) ' field1, filed2 etc
UpdateBookmark strBookName,
CStr(oResultSet.GetColumnValue(Column))
Next
'REM Done with the row, Copy the table and paste it below
Selection.Tables(1).Select
Selection.Copy
Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak
Selection.PasteAndFormat (wdPasteDefault)
' Move Next
Status = oResultSet.MoveNext
NumRows = NumRows + 1
Loop
' Clear the first table
CleanAllBookMarks
Exit Sub
using bookmarks and then copies this table and pastes it at the end of
the document before moving to the second row of data.
I am looking for improving the efficiency and performance of this
macro.
1. Is there a better copy and paste algo for tables
2. can i clear the clipboard between each copy and paste. will it
improve the performance.
3. any other suggestions
Some pieces of the macro pasted below
Set oWorkSpace = session.GetWorkSpace
Set oQueryDef = oWorkSpace.GetQueryDef(strQueryName)
' REM execute the query
Set oResultSet = currentsession.BuildResultSet(oQueryDef)
oResultSet.Execute
longNumColumns = oResultSet.GetNumberOfColumns ' no of columns
' Start populating the template table
Row = 1
Status = oResultSet.MoveNext
NumRows = 0
Do While Status = 1
'REm Go to the template table
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1,
Name:=""
Row = Row + 1
Dim strBookName As String
For Column = 2 To longNumColumns
'REM Fill it in the bookmark
strBookName = "field" & (Column - 1) ' field1, filed2 etc
UpdateBookmark strBookName,
CStr(oResultSet.GetColumnValue(Column))
Next
'REM Done with the row, Copy the table and paste it below
Selection.Tables(1).Select
Selection.Copy
Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak
Selection.PasteAndFormat (wdPasteDefault)
' Move Next
Status = oResultSet.MoveNext
NumRows = NumRows + 1
Loop
' Clear the first table
CleanAllBookMarks
Exit Sub