Closed Thread
Results 1 to 7 of 7

VBA script to substitute text from an excel sheet into various word docs by filepath

  1. #1
    Registered User
    Join Date
    09-09-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2019
    Posts
    52

    VBA script to substitute text from an excel sheet into various word docs by filepath

    Hi,

    I'd be grateful for some advice on the following VBA requirement I have, which I imagine has been created in one form or another but I've not been able to track it down.

    The company I work for has a word doc template for an inspection report. The user has to edit each report and fill in the data about a property. It would be useful to edit the data in a centralised work sheet, and then have this placed in the respective word documents for each property using either a substitution approach. I don't think mail merge is feasible as the company template is very and formatted with corporate branding etc.

    Is it possible to create a VBA that, when you tell it the filepath of a word document, can substitute a string for another desired string contained in the excel sheet?

    I've attached a zip folder which contains what I am looking to achieve.

    Thank you

    Tom
    Attached Files Attached Files

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: VBA script to substitute text from an excel sheet into various word docs by filepath

    Give this a try.

    Copy the template file to the same directory as the excel file.

    If you edit the template file, be sure to right click open, not double click, since this will create a new file instead.

    I store the path/filename on the sheet instead of navigating to it since I give the user the option to navigate to file location.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    David
    (*) Reputation points appreciated.

  3. #3
    Registered User
    Join Date
    09-09-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2019
    Posts
    52

    Re: VBA script to substitute text from an excel sheet into various word docs by filepath

    Hi David,

    Thanks for the code. It works great but wasn't quite what I was looking for - I don't think I was being clear.

    My company doesn't use templates so I'm more looking for something that will do a substitution of existing word docs based on the items in the square brackets only, and not something that creates new fresh word docs.

    The target word file is just a place for me to refer to the word doc wherever it is sitting on the server, i.e. I don't need the script to make up filenames etc based on the property name as I can deal with this plus the folder path will be a bit irregular.

    I've reattached before and after spreadsheet and word doc which hopefully makes it a bit clearer what I am looking for.

    Thank you

    Tom
    Attached Files Attached Files

  4. #4
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: VBA script to substitute text from an excel sheet into various word docs by filepath

    Quote Originally Posted by tomca View Post
    Hi David,

    My company doesn't use templates
    Never too late to start.

    that will do a substitution of existing word docs based on the items in the square brackets only...
    Ok. After you replace the square brackets (or any string) with the data, how will you find that data again? That was the point of using the template. Every time you create a new document, the brackets will be waiting to be replaced.

    We could use bookmarks, but those are too easy to delete by users.

    We could use formfields, but you have to protect the document in order for the fields to work correctly.

    Maybe content controls as you can set a 'not delete' property for those.

    Wait, are you just trying to grab the data from an already completed file? If so, you might be able to search for the title and grab the next line down.

    The target word file is just a place for me to refer to the word doc wherever it is sitting on the server,
    Ok, I think I have a solution for this.

  5. #5
    Registered User
    Join Date
    09-09-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2019
    Posts
    52
    Quote Originally Posted by Tinbendr View Post
    Never too late to start.


    Ok. After you replace the square brackets (or any string) with the data, how will you find that data again? That was the point of using the template. Every time you create a new document, the brackets will be waiting to be replaced.

    We could use bookmarks, but those are too easy to delete by users.

    We could use formfields, but you have to protect the document in order for the fields to work correctly.

    Maybe content controls as you can set a 'not delete' property for those.

    Wait, are you just trying to grab the data from an already completed file? If so, you might be able to search for the title and grab the next line down.

    Ok, I think I have a solution for this.
    Thanks for the response.

    My intention was to do my working on the excel file and then once I’m happy I execute that macro which does a find and replace - finding the square bracketed stuff and replacing it with the stuff in the cells. That means, if I make subsequent changes to the excel after I’ve ran the find and replace macro, I understand that these won’t then feed into the word document. I.e., this is a one time operation.

  6. #6
    Registered User
    Join Date
    10-22-2016
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    19

    Re: VBA script to substitute text from an excel sheet into various word docs by filepath

    Bumping this thread! I'm looking to do exactly this as well. I wonder if OP ever came to a solution? Would be a huuuuge help!

    Thanks

  7. #7
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,460

    Re: VBA script to substitute text from an excel sheet into various word docs by filepath

    Administrative Note:

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA script to get number of pages of word documents in excel sheet
    By mayurthakor13 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-22-2017, 03:05 AM
  2. converting excel macro into a google docs script??
    By scottatbuckman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2016, 12:47 PM
  3. [SOLVED] Aggregate Batch Data from many word docs into an Excel (i.e.Check Boxes, Text Form Fields)
    By Chrisdudley7 in forum Word Formatting & General
    Replies: 7
    Last Post: 05-13-2014, 01:28 PM
  4. Copying data from multiple word docs into one excel sheet
    By X82 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-15-2013, 07:24 AM
  5. Import cells from excel into word and create multiple word docs
    By scdaddy7269 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-20-2006, 03:03 PM
  6. [SOLVED] Embedding Word Docs into Excel Worksheets and Then Printing The Word Docs
    By mr_melvis in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-07-2005, 10:06 PM
  7. converting excel docs to word docs
    By rolercster in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 01-04-2005, 04:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1