Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-21-2020, 04:02 AM
nmkhan3010 nmkhan3010 is offline Replacing a string from one doc to another doc Windows 10 Replacing a string from one doc to another doc Office 2016
Novice
Replacing a string from one doc to another doc
 
Join Date: Feb 2020
Posts: 2
nmkhan3010 is on a distinguished road
Post Replacing a string from one doc to another doc

In the below table there are some untranslated words (native text) and i need to replace this strings from another document manually.

Can any one automate this work by VBA..

While running the code it will ask for the source file and next replacing text document and those are to be replaced on the exactly matched native text and it shpuld be colored in yellow color, after replacing we should msg as “Successfully replaced on the exactly matched strings” and finally saved as in RTF format with the same source file name. Is it possible to automate this and please help me in this regards and am thankful to you....

Source file ".docx " ".rtf"
Relacing file ".docx " ".rtf"


Output file ".rtf"

Am attaching attachments please look into this and do the needful....
Attached Files
File Type: docx test.docx (53.3 KB, 2 views)
File Type: docx 131313132(Source).docx (50.9 KB, 2 views)
File Type: docx Fixed version (replacing document).docx (50.4 KB, 2 views)
Reply With Quote
  #2  
Old 02-21-2020, 06:30 AM
macropod's Avatar
macropod macropod is offline Replacing a string from one doc to another doc Windows 7 64bit Replacing a string from one doc to another doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,049
macropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud of
Default

For some code to get you started, try:
Code:
Sub AutoTranslation()
Application.ScreenUpdating = False
Options.DefaultHighlightColorIndex = wdYellow
Dim DocSrc As Document, DocTgt As Document, Tbl As Table, r As Long
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
  .Title = "Select the source document containing the Find/Replace Table"
  .AllowMultiSelect = False
  If .Show = -1 Then
    Set DocSrc = Documents.Open(.SelectedItems(1), ReadOnly:=True, AddToRecentFiles:=False)
  Else
    MsgBox "No source file selected. Exiting", vbExclamation
    Exit Sub
  End If
End With
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
  .Title = "Select the target document to be updated"
  .AllowMultiSelect = False
  If .Show = -1 Then
    Set DocTgt = Documents.Open(.SelectedItems(1), ReadOnly:=False, AddToRecentFiles:=True)
  Else
    MsgBox "No target file selected. Exiting", vbExclamation
    DocSrc.Close SaveChanges:=False
    Set DocSrc = Nothing
    Exit Sub
  End If
End With
Set Tbl = DocSrc.Tables(1)
With DocTgt
  With .Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .MatchWholeWord = True
    .MatchCase = True
    'Process each word from the reference document's first table.
    For r = 2 To Tbl.Rows.Count
      .Text = Split(Tbl.Cell(r, 2).Range.Text, vbCr)(0)
      .Replacement.Text = Split(Tbl.Cell(r, 3).Range.Text, vbCr)(0)
      .Execute Replace:=wdReplaceAll
    Next
  End With
  .SaveAs2 FileName:=Split(.FullName, ".doc")(0) & ".rtf", Fileformat:=wdFormatRTF, AddToRecentFiles:=False
End With
DocSrc.Close SaveChanges:=False
Set Tbl = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing
Options.DefaultHighlightColorIndex = wdNoHighlight
Application.ScreenUpdating = True
MsgBox "Successfully replaced on the exactly matched strings"
End Sub
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 02-25-2020, 12:05 AM
kingsinger kingsinger is offline Replacing a string from one doc to another doc Windows 10 Replacing a string from one doc to another doc Office 2013
Novice
 
Join Date: Feb 2020
Posts: 6
kingsinger is on a distinguished road
Default

It didn't throw an error when I tried it, but I'm not sure that I'm setting things up correctly or that it's doing what I'm was hoping.

I set up one file with the two column table. In column 1 I had three bracketed variables (e.g., [Test 1], [Test 2], etc.). In column 2, I put the values I want to put in place of [Test 1], [Test 2], etc. (e.g., Test 4 and Test 5).

Then in the target document I don't have a table. It's just a regular document with the bracketed variables (e.g., [Test 1]). I was expecting the macro to replace [Test 1] with Test 4. But this didn't happen. It finished and and [Test 1] was still there, as were the other original bracketed variables.

Do I have things set up wrong?

KS
Reply With Quote
  #4  
Old 02-25-2020, 12:24 AM
macropod's Avatar
macropod macropod is offline Replacing a string from one doc to another doc Windows 7 64bit Replacing a string from one doc to another doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,049
macropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud of
Default

Done correctly, it works. Without actually seeing your two documents, I can't be sure what it is you've done.
If, as I suspect, your table is as per the OP's 'Fixed version (replacing document).docx ' attachment (which isn't actually the fixed version), you need to change:
Code:
      .Text = Split(Tbl.Cell(r, 2).Range.Text, vbCr)(0)
      .Replacement.Text = Split(Tbl.Cell(r, 3).Range.Text, vbCr)(0)
to:
Code:
      .Text = Split(Tbl.Cell(r, 1).Range.Text, vbCr)(0)
      .Replacement.Text = Split(Tbl.Cell(r, 2).Range.Text, vbCr)(0)
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 02-25-2020, 01:23 AM
kingsinger kingsinger is offline Replacing a string from one doc to another doc Windows 10 Replacing a string from one doc to another doc Office 2013
Novice
 
Join Date: Feb 2020
Posts: 6
kingsinger is on a distinguished road
Default

Here's what the source document looks like:

Reply With Quote
  #6  
Old 02-25-2020, 01:24 AM
kingsinger kingsinger is offline Replacing a string from one doc to another doc Windows 10 Replacing a string from one doc to another doc Office 2013
Novice
 
Join Date: Feb 2020
Posts: 6
kingsinger is on a distinguished road
Default

Here's what the target document looks like:

Reply With Quote
  #7  
Old 02-25-2020, 03:47 AM
macropod's Avatar
macropod macropod is offline Replacing a string from one doc to another doc Windows 7 64bit Replacing a string from one doc to another doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,049
macropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud ofmacropod has much to be proud of
Default

The modified code works just fine with documents constructed along those lines and selected at the appropriate points when the macro runs.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply

Tags
word replacing

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing a string from one doc to another doc Wildcard replace any string in context with a specified string wardw Word 7 05-07-2018 09:13 AM
Replacing a string from one doc to another doc How can I compare a string in a cell to another string? Amitti Word VBA 2 04-10-2017 07:35 PM
How to find all string withing string. PRA007 Word VBA 18 02-12-2016 08:11 PM
Replacing a string from one doc to another doc Way to search for a string in text file, pull out everything until another string? omahadivision Excel Programming 12 11-23-2013 12:10 PM
Replacing text string within document, it's not retaining formatting livemusic Word 4 02-25-2013 12:33 AM


All times are GMT -7. The time now is 11:23 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft