Insert and fill a table with high-performance [Word]
-
Hello, my aim is to insert a table with 100 rows and 2 cols at the end of a document. Next I have to fill every row with a random font-name in the first column and a formatted text with that random font in the second column. A font-type must not be repeated within the next 10 rows. So here is a code, that does exactly that thing, but I want to find a more performant solution - maybe someone could help me how to optimize it. I know that there are much better ways how to solve this problem, thats why I ask. Thanks in advance for taking the time, cherry
'Set Range to end of document
Dim where As Range
Set where = ActiveDocument.Range(ActiveDocument.Range.End - 1, ActiveDocument.Range.End - 1)'Insert table with 100 rows and 2 cols Set tablewith\_different\_styles = ActiveDocument.Tables.Add(where, 100, 2, \_ DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed) 'Loop 100 times for each Font i = 0 g = 1 Do While i < 5 Do While g < 21 'Got to table row i, col 1 tablewith\_different\_styles.Cell((i \* 20) + g, 1).Select 'Set random font Selection.Font.Name = FontNames(g + 20) 'Save random font Name\_Font = Selection.Font.Name 'reset font for next output Selection.Font.Name = "Courier New" Selection.Font.Size = 9 'Insert font name and iterate to next line Selection.TypeText Text:=Name\_Font Selection.MoveRight Unit:=wdCell 'set font and size Selection.Font.Name = FontNames(g) Selection.Font.Size = 9 Selection.TypeText Text:="My Formatted Text in every Row." g = g + 1 Loop g = 1 i = i + 1 Loop
-
Hello, my aim is to insert a table with 100 rows and 2 cols at the end of a document. Next I have to fill every row with a random font-name in the first column and a formatted text with that random font in the second column. A font-type must not be repeated within the next 10 rows. So here is a code, that does exactly that thing, but I want to find a more performant solution - maybe someone could help me how to optimize it. I know that there are much better ways how to solve this problem, thats why I ask. Thanks in advance for taking the time, cherry
'Set Range to end of document
Dim where As Range
Set where = ActiveDocument.Range(ActiveDocument.Range.End - 1, ActiveDocument.Range.End - 1)'Insert table with 100 rows and 2 cols Set tablewith\_different\_styles = ActiveDocument.Tables.Add(where, 100, 2, \_ DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed) 'Loop 100 times for each Font i = 0 g = 1 Do While i < 5 Do While g < 21 'Got to table row i, col 1 tablewith\_different\_styles.Cell((i \* 20) + g, 1).Select 'Set random font Selection.Font.Name = FontNames(g + 20) 'Save random font Name\_Font = Selection.Font.Name 'reset font for next output Selection.Font.Name = "Courier New" Selection.Font.Size = 9 'Insert font name and iterate to next line Selection.TypeText Text:=Name\_Font Selection.MoveRight Unit:=wdCell 'set font and size Selection.Font.Name = FontNames(g) Selection.Font.Size = 9 Selection.TypeText Text:="My Formatted Text in every Row." g = g + 1 Loop g = 1 i = i + 1 Loop
This looks pretty simple and efficient to me. The only other approach that might make this process faster is by going through XML. Maybe code creating the word file in xml format (but saving it as word) might execute faster.
My advice is free, and you may get what you paid for.
-
This looks pretty simple and efficient to me. The only other approach that might make this process faster is by going through XML. Maybe code creating the word file in xml format (but saving it as word) might execute faster.
My advice is free, and you may get what you paid for.
Thanks for your answer. But it is not what I'm looking for. The problem is: I know this works usually much faster - the table gets inserted and filled in less than 2 seconds - there has to be another way and sadly I'm totally new to VB. I would be happy if someone had another tweak for me... Thx cherry
-
Thanks for your answer. But it is not what I'm looking for. The problem is: I know this works usually much faster - the table gets inserted and filled in less than 2 seconds - there has to be another way and sadly I'm totally new to VB. I would be happy if someone had another tweak for me... Thx cherry
You said, you know that it usually works faster. It might help, if you could explain how you used to do it. What changed?
My advice is free, and you may get what you paid for.
-
You said, you know that it usually works faster. It might help, if you could explain how you used to do it. What changed?
My advice is free, and you may get what you paid for.
Oh, if it would have been me who worked it faster, then I maybe wouldnt ask you ;-) Its kind of a challenge, the fastest makro wins. It not only this table, but this table is the part that costs me the most computation time. I only know, that it would eventually be faster, if you set up the table step by step. But I don't know how to do it best and I don't think of this as a great idea. So far, thanks. cherry
-
Oh, if it would have been me who worked it faster, then I maybe wouldnt ask you ;-) Its kind of a challenge, the fastest makro wins. It not only this table, but this table is the part that costs me the most computation time. I only know, that it would eventually be faster, if you set up the table step by step. But I don't know how to do it best and I don't think of this as a great idea. So far, thanks. cherry
I am not sure if something is getting lost in translation. Do I understand you correctly, the code you posted is written in MS Word's own code editor (i.e. it is VBA), or has it been written in VB.NET using Office.Interop?
My advice is free, and you may get what you paid for.