How to extract specific tables from a MS-Word document using VBA? - vba

VBA is a programming language so I'll assume this question is ok on SO.
What API calls and other techniques can I use, to extract specific tables from an MS-Word document?
I need to write a program which will open several Word documents, and look inside for tables which have a certain text in Row 1 Column 1, and output those tables to another file, preferrably as cells in an Excel spreadsheet.
Is this possible? How would you tackle this? Where to start?
Thanks

The Document.Tables collection contains all tables in a document.
Each Table in this collection has certain properties, such as .Rows, .Columns, or .Cell, which give access to a given row, a given column or a given cell.
The Table.Range.Copy method copies the given table to the clipboard.
The Worksheet.Paste method pastes the copied table into an Excel sheet, using the currently active cell as the insertion point.

Related

Can I copy an ENTIRE Excel sheet and paste to another Excel spreadsheet using Power Automate. (Sharepoint)

I am working from SharePoint.
I would like to COPY AN ENTIRE Excel Sheet-A from Spreadsheet A and Paste it into another Sheet, Sheet-B on Spreadsheet B using Microsoft Power Automate.
Some important things to note:
Sheet-A does not have column names (I know).
There are no Key values or Key Columns. Just random information.
Is there a way to go about this?
Unfortunately PowerAutomate really wants you to have your information in a table if you want to move or manipulate the data and wouldn't it be so useful to have a "Duplicate Sheet" function and copy n paste of individual cells.
If your sheet with the random data happened to be in a single large table, then this may work by apply to each item when navigating the underlying table.

How to create a macro in word to import multiple tables from excel?

Frequently in my job I need to generate reports with lots of tables of inputs and results. Especially for the result tables, one change in analysis may require editing a dozen spreadsheets. I'd like to create a macro in word that pulls in data from a spreadsheet, with each table on it's own tab, so that if I update any of those tables in excel the word document tables will also update. Given the number of tables/data points, I don't want to have to tell the macro to pull each single data point. The aim would be to reduce time and errors from manual entry.
I'm thinking this would involve the following steps, but not sure how to go about them:
1) Define the name/size for each table in word with matching name/size in excel
2) Tell the macro to pull the data into a table format
I'm not sure if this is possible as so far I've only seen how to insert a caption or a text box, not insert or update entire tables. Any help would be greatly appreciated!
Depending on what you're doing, you may not even need any VBA code.
If you copy a range from Excel and paste it into Word using Paste Special with the 'paste link' option, any subsequent changes in the Excel range will automatically be reflected in the document when the workbook is saved. And, if you name the range in Excel before copying/pasting, the Word content will expend/contract to reflect changes in the named range's scope in Excel. A variety of paste formats is supported.
Alternatively, you might use a DATABASE field in Word.

Is there a way to activate Autofilter only on specific columns in VBA?

I have a macro that creates a separate workbook from a larger document. The number of columns vary on how much data is entered. I would like to add an autofilter to only the currently used columns but every time I add an autofilter I have to put in a range and the range varies. Is there a way to write in the code to only add the filter arrow on the applicable columns and not all of them that could possibly be shown? We are sending the document out to a customer and have to manually unfilter the blank boxes which we would prefer not to do.
Thanks!
Turn your data into an Excel Table using the Ctrl + T keyboard shortcut. These have the autofilter built in, and it automatically gets applied to any new columns in the Table, because Tables expand automatically to accommodate any new data.
Furthermore, because Tables are basically Named Ranges that Excel maintains on your behalf, it is very easy to identify the ranges concerned in VBA, as per my answer at VBA coding to identify and clear data in a specific table

Alternative to Excel INDIRECT that works on closed files, and links to workbook based on cell value?

I've seen many questions on this forum about linking Excel files based on cell values and INDIRECT always pops up as an answer, and it does do function and fill my sheet the way that I want, but I need to find a way to work with the source file being closed. My problem when it comes to linking, is that the file path to pull the data from will not be known until part of the file name is entered into a cell.
For example, in 'Print Summary' workbook, Sheet1 Cell A2 is where the file name is entered as a number 12345 (and gets auto-formatted to place 'WIP' in front) which represents WIP12345.xls. WIP12345.xls is a form that holds information that needs to populate certain columns across row 2. WIP12345.xls is an order form and completed days ahead. Once it has been approved, the summary workbook is updated with the WIP#.
I did CONCATENATE WIP12345 and .xls to create the file name WIP12345.xls on Sheet2, and I have a Macro that copies and pastes special as value to turn the result into text. But, I can't find a way to create a formula that will take this value and lookup the file to pull information from. I need to pull and fill information from different cells to 10 columns down 43 rows (each row representing a different WIP#####.xls file).
I'm guessing VBA is the only way to go, but I have no idea how to write it. Anyone have a direction they can point me in? I hope I'm coming across clearly.
The free add-in morefunc.xll contains a function called Indirect.Ext, which works with closed worbooks.

Populate table in Word Template with VBA?

I'm filling in a Word template with data that's been collected from user input. In particular a (variable) number of documents is chosen, and information about each document fills a row of a table.
I've bookmarked several items in the template and successfully filled information in the header from my macro, but the table I'm not so sure with. I bookmarked the first cell and tried tabbing (with Chr(9)) through, and also tried passing an array. (In the template the table has only a first row. Usually tabbing past the last column creates an additional row.)
I can retieve cell contents with
Word.Application.ActiveDocument.Tables(1).Cell(3, 1).Range.Text
but can't write to the any cell except the first, where I placed a bookmark.
Can anyone offer a possible solution to populate the table?
To populate table, use this code
ActiveDocument.Tables(1).Cell(1, 1).Range.Text = "Blah Blah"
This will write to the first cell in the first table. Use a loop to fill the rest of the cells.
I would also recommend see this link.
Topic: Automating Word Tables for Data Insertion and Extraction
Link: http://msdn.microsoft.com/en-us/library/aa537149%28v=office.11%29.aspx#Y1254
Extract:
Summary: Learn how to automate the creation and formatting of tables in Word. Get information about optimizing performance, populating a table with data, formatting table structure, linking table data, and extracting data from a table. (25 printed pages)
I've actually ran into a similar problem using Access to automate filling out a Word table template. I found that if I opened the template in Word prior to running the VBA script, then the Word document is successfully filled out with the table information. My code looks similar to yours as far as adding to it by row. Because the number of fields to be transferred to the form is dynamic it didn't seem like bookmarks for each section would work. If you have any update, I'd be happy to hear of a different way to resolve this.