#1
|
|||
|
|||
Data from hidden internal tables feeding listboxes in same Word Document
Hello there,
I'm currently pulling data for my listboxes in an MS Word document from a separate document in Excel. I'd like to pull the data instead from hidden tables within the same Word Document. I can find some answers in the FAQ sections about pulling data from EXTERNAL sources, but not from within. Any ideas to point me in my next right direction would be appreciated. This is the code that I am currently using for one of my listboxes. I use Office 2010 on a Windows 7 machine. Thanks in advance, Mark: Code:
Option Explicit Private Sub UserForm_Initialize() 'Late binding. No reference to Excel Object required. Dim xlApp As Object Dim xlWB As Object Dim xlWS As Object Dim cRows As Long Dim i As Long Dim LR As Long Set xlApp = CreateObject("Excel.Application") 'Open the spreadsheet to get data Set xlWB = xlApp.Workbooks.Open("C:\Users\mark.miller\Desktop\Te1.xltm") Set xlWS = xlWB.Worksheets("Evidence") On Error Goto 0 cRows = xlWS.Range("$A1:$A15").Rows.Count Me.lb1aa.ColumnCount = 1 'Populate the listbox. With Me.lb1aa For i = 2 To cRows 'Use .AddItem property to add a new row for each record and populate column 0 .AddItem xlWS.Range("Ev1a").Cells(i, 1) Next i End With 'Clean up Set xlWS = Nothing Set xlWB = Nothing xlApp.Quit Set xlApp = Nothing lblaa_Exit: Exit Sub End Sub Private Sub cb1aa_Click() Dim i As Long Dim strDocs As Variant For i = 0 To lb1aa.ListCount - 1 If lb1aa.Selected(i) = True Then strDocs = strDocs & lb1aa.List(i) & vbCr End If Next i strDocs = Left$(strDocs, Len(strDocs) - 1) ActiveDocument.Bookmarks("bm1aa").Range _ .InsertBefore strDocs UF1aa.Hide End Sub Last edited by macropod; 02-19-2014 at 03:35 PM. Reason: Added code tags & formatting |
#2
|
||||
|
||||
I suppose the first issue you'll need to address is: How do you propose to hide the table and prevent its deletion?
Once you've resolved that little issue, your 'UserForm_Initialize' sub can be implemented as: Code:
Private Sub UserForm_Initialize() Dim Rng As Range Me.lb1aa.ColumnCount = 1 'Populate the listbox. With ActiveDocument.Tables(#) For i = 2 To .Rows.Count 'Use .AddItem property to add a new row for each record and populate column 0 Set Rng = .Cell(i, 1).Range Rng.End = Rng.End - 1 .AddItem Rng.Text Next i End With End Sub PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks!
That worked like a charm. Thanks for the code posting tip, as well. I finally figured that out
|
Tags |
internal source, listbox, table |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Data transfer from Word tables to Excel | shoro | Word | 4 | 10-01-2013 05:08 AM |
Internal links in Word 2010 | MKR | Word | 2 | 09-27-2013 05:30 PM |
Hidden tables? | dluhop | Word Tables | 1 | 09-04-2011 02:41 AM |
Tables that carry hidden para styles | Ulodesk | Drawing and Graphics | 4 | 03-10-2011 08:16 AM |
*Big One* feeding sql server data into MS Word 2007 forms | vafo | Mail Merge | 0 | 02-03-2010 04:49 AM |