![]() |
|
![]() |
|
Thread Tools | Display Modes |
|
#1
|
||||
|
||||
![]()
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] |
#2
|
|||
|
|||
![]()
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:
From_Blank Line.Text = 10 To_BlankLine.Text = 14 Quote:
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 |
#3
|
||||
|
||||
![]() Quote:
Correct - which is as I originally described. Quote:
Quote:
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] |
#4
|
|||
|
|||
![]()
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:
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:
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:
SMehta Thread 1: No: 46342 : Post No13 : TM 7 |
#5
|
|||
|
|||
![]()
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 SMehta Thread 1: No: 46342 : Post No14 : TM 8 |
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
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 |
![]() |
Noclip1 | Word | 1 | 10-25-2017 10:55 PM |
![]() |
JohnTurnbull | Word | 5 | 08-27-2017 11:17 PM |
![]() |
Robert11 | Word | 3 | 08-12-2013 12:35 PM |