Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-09-2019, 04:06 AM
ganesang ganesang is offline Need help to get data from notepad to word Windows XP Need help to get data from notepad to word Office 2016
Competent Performer
Need help to get data from notepad to word
 
Join Date: Jul 2018
Posts: 171
ganesang is on a distinguished road
Default Need help to get data from notepad to word

Hi Guys

I am back...

I need the macro which helps to get data from notepad or excel to replace in word file.

I have the data in notepad or excel in two columns as follows,

seq-0001 seq-XX0001
seq-0002 seq-XX0010
sup-0010 sup-XX0022
fig-0022 fig-XX0012 etc.....

The first column values found in the word file without any specific order and found more than once in also in several places.



What i need to do is, if i found any first column value in word that needs to be changed as per the second column's respective values vice vara.

Please check and advise on this....
Reply With Quote
  #2  
Old 08-09-2019, 08:30 PM
gmayor's Avatar
gmayor gmayor is offline Need help to get data from notepad to word Windows 10 Need help to get data from notepad to word Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

This is fairly straightforward. You cannot read from Notepad, however you can read from a text file created by Notepad. Assuming the space between the columns in the list is a tab character (as copied from Excel) then the following will find the first part in the body of the document and replace it with the second. If there are other areas to process, you will have to loop through the story ranges to use this code.

Code:
Sub ReplaceFromTextFile()
Dim oRng As Range
Dim sFindText As String, sReplacement As String
Dim iNum As Integer
Dim sData As String
Dim vData As Variant
Const sName As String = "C:\Path\Forums\list.txt"

    iNum = FreeFile()
    Open sName For Input As #iNum

    While Not EOF(iNum)
        Line Input #iNum, sData
        vData = Split(sData, Chr(9))
        Set oRng = ActiveDocument.Range
        sFindText = vData(0)
        sReplacement = vData(1)
        With oRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            Do While .Execute(FindText:=sFindText, _
                              MatchWholeWord:=True, _
                              MatchWildcards:=False, _
                              Forward:=True, _
                              Wrap:=wdFindStop) = True
                oRng.Text = sReplacement
                oRng.Collapse wdCollapseEnd
            Loop
        End With
    Wend
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 08-11-2019, 10:21 PM
ganesang ganesang is offline Need help to get data from notepad to word Windows XP Need help to get data from notepad to word Office 2016
Competent Performer
Need help to get data from notepad to word
 
Join Date: Jul 2018
Posts: 171
ganesang is on a distinguished road
Default

Thanks a lot!!! @gmayor

This one is perfect......
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word does not stop at the set tab, but it keeps forever on the same line like it would in Notepad. danvina@gmail.com Word 2 01-16-2019 02:33 PM
Need help to get data from notepad to word Data format to Notepad k31453 Excel 1 04-18-2017 07:24 AM
Notepad Terminal Font in Word? Elap Word 6 05-24-2016 03:25 AM
Need help to get data from notepad to word i want select some word open in the notepad gsrikanth Office 3 02-25-2012 02:47 PM
Need help to get data from notepad to word How make WORD 2007 act like Notepad WASplawn Word 6 09-27-2011 10:17 AM

Other Forums: Access Forums

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


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