-
-
Save oliverhanappi/dde9609fe324d71ea90b to your computer and use it in GitHub Desktop.
Decreasing Performance of Table Creation in Content Controls in Word 2010
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void OnButtonCreateTablesClick(object sender, EventArgs e) | |
{ | |
var document = Globals.ThisDocument.InnerObject; | |
var stopwatch = Stopwatch.StartNew(); | |
try | |
{ | |
document.Application.ScreenUpdating = false; | |
for (var id = 1; id <= 10; id++) | |
{ | |
CreateTable(document, id); | |
} | |
} | |
catch (Exception exception) | |
{ | |
MessageBox.Show(exception.Message); | |
} | |
finally | |
{ | |
document.Application.ScreenUpdating = true; | |
if (checkBoxClearUndo.Checked) document.UndoClear(); | |
} | |
stopwatch.Stop(); | |
var memorySize = Process.GetCurrentProcess().PrivateMemorySize64 / 1024.0 / 1024.0; | |
listBoxTimes.Items.Add(String.Format("{0:00.000} s\t{1:0.000} MB", stopwatch.Elapsed.TotalSeconds, memorySize)); | |
listBoxTimes.SelectedIndex = listBoxTimes.Items.Count - 1; | |
} | |
private object _oEndOfDoc = "\\endofdoc"; | |
private void CreateTable(Document document, int id) | |
{ | |
ContentControl contentControl; | |
if (document.ContentControls.Count >= id) | |
{ | |
contentControl = document.ContentControls[id]; | |
} | |
else | |
{ | |
var range = document.Bookmarks.get_Item(ref _oEndOfDoc).Range; | |
range.Paragraphs.Add(); | |
range = document.Bookmarks.get_Item(ref _oEndOfDoc).Range; | |
contentControl = document.ContentControls.Add(WdContentControlType.wdContentControlRichText, range); | |
contentControl.SetPlaceholderText(Text: "Table " + id); | |
} | |
var data = ""; | |
var style = 0; | |
var table = document.Tables.Add(contentControl.Range, 30, 15); | |
for (var row = 1; row <= table.Rows.Count; row++) | |
{ | |
for (var column = 1; column <= table.Columns.Count; column++) | |
{ | |
data += String.Format("Table {0}, Cell {1}|{2}", id, row, column); | |
if (column != table.Columns.Count) data += "\t"; | |
} | |
if (row != table.Rows.Count) data += Environment.NewLine; | |
} | |
Clipboard.SetText(data); | |
table.Range.Paste(); | |
for (var row = 1; row <= table.Rows.Count; row++) | |
{ | |
object styleName = String.Format("MyStyle{0}", (style++) + 1); | |
table.Rows[row].Range.set_Style(ref styleName); | |
style = style % 3; | |
} | |
var cellRange = table.Cell(1, 1).Range; | |
var innerTable = cellRange.Tables.Add(cellRange, 3, 3); | |
innerTable.Cell(1, 1).Range.Text = "123"; | |
innerTable.Cell(1, 2).Range.Text = "456"; | |
innerTable.Cell(1, 3).Range.Text = "789"; | |
cellRange = table.Cell(1, 2).Range; | |
innerTable = cellRange.Tables.Add(cellRange, 3, 3); | |
innerTable.Cell(1, 1).Range.Text = "123"; | |
innerTable.Cell(1, 2).Range.Text = "456"; | |
innerTable.Cell(1, 3).Range.Text = "789"; | |
cellRange = table.Cell(1, 3).Range; | |
innerTable = cellRange.Tables.Add(cellRange, 3, 3); | |
innerTable.Cell(1, 1).Range.Text = "123"; | |
innerTable.Cell(1, 2).Range.Text = "456"; | |
innerTable.Cell(1, 3).Range.Text = "789"; | |
} | |
private void OnButtonClearTablesClick(object sender, EventArgs e) | |
{ | |
var document = Globals.ThisDocument.InnerObject; | |
try | |
{ | |
Globals.ThisDocument.Application.ScreenUpdating = false; | |
foreach (ContentControl contentControl in document.ContentControls) | |
{ | |
foreach (Table existingTable in contentControl.Range.Tables) | |
{ | |
existingTable.Delete(); | |
} | |
contentControl.Range.Text = ""; | |
contentControl.Range.set_Style(WdBuiltinStyle.wdStyleNormal); | |
} | |
} | |
finally | |
{ | |
Globals.ThisDocument.Application.ScreenUpdating = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment