Excel VBA - modify a macro to hard code file and ranges

Dont Call me Betty

New Member
Joined
Sep 29, 2023
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Below code resides in Excel file and allows to manually pick a Word file, then manually select two ranges of cells, then does find/replace in the picked Word file

Need to hard code into it:

a) specific file (XYZ.doc) from the same directory Excel file with this code is located
b) the two specific cell ranges: B5:B17 and C5:C17

in order to not to have to manually pick the Word file and manually select those ranges every time, but so that they are picked automatically

VBA Code:
Sub replace_texts_range_of_cells_HRD()
'Update by
Dim xWordApp As Word.Application
Dim xDoc As Word.Document
Dim xRng As Range
Dim I As Integer
Dim xFileDlg As FileDialog
On Error GoTo ExitSub
Set xFileDlg = Application.FileDialog(msoFileDialogFilePicker)
xFileDlg.AllowMultiSelect = False
xFileDlg.Filters.Add "Word Document", "*.docx; *.doc; *.docm"
xFileDlg.FilterIndex = 2
If xFileDlg.Show <> -1 Then GoTo ExitSub
Set xRng = Application.InputBox("Please select the lists of find and replace texts (Press Ctrl key to select two same size ranges):", "XXXX says", , , , , , 8)
If xRng.Areas.count <> 2 Then
  MsgBox "Please select two columns (press Ctrl key), the two ranges have the same size.", vbInformation + vbOKOnly, "XXXX says"
  GoTo ExitSub
End If
If (xRng.Areas.Item(1).Rows.count <> xRng.Areas.Item(2).Rows.count) Or _
  (xRng.Areas.Item(1).Columns.count <> xRng.Areas.Item(2).Columns.count) Then
  MsgBox "Please select two columns (press Ctrl key), the two ranges have the same size.", vbInformation + vbOKOnly, "XXXX says"
  GoTo ExitSub
End If
Set xWordApp = CreateObject("Word.application")
xWordApp.Visible = True

Set xDoc = xWordApp.Documents.Open(xFileDlg.SelectedItems.Item(1))
For I = 1 To xRng.Areas.Item(1).Cells.count
  With xDoc.Application.Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = xRng.Areas.Item(1).Cells.Item(I).Value
    .Replacement.Text = xRng.Areas.Item(2).Cells.Item(I).Value
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchByte = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  xDoc.Application.Selection.Find.Execute Replace:=wdReplaceAll
Next
ExitSub:
  Set xRng = Nothing
  Set xFileDlg = Nothing
  Set xWordApp = Nothing
  Set xDoc = Nothing
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try this:

VBA Code:
Sub replace_texts_range_of_cells_HRD()
  Dim xWordApp As Word.Application
  Dim xDoc As Word.Document
  Dim rng1 As Range, rng2 As Range
  Dim i As Long
  Dim specificFile As String
  
  specificFile = ThisWorkbook.Path & "\" & "xyz.docx"
  Set rng1 = ActiveSheet.Range("B5:B17")
  Set rng2 = ActiveSheet.Range("C5:C17")
  
  Set xWordApp = CreateObject("Word.application")
  xWordApp.Visible = True
  Set xDoc = xWordApp.Documents.Open(specificFile)
  For i = 1 To rng1.Cells.Count
    With xDoc.Application.Selection.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = rng1.Cells(i).Value
      .Replacement.Text = rng2.Cells(i).Value
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchByte = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
    End With
    xDoc.Application.Selection.Find.Execute Replace:=wdReplaceAll
  Next
End Sub


Regards
Dante Amor
 
Upvote 0
Try this:

VBA Code:
Sub replace_texts_range_of_cells_HRD()
  Dim xWordApp As Word.Application
  Dim xDoc As Word.Document
  Dim rng1 As Range, rng2 As Range
  Dim i As Long
  Dim specificFile As String
 
  specificFile = ThisWorkbook.Path & "\" & "xyz.docx"
  Set rng1 = ActiveSheet.Range("B5:B17")
  Set rng2 = ActiveSheet.Range("C5:C17")
 
  Set xWordApp = CreateObject("Word.application")
  xWordApp.Visible = True
  Set xDoc = xWordApp.Documents.Open(specificFile)
  For i = 1 To rng1.Cells.Count
    With xDoc.Application.Selection.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = rng1.Cells(i).Value
      .Replacement.Text = rng2.Cells(i).Value
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchByte = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
    End With
    xDoc.Application.Selection.Find.Execute Replace:=wdReplaceAll
  Next
End Sub


Regards
Dante Amor
That works well, Thank you! Last thing - How do I then:
a) save that word file into subdirectory named ABC of the same folder where the Excel file containing this macro resides
b) with a file name from cell Z6?
 
Upvote 0
a) save that word file into subdirectory named ABC of the same folder where the Excel file containing this macro resides
b) with a file name from cell Z6?
Try:

VBA Code:
Sub replace_texts_range_of_cells_HRD()
  Dim xWordApp As Word.Application
  Dim xDoc As Word.Document
  Dim rng1 As Range, rng2 As Range
  Dim i As Long
  Dim specificFile As String, sFolder As String, sFile As String
  
  specificFile = ThisWorkbook.Path & "\" & "xyz.docx"
  Set rng1 = ActiveSheet.Range("B5:B17")
  Set rng2 = ActiveSheet.Range("C5:C17")
  
  Set xWordApp = CreateObject("Word.application")
  xWordApp.Visible = True
  Set xDoc = xWordApp.Documents.Open(specificFile)
  For i = 1 To rng1.Cells.Count
    With xDoc.Application.Selection.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = rng1.Cells(i).Value
      .Replacement.Text = rng2.Cells(i).Value
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchByte = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
    End With
    xDoc.Application.Selection.Find.Execute Replace:=wdReplaceAll
  Next
  
  'save document
  sFolder = ThisWorkbook.Path & "\" & "ABC" & "\"
  sFile = ActiveSheet.Range("Z6").Value
  xWordApp.ActiveDocument.SaveAs sFolder & sFile & ".docx"
  xWordApp.Quit     'quit word
End Sub

🫡
Dante Amor
 
Upvote 1
Solution

Forum statistics

Threads
1,215,126
Messages
6,123,200
Members
449,090
Latest member
bes000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top