Closed Thread
Results 1 to 2 of 2

VBA script to pull various text value from an Excel sheet template into various Word docs

Hybrid View

aftbrah VBA script to pull various... 01-09-2021, 05:43 PM
AliGW Re: VBA script to pull... 01-09-2021, 05:55 PM
  1. #1
    Registered User
    Join Date
    10-22-2016
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    19

    VBA script to pull various text value from an Excel sheet template into various Word docs

    Hi everyone,

    I've been browsing this forum for about 5 years and need some help!

    This is a continuation of an older thread I managed to track down ( https://www.excelforum.com/excel-pro...-filepath.html. )

    Sub CreateSummary()
    
    Dim oWord As Object
    Dim WordWasNotRunning As Boolean
    Dim oDoc As Object
    Dim WS As Worksheet
    Dim FN As Variant
    Dim Phrase$
    Dim Choice As Integer
    
    Set WS = ActiveSheet
    
    'See if Word is already running
    On Error Resume Next
    Set oWord = GetObject(, "Word.Application")
    
    If Err.Number <> 0 Then
        'Launch a new instance of Word
        Err.Clear
        On Error GoTo Err_Handler
        Set oWord = CreateObject("Word.Application")
        WordWasNotRunning = True
        oWord.Visible = True 'Make the application visible to the user (if wanted)
    End If
    
    On Error GoTo Err_Handler
    
    oWord.Visible = True
    oWord.Activate
    Set oDoc = oWord.Documents.Add(ThisWorkbook.Path & "\assessment template.dotx")
    
    With oDoc.Range.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[Property name]"
        Phrase$ = WS.Range("D" & ActiveCell.Row)
        .Replacement.Text = Phrase$
        .Wrap = 0 'wdfindstop
        .Execute Replace:=1 'Word.WdReplace.wdReplaceone
    End With
    With oDoc.Range.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[Comment1]"
        Phrase$ = WS.Range("E" & ActiveCell.Row)
        .Replacement.Text = Phrase$
        .Wrap = 0 'wdfindstop
        .Execute Replace:=1 'Word.WdReplace.wdReplaceone
    End With
    With oDoc.Range.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[Comment2]"
        Phrase$ = WS.Range("F" & ActiveCell.Row)
        .Replacement.Text = Phrase$
        .Wrap = 0 'wdfindstop
        .Execute Replace:=1 'Word.WdReplace.wdReplaceone
    End With
    
    'Parse suggested filename
    Temp = Split(WS.Range("D" & ActiveCell.Row), ",")
    If Temp(0) <> "" Then
        FN = Temp(0)
        oWord.FileDialog(2).InitialFileName = ThisWorkbook.Path & "\" & FN
        Choice = oWord.FileDialog(2).Show
        If Choice <> 0 Then
            oWord.FileDialog(2).Execute
            'Store the path and filename
            WS.Range("G" & ActiveCell.Row) = oDoc.FullName
        End If
    End If
    
    oDoc.Close False
    
    If WordWasNotRunning Then
        oWord.Quit
    End If
    
    'Make sure you release object references.
    
    Set oWord = Nothing
    Set oDoc = Nothing
    Set myDialog = Nothing
    
    'quit
    Exit Sub
    
    Err_Handler:
        MsgBox "Word caused a problem. " & Err.Description, vbCritical, "Error: " _
                & Err.Number
        If WordWasNotRunning Then
            oWord.Quit
        End If
    End Sub
    The solution that user Tinbendr was offering is precisely what I am looking for. However, I tried using the scripts and it doesn't work at all. No word doc was created nor any changes made to the existing word document in his attachment. Wondering if any Excel VBA masters on here can help out with this one? Attachment function on this forum does not work for me for some reason, otherwise I would have attached to this thread.



    Thanks in advance for any help!

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2509 (Windows 11 Home 24H2 64-bit)
    Posts
    92,377

    Re: VBA script to pull various text value from an Excel sheet template into various Word d

    Administrative Note:

    Welcome to the forum.

    Unfortunately, this is a duplicate thread, and you are allowed only ONE thread per issue here.

    Please see Forum Rule #5 about thread duplication.

    I am closing this thread, but you may continue here in the original thread: https://www.excelforum.com/excel-pro...soft-word.html
    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. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    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. Replies: 6
    Last Post: 01-09-2021, 05:57 PM
  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. pull certain data from google docs form-produced spreadsheet to other docs
    By MisterCadillac in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 0
    Last Post: 03-11-2014, 04:49 AM
  5. 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
  6. 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