Programming shortcuts to delete extra blank lines in a document

Source: Internet
Author: User
Tags comments empty trim

Daily work in the online found some text material, want to copy down the text, but from the page copy down text in the text editor, there will be a lot of empty lines between paragraphs and paragraphs. If you delete these blank lines manually, it is time-consuming and cumbersome. Several solutions on this issue have been introduced on the Internet, each has its pros and cons, I will introduce the other two ways to write code, more efficient.

One, using Word's macros

If you copy down the text has a continuous line of many lines, or in some empty lines there are a number of unequal space characters, you may wish to let Word macro to a show of skill.

Create a macro named Dbl. The code and comments are as follows:

Sub DBL()
i = 1
Do
'从当前文档的顶端开始逐一选择文档的每一段文字
ActiveDocument.Paragraphs(i).Range.Select
If Trim(Selection.Text) = Chr(13) Then
'如果选择的段落里只有回车符和空格则删除此段
Selection.Delete
Else
'如果选择的段落非空,就将选择的指针移向下一段
Selection.MoveDown
i = i + 1
End If
Loop Until i = ActiveDocument.Parag
raphs.Count
End Sub

After you have written the macro program, you can drag and drop the macro you just built to the shortcut toolbar, do a good job, if you need to copy text from the Web page to edit word, as long as the click of the Macro button, you can instantly delete the entire document empty line.

Second, the use of Web scripting language

If the computer with the word processing tool is Kingsoft WPS series word processing software, and did not install Word and ultraedit software, you might as well try to use Microsoft's Web scripting language (VBScript) to deal with the problem of deleting empty lines. Now most of the machine-mounted operating system is windows, the general office of the machine can be implemented.

The task that the script is going to accomplish is different from the Word macro above. First, paste the copied text from the Web page into a text file (hereinafter referred to as a file) to save. What the script is going to do is to create a text file (hereinafter referred to as B) in the execution of the program, read the text line-by-row from a file, and write the text of the Non-empty line to the B file. In this way, there is no empty line in the B file.

Creates a new text file that changes the file extension to. vbs. Open the file in Notepad and enter the script code, with the code and comments as follows:

Const ForReading = 1, ForWriting = 2, TristateUseDefault = -2
Dim fsoA, fA, tA, fsoB, fB, tB, s, dir
'如果脚本程序和A文件在同一目录下,直接输入A文件的文件名和扩展名即可
dir=inputbox("请输入源文件的路径或相对路径:")
if dir<>"" then
'初始化A文件的对象
Set fsoA = CreateObject("Scripting.FileSystemObject")
Set fA = fsoA.GetFile(dir)
Set tA = fA.OpenAsTextStream(ForRe
ading, TristateUseDefault)
'创建并初始化B文件的对象
Set fsoB = CreateObject("Scripting.FileSystemObject")
fsoB.CreateTextFile("_"+dir)
Set fB = fsoB.GetFile("_"+dir)
Set tB = fB.OpenAsTextStream(ForWr
iting, TristateUseDefault)
'逐行读A文件并把非空行写入B文件
while not tA.AtEndOfStream
s=tA.ReadLine
if trim(s)<>"" then tB.WriteLine s
wend
'关闭文件对象
tB.Close
tA.Close
end if

After the script file code is written, save, and exit Notepad. The following script file can be used to delete the blank line. Use the mouse to double-click the script file to execute the program. When the program is finished, it automatically generates a new file with an underscore as the filename before the a filename, open it and see if the empty line inside is dispelled.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.