Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Insert and fill a table with high-performance [Word]

Insert and fill a table with high-performance [Word]

Scheduled Pinned Locked Moved Visual Basic
helpperformancetutorialcode-reviewlounge
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cherrymotion
    wrote on last edited by
    #1

    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
    
    J 1 Reply Last reply
    0
    • C cherrymotion

      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
      
      J Offline
      J Offline
      Johan Hakkesteegt
      wrote on last edited by
      #2

      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.

      C 1 Reply Last reply
      0
      • J Johan Hakkesteegt

        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.

        C Offline
        C Offline
        cherrymotion
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • C cherrymotion

          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

          J Offline
          J Offline
          Johan Hakkesteegt
          wrote on last edited by
          #4

          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.

          C 1 Reply Last reply
          0
          • J Johan Hakkesteegt

            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.

            C Offline
            C Offline
            cherrymotion
            wrote on last edited by
            #5

            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

            J 1 Reply Last reply
            0
            • C cherrymotion

              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

              J Offline
              J Offline
              Johan Hakkesteegt
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups