VBA Word table with bookmarks-VBForums
Results 1 to 4 of 4

Thread: VBA Word table with bookmarks

  1. #1

    Thread Starter
    Join Date
    Sep 2006

    VBA Word table with bookmarks

    i have a vb6 application that want to write into a Word document. My document is a pre-saved template which has already a table (1 row 3 columns) created in it with a bookmark at the first cell...

    my question is how do i iterate through the cells of my table from my bookmark and create a new row each time i iterate through all 3 cells of the current row.

    i tried oDoc.bookmark("bookname").range.next but it only seems to work if i didnt insert any text in the cell before, other case, it doesnt iterate and write in the same cell

    help please..this is very frustrating

  2. #2

    Thread Starter
    Join Date
    Sep 2006

    Re: VBA Word table with bookmarks


    found something to iterate ..works good but crash when it tries to create a new row


    obWordApp.Selection.Range.Cells(1).Range.Text = "cell1"
    obWordApp.Selection.Range.Cells(1).Range.Text = "cell2"


  3. #3
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Lincoln, NE

    Re: VBA Word table with bookmarks

    What I would do would be to grab a reference to the table from the bookmark. Since the bookmark is in the cell, the tables collection of it's range object will only have one table in it (the one that contains it). From there, instead of working with selections, just reference the table directly. This will be much easier to manipulate. Step through the example code below with the debugger and watch what happens in the Word window. Should be pretty easy to adapt to your needs.
    VB Code:
    1. Private Sub WordTableIE()
    3.     Dim obWordApp As Word.Application, oDoc As Word.Document, oTable As Word.Table, lRow As Long, lCol As Long
    5.     Set obWordApp = New Word.Application
    6.     obWordApp.Visible = True
    8.     'Open the document.
    9.     Set oDoc = obWordApp.Documents.Open("D:\Template.doc")
    10.     'Reference the table.
    11.     Set oTable = oDoc.Bookmarks("bookname").Range.Tables(1)
    13.     With oTable
    14.         'Loop through all cells.
    15.         For lRow = 1 To .Rows.Count
    16.             For lCol = 1 To .Columns.Count
    17.                 'Write a value to the cell.
    18.                 .Cell(lRow, lCol).Range.Text = "Row" & lRow & ", Col" & lCol
    19.             Next lCol
    20.         Next lRow
    21.         'Add a row.
    22.         Call .Rows.Add
    23.         'Find out what row number it is.
    24.         lRow = .Rows.Count
    25.         'Write some values.
    26.         For lCol = 1 To .Columns.Count
    27.             .Cell(lRow, lCol).Range.Text = "New Row, Cell" & lCol
    28.         Next lCol
    29.         'Etc.
    30.     End With
    32.     Set oTable = Nothing
    34.     oDoc.Close (wdDoNotSaveChanges)
    35.     obWordApp.Quit
    37.     Set oDoc = Nothing
    38.     Set obWordApp = Nothing
    40. End Sub

  4. #4
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™

    Re: VBA Word table with bookmarks

    For more info on Word Tables - http://vbforums.com/showthread.php?t=402098

    By eliminating the selection object you are reducing the resources needed and increasing the stability of your macro. The Selection object is a visual physical selection which can be changed by a user making their own selection or messing up the macro by clicking and deselecting the selection causing it to become un-set.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Corsair H100i v2 water cooler, Geforce GTX1060, Samsung M.2 500 GB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2010, VS 2010

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width