Rebecca Shabad
asked on
Search and replace in visual basic application using arrays from text file to word document
Find before dollar symbol content(Billion) from text file and then if text found in word document replace it as after dollar symbol content(1,000,000,000) using arrays in visual basic application.
Input text file(.txt) contains below:
Billion $ 1,000,000,000
Million $ 1,000,000
Input text file(.txt) contains below:
Billion $ 1,000,000,000
Million $ 1,000,000
For clarity, can you give a smaple of the text file, as well as the before and after content in the Word document.
ASKER
Text file content:
Billion $ 1,000,000,000
Million $ 1,000,000
Word doc contains Billion and Million text or may be it has any other text. If given text found in word document then it should replace as 1,000,000,000 instead of Billion. If not found should throw an message.
Billion $ 1,000,000,000
Million $ 1,000,000
Word doc contains Billion and Million text or may be it has any other text. If given text found in word document then it should replace as 1,000,000,000 instead of Billion. If not found should throw an message.
Try this macro.
Note that it doesn't 'tidy up' the document. That is to say that it doesn't save or close it afterwards
Note that it doesn't 'tidy up' the document. That is to say that it doesn't save or close it afterwards
Sub ReplaceText()
Dim strTextFile As String 'full path of text and
Dim strWordFile As String 'word files
Dim docText As Document
Dim docWord As Document
Dim para As Paragraph
Dim strSearchWord As String
Dim strReplaceWord As String
strTextFile = "I:\MyFolder\MyTextFile.txt"
strWordFile = "I:\MyFolder\MyWordFile.docx"
Set docText = Documents.Open(strTextFile)
Set docWord = Documents.Open(strWordFile)
For Each para In docText.Paragraphs
strSearchWord = Trim(para.Range.Words.First.Text)
strReplaceWord = Trim(para.Range.Words(3).Text)
With docWord.Range.Find
.Text = strSearchWord
.MatchWholeWord = True
.Replacement.Text = strReplaceWord
If Not .Execute(Replace:=wdReplaceAll) Then
MsgBox strSearchWord & " not found in document"
End If
End With
Next para
docText.Close wdDoNotSaveChanges
End Sub
ASKER
Its working but messagebox not working if text not found and doesn't meet my requirement. Please use array and use Open Statement instead of below code:
Set docText = Documents.Open(strTextFile )
Set docWord = Documents.Open(strWordFile )
Set docText = Documents.Open(strTextFile
Set docWord = Documents.Open(strWordFile
I don't understand how you want it to work. If they aren't already open, then we must open each of the files to get at the data. We need to open the text file in order to get the 'before' and 'after' text. If you want its data to be organised into an array, then that could be done, but why? In my code suggestion it can be used straight away.
What sort Open statement do you have in mind, and how would it be better?
What sort Open statement do you have in mind, and how would it be better?
ASKER
Yes your opinion may be right but i don't need this way my requirement it should be done in array i just learn in using this method via array concept and we can also get the 'before' and 'after' text from text file using like a open statement & input statement. Your opinion may be right but need this way to learning purpose.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Graham -- it does look like homework. You and I have both answered very similar questions in the past few weeks.
ASKER
It does not a homework or any other business concern question its just simple question I just learning about arrays using in this method. However thanks Graham for your suggestion.
Graham's solution clearly does what it the questioner asked for.