+ Reply to Thread
Results 1 to 5 of 5

VBA Macro Excel to Word replace all

Hybrid View

clilley113 VBA Macro Excel to Word... 11-01-2016, 01:08 PM
Neil_ Re: VBA Macro Excel to Word... 11-02-2016, 03:39 AM
Tinbendr Re: VBA Macro Excel to Word... 11-02-2016, 11:00 AM
clilley113 Re: VBA Macro Excel to Word... 11-02-2016, 11:22 AM
Tinbendr Re: VBA Macro Excel to Word... 11-03-2016, 03:11 PM
  1. #1
    Registered User
    Join Date
    11-01-2016
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    2

    Question VBA Macro Excel to Word replace all

    Hi Everyone,

    Does anyone know of simple VBA macro code that I can use to make data in excel cells auto replace phrases in a word document?

    For example if I have data in B:3 and I want it to be input into a word document where the word 'ofice' is. how would I go about doing this.

    or is there an easier way of doing this?

    Any help would be much appreciated.

    Thanks

    Chris

  2. #2
    Valued Forum Contributor Neil_'s Avatar
    Join Date
    04-19-2013
    Location
    Yorkshire
    MS-Off Ver
    Office 365 Enterprise E3 2013 / 2016
    Posts
    479

    Re: VBA Macro Excel to Word replace all

    Open your Word document. Press Ctrl H. Type 'ofice' in find. Type the phrase you want to replace it with in 'replace with'. Replace all. If you must do it by macro, why get Excel involved? Word supports VBA.

    If you want to use abbreviations or auto-correct common mistakes you make, use Word's 'Auto correct' feature in File>Options>Proofing
    Last edited by Neil_; 11-02-2016 at 03:55 AM.
    Frob first, tweak later

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

    Re: VBA Macro Excel to Word replace all

    of simple VBA macro code
    Moderate is how I would describe it.

    Are all the replacements in one Word file?

    Is this like a template Word file?

    A sample file of each would be helpful. (You can upload here. Scroll down to Manage Attachments.)
    David
    (*) Reputation points appreciated.

  4. #4
    Registered User
    Join Date
    11-01-2016
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    2

    Re: VBA Macro Excel to Word replace all

    Thanks Tinbendr.

    I have added the check list I use plus a sample block of text. The report is basically a template where I need to do around 12 'replace all' tasks before I can add my manual wording.

    It would be really handy if I could automate the checklist to do these for me.

    Thanks
    Attached Files Attached Files

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

    Re: VBA Macro Excel to Word replace all

    OK, give this a try.

    Rename the docx file to a dotx file. (Website thinks it's invalid.)

    Change the paths as required.

    Sub CheckListPhraseCreator()
    'Creates a Word document using phrase from this worksheet.
    Dim WS As Worksheet
    Dim Rng As Range
    Dim WrdApp As Word.Application
    Dim WrdDoc As Word.Document
    Dim WrdRng As Word.Range
    'Change second number of first set to total number of phrases.
    Dim Phrase(1 To 5, 1 To 2) As String
    Dim MyPath As String
    Dim MyTmplt As String
    Dim A As Long
    
    Set WS = ActiveSheet
    With WS
        'RETIREMENT LOCATION
        Phrase(1, 1) = "[LOCATION]"
        Phrase(1, 2) = .Range("F3")
        'PENSION SCHEME
        Phrase(2, 1) = "[PENSION]"
        Phrase(2, 2) = .Range("B7")
        'CETV
        Phrase(3, 1) = "[CTEV]"
        Phrase(3, 2) = .Range("D7")
        'YEARS TO NRA
        Phrase(4, 1) = "[YrsTo]"
        Phrase(4, 2) = .Range("H6")
        'DEVERE OFFICE
        Phrase(5, 1) = "[RETLOC]"
        Phrase(5, 2) = .Range("D3")
    End With
    
    'Path to Word Template.  Change as required.
    MyPath = "C:\Users\owner\Mydocuments\"
    'Template name.  Change as required.
    MyTmplt = "CheckList template.dotx"
    If Dir(MyPath & MyTmplt) <> "" Then
        'Start word and make visible.
        Set WrdApp = CreateObject("Word.Application")
        WrdApp.Visible = True
        Set WrdDoc = WrdApp.Documents.Add(MyPath & MyTmplt, , , True)
        
        With WrdDoc
            For A = 1 To 5
                Set WrdRng = WrdDoc.Range
                With WrdRng.Find
                    .Text = Phrase(A, 1)
                    .Replacement.Text = Phrase(A, 2)
                    .Forward = True
                    .Wrap = wdFindContinue
                    .Format = False
                    .MatchCase = False
                    .MatchWholeWord = True
                    .MatchWildcards = False
                    .MatchSoundsLike = False
                    .MatchAllWordForms = False
                End With
                WrdRng.Find.Execute Replace:=wdReplaceAll
            Next
            'Deletes any prior version.
            If Dir(MyPath & WS.Range("b3").Text & ".docx") <> "" Then
                Kill MyPath & WS.Range("b3").Text & ".docx"
            End If
            'Save File using Client name.
            .SaveAs2 MyPath & WS.Range("b3").Text, 16
        End With
        
        'Closes the Word document
        'WrdDoc.Close False
        'Closes Word
        'WrdApp.Quit
        'Releases object
        Set WrdDoc = Nothing
        Set WrdApp = Nothing
    End If
    
    End Sub
    This code is kinda fixed, but easy to understand and modify.

    I would tend to write a table, like sheet2, to handle a situation like this. (See the second macro.)
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Find and Replace in Word document from Excel macro
    By r.vanmoorsel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-25-2015, 04:24 PM
  2. Need a macro for batch find and replace in word using excel
    By ramradha6 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-25-2014, 03:29 AM
  3. [SOLVED] Excel macro- Find replace an itallic font word with a regular word?
    By thisisaboutwork in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-08-2013, 01:48 PM
  4. Need help with Excel-to-Word find and replace macro based on word filename
    By EnterTheSerpent in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-16-2013, 12:48 PM
  5. Word text replace from Excel macro
    By GoDuncan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-18-2011, 01:08 PM
  6. [SOLVED] Find/Replace macro from excel to word
    By Matt in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-31-2006, 10:48 PM
  7. Find/Replace macro from excel into Word
    By Matt in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-28-2006, 11:40 AM

Tags for this Thread

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