![]() |
|
|||||||
|
|
|
Thread Tools | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Hi
As this is my first post in this forum kindly excuse me for the mistakes while posting a question There are many VBA forums where coding to count lines of text file and return/get Line number positions but was unable to find any coding for counting Nof of blank lines and its position I would like to know the Empty Line numbers of text file so that whatever data is there in text file between 2 or more blank line can paste in to word For eg if i know there are lines with data from Line 4 to 10 therefore Blank Lines in this file would be Line 3 and Line 11. before the below input Any ideas how can i get List of Blank Line nos Input shall be from userform textboxes From_Blank Line.Text To_BlankLine.Text E.G so if i put value 3 in From_Blank Line.Text and 11 in To_BlankLine.Text the lines from text file from 4 to 10 could be pasted Although it is Easy to Open Notepad and word together and paste the content. But i was searching for a code which will help me achieve to what i desried 1. Mehtod Code:
Sub NotePad_to_word()
Dim sourceFile As Object
Dim myFilePath As String
Dim myFileText As String
Dim line As String
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim wrdSelection
myFilePath = "C:\Text-To-MsWord\Sample.txt"
Set sourceFile = fso.OpenTextFile(myFilePath, ForReading)
While Not sourceFile.AtEndOfStream
line = sourceFile.ReadLine
'''''How to get position of all Blank line nos
Wend
sourceFile.Close
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add
Set wrdSelection = wrdApp.Selection
wrdSelection.TypeText ''''' DataAfterFrom in this Eg from Line 4 to Line 10
wrdDoc.Close
wrdApp.Quit
End sub
Thanks SMehta thread 1 : Post 1 |
|
#2
|
||||
|
||||
|
You could, of course, do the whole job in Word. Word can open and process .txt files just as easily as it open and process Word documents. Doing the whole job in Word gives you access to all of Word's tools for processing the .txt file.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Quote:
Then in this case will i be able to count blank lines in .docx files. if yes then How ? Thanks for the option which i had never thought of. SMehta Thread 1: Post 2 |
|
#4
|
||||
|
||||
|
If the aim is simply to write all the non-empty lines to the active document, you could use code like:
Code:
Sub Demo1()
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
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Text = "[^13]"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = "[^13]{2,}"
.Execute Replace:=wdReplaceAll
End With
DocTgt.Range.Characters.Last.Text = .Text
End With
.Close False
End With
Application.ScreenUpdating = True
End Sub
Code:
Sub Demo()
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
|||
|
|||
|
Sir if i may ask what does the following mean
in Demo 1 Code:
.Text = "[^13]{2,}"
.Replacement.Text = "^p"
Code:
.Text = "[^13]{2}[!^13]*[^13]{2}"
.Replacement.Text = "^p"
i got following the error "438" Object doesnt suport this property at DocTgt.Range.Characters.Last.Text = .Text Something happened surprisingly when I ran Demo 2 Everything disappeared from VBA programming enviornment Coding Editor it just displayed Microsoft Visual Basic for Application - Sample and main document White Page from word was also not seen Can you Please check and revert back on Demo 1 and Demo 2 ? SMehta Thread 1: Post 3 |
|
#6
|
||||
|
||||
|
Quote:
Quote:
In both sets of code, the source document isn't visible while being processed. Everything should return to normal once the macro has finished executing.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#7
|
|||
|
|||
|
Quote:
Code:
DocTgt.Range.Characters.Last.Text = .Range.Text Demo 2 still the same effect. Will it be possible to see the effect by making the document visible ? Getting a feel of whats really happening when running the code and effects on document shall clear my doubt And I really wish to know the blank line nos or poped up msgbox with list of blank line nos. Although you mentioned below. Quote:
SMehta Thread 1 Post:4 |
|
#8
|
||||
|
||||
|
Perhaps you could attach representative examples of your document and text file to a post. You can do this via the paperclip symbol, accessed via 'Go Advanced' at the bottom of the posting screen.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#9
|
|||
|
|||
|
Sir, Attaching 2 .txt files for your reference
I've not yet saved the above two files as docx rather opened as txt file in document as you mentioned Quote:
Thread 1: No: 4632 : Post 5 : TM 5 |
|
#10
|
||||
|
||||
|
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] |
|
#11
|
|||
|
|||
|
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 |
|
#12
|
||||
|
||||
|
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] |
|
#13
|
|||
|
|||
|
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 |
|
#14
|
|||
|
|||
|
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 |
|
#15
|
||||
|
||||
|
Since you haven't answered the questions I asked in my previous post, I don't propose to waste any more time on this.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
| 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 |
Microsoft notepad over word for saving important text files
|
Noclip1 | Word | 1 | 10-25-2017 10:55 PM |
Copy text with tab at start of line and paste to Notepad
|
JohnTurnbull | Word | 5 | 08-27-2017 11:17 PM |
Sr. Citizen Question, Please: WORD Shows Blank, But Notepad Shows "Gibberish" ?
|
Robert11 | Word | 3 | 08-12-2013 12:35 PM |