Link to home
Start Free TrialLog in
Avatar of Peter Cartledge
Peter Cartledge

asked on

Using Data in Table to auto populate text areas of doc

"I am using MS word. I run a report every month that is generated as a Word Document. The 1st part of the document is a Table, I then need to write an analysis of the Staff member based on Data in the table. Rather than re type the Data I would like to know a way that I can have MSWORD get the data from the table and insert it where I want it. Is this possible? It all happens in the Word document and using EXCEL is not an option. it MUST be MSWord.. Thanks"
Change query
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Mail merge might be a solution. Can we see a non-confidential example of the table and what needs to be done with it?
Avatar of Peter Cartledge
Peter Cartledge

ASKER

indiv
Team
National

qol
95%
88%
90%

aht
1044
1000
1200

adherence
85%
85%
88%






Jim achieved QOL of 95% against Team result of 88% and National Average of 90%  etc
this would be in a table but it did not paste
More careful reading now leads me to rule out mail merge. That would expect one table for all the individuals with one individual per row and in a separate document.

Another possibility is cross-referencing, but that would be as much work setting it up as retyping. It needs a bookmark for each cell in the table and a REF field where it it to be repeated.

That leaves a VBA macro. Are the sentences to be filled in already in the text with some sort of pilot like "<Qol_Team>" or do they have to be typed in their entirety?

This sample code attaches a sentence to the end of the document.

Sub TextLine()
Dim tbl As Table
Dim strName As String
Dim strTextLine As String

strName = "Jim" 'don't know where the name comes from, so it is hard-coded for this example
Set tbl = ActiveDocument.Tables(1)

strTextLine = strName & " achieved QOL of " & GetCellText(tbl.Cell(2, 2)) & _
                        " against Team result of " & GetCellText(tbl.Cell(3, 2)) & _
                        " and National Average of " & GetCellText(tbl.Cell(4, 2)) & vbCr
ActiveDocument.Bookmarks("\EndOfDoc").Range.Text = strTextLine
End Sub


Function GetCellText(cl As Cell) As String
    Dim rng As Range
    
    Set rng = cl.Range
    rng.MoveEnd wdCharacter, -1
    GetCellText = rng.Text
End Function

Open in new window


Could you attach an actual sample document, please? Change any real names for false ones.

First time users often have difficulty attaching files. The sequence is:  Attach file, Browse, Upload File.
Thanks for that. It would be so much easier if people used excel or appropriate file types. An added complexity is I work in a government dept so all macros etc have to go to digital security. When at work tomorrow I will grab a actual document. Thanks to date Peter
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.