Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-18-2021, 03:15 PM
macropod's Avatar
macropod macropod is offline Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word Windows 10 Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,379
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I've made some further refinements to the code in post 4 but it mostly already did as I said. Perhaps you could explain what you are trying to achieve. Neither of your attachments makes that apparent.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #2  
Old 01-18-2021, 07:25 PM
SMehta SMehta is offline Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word Windows 10 Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word Office 2013
Novice
Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word
 
Join Date: Jan 2021
Posts: 29
SMehta is on a distinguished road
Default

Sir, I've implemented your refined codes as per your post#4

What is the reason that this time your refined codes worked properly without any issues ?

Your Demo1 procedure pastes the full text file in Word document
Procedure changed from Demo to Demo4
The below code pastes the 2nd paragraph from 1st Blank line uptil the first blank line
after the 2 paragraph or after lines representing as 2nd paragrpah in notepad.

Code:
Sub Demo4()
Application.ScreenUpdating = False
Dim DocSrc As Document, DocTgt As Document
Set DocTgt = ActiveDocument: Set DocSrc = Documents.Open(FileName:="C:\Text-To-MsWord\Sample.txt", AddToRecentFiles:=False, Visible:=False)
With DocSrc
  With .Range
    With .Find
    .Format = False
    .Forward = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
    .Text = "[^13]"
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceAll
      .Wrap = wdFindStop
      .MatchWildcards = True
      .Text = "[^13]{2}[!^13]*[^13]{2}"
      .Replacement.Text = "^p"
      .Execute
    End With
    If .Find.Found = True Then
      DocTgt.Range.Characters.Last.Text = Replace(.Text, vbCr & vbCr, "")
    End If
  End With
  .Close False
End With
Application.ScreenUpdating = True
End Sub
Quote:
Perhaps you could explain what you are trying to achieve. Neither of your attachments makes that apparent.
Now I would like if i put values in following Textboxes From_Blank Line.Text and in To_BlankLine.Text to get the following text Pasted
From_Blank Line.Text = 10
To_BlankLine.Text = 14
Quote:
Jim takes the map to Squire Trelawney and Doctor Livesey who realise that it shows where Captain Flint, an evil and heartless pirate, has buried his stolen treasure. The Squire and the Doctor decide to go and find the treasure and invite Jim to come along. The Squire then buys a ship called the 'Hispaniola' and hires a crew led by the respected Captain Smollet. The ship's cook is a one legged man called Long John Silver whom everyone admires.
The above lines after Blank Line 10 and before Blank line14
So if i get a list of blank lines like from Notepad Structure
Blank lines are at 3
7
10
14
17
20
23

So knowing from blank line position i can paste the lines or paragraph between the blanklines.
would prefer something as From Blank Lines To BlankLines
3 7
7 10
10 14
14 17
17 20
20 23

Sir, in notepad 2nd paragraph shows with 3 lines and in Word Document it shows with 5 lines. I would prefer with notepad structure.

SMehta
Thread 1: No: 46342 : Post 6 : TM 6
Reply With Quote
  #3  
Old 01-20-2021, 03:09 AM
macropod's Avatar
macropod macropod is offline Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word Windows 10 Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,379
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by SMehta View Post
What is the reason that this time your refined codes worked properly without any issues ?
Mainly that the first Find/Replace in the new code fixes an issue with the paragraph breaks in the text file.

Quote:
Originally Posted by SMehta View Post
Your Demo1 procedure pastes the full text file in Word document
Correct - which is as I originally described.
Quote:
Originally Posted by SMehta View Post
Procedure changed from Demo to Demo4
The below code pastes the 2nd paragraph from 1st Blank line uptil the first blank line
after the 2 paragraph or after lines representing as 2nd paragrpah in notepad.
Correct.
Quote:
Originally Posted by SMehta View Post
Now I would like if i put values in following Textboxes From_Blank Line.Text and in To_BlankLine.Text to get the following text Pasted
From_Blank Line.Text = 10
To_BlankLine.Text = 14
It's still not clear what the rules are for what gets pasted where. Your Sample.txt file has 7 paragraphs, whilst your Sample2.txt file has 15 groups of 5 paragraphs.

For your Sample2.txt file, what are the rules for which blocks from get copied and what are the rules for where those blocks get inserted into your Sample.txt file?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 01-20-2021, 09:07 AM
SMehta SMehta is offline Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word Windows 10 Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word Office 2013
Novice
Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word
 
Join Date: Jan 2021
Posts: 29
SMehta is on a distinguished road
Default

Sir with your corrected in Sub Demo which i changed to Sub Demo 4 for my understanding.
For both files I've not opened them through MS-Word it is Notepad

Word document is blank with complete Empty White page

My Apologies for incorporating word as "Pasted" in thread and few posts rather than word Copied or Displayed. I am sorry

Sir, From both the files the following has been copied after the first Blank Line after you corrected version of Sub Demo
the follwoing has been the output.

Sir in sample2. text
Text has been copied from Line 7 to Line 11 which means Line 6 and Line 12 are blank
the following has been displayed/Copied in Plain Word Document
Quote:
2
"VP75X"
"SURGI*II 5/0 24 BLUE CV-11 D/A"
36
12288.43
and from Sample.text
Text has been copied from Line 4 to Line 6 which means Line 3 and Line 7 are blank
the following has been displayed/Copied in Plain Word Document
Quote:
The story begins at 'The Admiral Benbow', the inn that belongs to Jim Hawkin's parents. A mysterious stranger called Billy Bones, who rents a room at the inn, warns Jim to keep a look out for a 'one legged man'. One day, Billy is visited by a beggar called 'Blind Pew' who gives him the 'black spot' which is the mark of imminent death among pirate crews. After Blind Pew leaves, Billy collapses and dies.
In both file we have blank lines after each Paragraph or group of non-empty lines with data
in Sample.txt we have story type paragraph.
in Sample2.txt we have structure with [sr no , code, item description, unit and Rate] somewhat like a record set
in above both the case there are blank lines between each Paragraph or group of Non-Empty Lines

Sir with your help and guidance
A genric code is required to get list of Blank line before and after each paragraph or non-Empty lines for a txt file opened in Notepad
For Sample.txt i would like to have the list of Empty Lines
ie
From_Blank Line To_BlankLine
3 7
7 10
10 14
14 17
17 20
20 23

If i put value 10 in textbox:From_BlankLine and value 14 in Text box: To_blank line
Then in Word Document it should display the following text with Lines from 11 to 13 of Notepad
Quote:
Jim takes the map to Squire Trelawney and Doctor Livesey who realise that it shows where Captain Flint, an evil and heartless pirate, has buried his stolen treasure. The Squire and the Doctor decide to go and find the treasure and invite Jim to come along. The Squire then buys a ship called the 'Hispaniola' and hires a crew led by the respected Captain Smollet. The ship's cook is a one legged man called Long John Silver whom everyone admires.
For setting rules you can guide me

SMehta
Thread 1: No: 46342 : Post No13 : TM 7
Reply With Quote
  #5  
Old 01-23-2021, 01:08 AM
SMehta SMehta is offline Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word Windows 10 Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word Office 2013
Novice
Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word
 
Join Date: Jan 2021
Posts: 29
SMehta is on a distinguished road
Default

Sir I tried all together new method to achieve partly to what was desired.
by using FSO method. I thought the below coding would help me at least get list of Blank Line nos
somehow not successful

I get Subscript out of Range Error

If some idea could be implemented on how to define and set the wildcard characters for blank paragraph
txtToSrch ="[^13]" or "^p" or "[^13]{2}[!^13]*[^13]{2}"

Else if
txtToSrch ="Jim" then there is no Subscript out of Range Error and it shows word Jim in Lines

FYI I've added in reference Microsoft Scripting Runtime as this was required after exploring through few VBA forums. Below code is also implemented after exploring VBA forums

Code:
Option Explicit

Public Type SearchResults
    BlankLineNumber As Long
    CharPosition As Long
    StrLen As Long
End Type

Sub Example()
    Dim MySearch() As SearchResults, i As Long, txtToSrch As String
    
    txtToSrch = "[^13]"
    
    MySearch = GetSearchResults_2("C:\Text-To-MsWord\Sample.txt", txtToSrch, vbTextCompare)
    
    For i = 0 To UBound(MySearch)
        With MySearch(i)
            MsgBox "Blank Lines Are At " & vbCrLf & .BlankLineNumber
        End With
    Next
    
End Sub

Function GetSearchResults_2(FileFullName As String, FindThis As String, Optional CompareMethod As VbCompareMethod = vbBinaryCompare) As SearchResults()
  
    Dim fso As New FileSystemObject, s As String, pos As Long, l As Long, sr As SearchResults, ret() As SearchResults, i As Long
    Dim blnkLineNo As Long
    
    With fso.OpenTextFile(FileFullName)
        Do Until .AtEndOfStream
              l = l + 1
              s = .ReadLine
            pos = 1
            Do
                pos = InStr(pos, s, FindThis, CompareMethod)
                If pos > 0 Then
                    sr.CharPosition = pos
                    sr.BlankLineNumber = l
                    sr.StrLen = Len(FindThis)
                    
                    ReDim Preserve ret(i)
                    ret(i) = sr
                    i = i + 1
                    pos = pos + 1
                End If
            Loop Until pos = 0
        Loop
    End With
    GetSearchResults_2 = ret
End Function
Will await your inputs, corrections and guidance

SMehta
Thread 1: No: 46342 : Post No14 : TM 8
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help to get data from notepad to word ganesang Word VBA 2 08-11-2019 10:21 PM
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
Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word Microsoft notepad over word for saving important text files Noclip1 Word 1 10-25-2017 10:55 PM
Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word Copy text with tab at start of line and paste to Notepad JohnTurnbull Word 5 08-27-2017 11:17 PM
Notepad to Word : Pasting Specific text ie between any two blank line from notepad to word Sr. Citizen Question, Please: WORD Shows Blank, But Notepad Shows "Gibberish" ? Robert11 Word 3 08-12-2013 12:35 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:15 PM.


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