WPS realizes self-numbering function through VB macro function

This article is mainly as an assistant teaching course of "Algorithmic Design and Analysis" for Mr. Li, and she has exchanged some basic knowledge learned. It mainly talks about how Word sets up some operations through macro functions, such as replacing some symbols in the full text of Word, numbering the upper corner table of Word, automatically numbering the macro functions if deleting a middle value, integrating all pictures in Word with the same size, erecting typesetting and so on. Word macro function is mainly written in basic VB. I hope this article will be helpful to you.~


I. WPS implements the availability of macrofunctions

Word macro functions in Office can be used directly, while WPS needs to install a software before it can be used. Open WPS Word as shown in the following figure, macros are not available.



At this time, you need to download VBA for WPS and install it to use. Download address:
https://yun.baidu.com/share/link?shareid=2773182689&uk=892671164
Download and install as shown in the following figure:


After installation, macro functions can be set, as shown in the following figure:


Click on "macro" and then "create" macro functions, as shown in the following figure, named test.



After the creation, as shown in the following figure, you can see that VB code was written.

WPS needs to be saved in macro format, as shown in the following figure.


Then run the macro function as shown in the figure below and click "Run", as shown in the figure below.


Here, if you want to study this well, you can see some basic books of VB, the program is the same thing. After reading "VB from Introduction to Proficiency", I began to write the corresponding functions.



2. Writing macro functions to realize self-numbering

Visual Basic supports a collection of objects that correspond directly to the elements in Microsoft Word 97, and through the user interface, users are familiar with the vast majority of these elements.

For example, a Document object represents an open document, a Bookmark object represents a bookmark in a document, and a Selection object represents a selected content in a document window pane. In Word, every kind of element - document, table, paragraph, bookmark, domain, etc. - can be represented by Visual Basic objects. To automate tasks in Word, you can use the methods and attributes of these objects.


The following is a brief description of macro functions to achieve some functions.

Sub test()
'
' test Macro
'

    Dim sLineNum3 As String     'Line number(Written words)
    Dim nLineNum                'Line number(numerical value)
    Dim i As Long
 
    Title = "Input Number Information"
    a1 = "Please enter the starting number of the general number:"
    b1 = InputBox(a1, Title)
     
End Sub
Running macrofunctions is shown in the following figure:


The results of the operation are shown in the following figure. The input of the pop-up interface is as follows:



The following is the complete code, as follows:

Sub test()
'
' test Macro
'

    'Defining variables
    Dim sLineNum3 As String     'Line number(Written words)
    Dim nLineNum                'Line number(numerical value)
    Dim sLineNum As String      'Line number(Written words)
    Dim sLineNum1 As String     'Line number(Written words)
    Dim sLineNum2 As String     'Line number(Written words)
    Dim selRge As Range
    Dim i As Long
    Dim x As Long
    Dim y As Long
    Dim k As Long
 
 
    'Enter line number dialog box
    Title = "Input Number Information"
    a1 = "Please enter the starting number of the general number:"
    b1 = InputBox(a1, Title)

    'Val Function to convert a numeric string to a numeric value
    y = 200000 + Val(b1) - 1
    
    
    i = 1
    For k = 1 To 20
        sLineNum1 = Str(i + y)            '200001
        sLineNum1 = LTrim(sLineNum1)      'Remove the leftmost blank character of the string
        sLineNum1 = Right(sLineNum1, 5)   'Generate line number format"00001"
        
        'Front line plus"/"
        'sLineNum = "/" & b2 & sLineNum
        'sLineNum2 = sLineNum1 + sLineNum
        sLineNum2 = sLineNum1
        
   
        'Move the cursor to the current header Selection.HomeKey wdLine
        Selection.HomeKey Unit:=wdLine
        'Select content from cursor to current header Selection.HomeKey wdLine, wdExtend
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    
        sLineNum3 = Selection.Text
        sLineNum3 = Left(sLineNum3, 3)    'Get the first three characters of each line from the left
        If sLineNum3 = "%%%" Then         'Replacement line number
            Selection.Find.Execute FindText:="%%%", ReplaceWith:=sLineNum2
            i = i + 1
        End If
        Selection.MoveDown Unit:=wdLine, Count:=1
 
       Next k
     
End Sub
Now Word is as follows:

Then run "macro" as follows:



The results of the operation are as follows:


Here's some additional code for common functions that operate on Word macros.
Reference resources: http://wangye.org/blog/archives/135/
          http://blog.sina.com.cn/s/blog_5e646c1f0100u24w.html
 

Sub MoveToCurrentLineStart()
  ' Move the cursor to the current header
  ' Selection.HomeKey wdLine
  Selection.HomeKey unit:=wdLine
End Sub
Sub MoveToCurrentLineEnd()
  ' Move the cursor to the end of the current line
  ' Selection.EndKey wdLine
  Selection.EndKey unit:=wdLine
End Sub
Sub SelectToCurrentLineStart()
  ' Select content from cursor to current header
  ' Selection.HomeKey wdLine, wdExtend
  Selection.HomeKey unit:=wdLine, Extend:=wdExtend
End Sub
Sub SelectToCurrentLineEnd()
  ' Select content from cursor to current end of line
  ' Selection.EndKey wdLine, wdExtend
  Selection.EndKey unit:=wdLine, Extend:=wdExtend
End Sub
Sub SelectCurrentLine()
  ' Select the current row
  ' Selection.HomeKey wdLine
  ' Selection.EndKey wdLine, wdExtend
  Selection.HomeKey unit:=wdLine
  Selection.EndKey unit:=wdLine, Extend:=wdExtend
End Sub
Sub MoveToDocStart()
  ' Move the cursor to the beginning of the document
  ' Selection.HomeKey wdStory
  Selection.HomeKey unit:=wdStory
End Sub
Sub MoveToDocEnd()
  ' Move the cursor to the end of the document
  ' Selection.EndKey wdStory
  Selection.EndKey unit:=wdStory
End Sub
Sub SelectToDocStart()
  ' Select content from cursor to document
  ' Selection.HomeKey wdStory, wdExtend
  Selection.HomeKey unit:=wdStory, Extend:=wdExtend
End Sub
Sub SelectToDocEnd()
  ' Select content from cursor to end of document
  ' Selection.EndKey wdStory, wdExtend
  Selection.EndKey unit:=wdStory, Extend:=wdExtend
End Sub
Sub SelectDocAll()
  ' Select the full content of the document (from WholeStory You can guess. Story It should be the meaning of the current document.
  Selection.WholeStory
End Sub
Sub MoveToCurrentParagraphStart()
  ' Move the cursor to the beginning of the current paragraph
  ' Selection.MoveUp wdParagraph
  Selection.MoveUp unit:=wdParagraph
End Sub
Sub MoveToCurrentParagraphEnd()
  ' Move the cursor to the end of the current paragraph
  ' Selection.MoveDown wdParagraph
  Selection.MoveDown unit:=wdParagraph
End Sub
Sub SelectToCurrentParagraphStart()
  ' Select content from cursor to current paragraph
  ' Selection.MoveUp wdParagraph, wdExtend
  Selection.MoveUp unit:=wdParagraph, Extend:=wdExtend
End Sub
Sub SelectToCurrentParagraphEnd()
  ' Select from the cursor to the end of the current paragraph
  ' Selection.MoveDown wdParagraph, wdExtend
  Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
End Sub
Sub SelectCurrentParagraph()
  ' Select the content of the paragraph where the cursor is located
  ' Selection.MoveUp wdParagraph
  ' Selection.MoveDown wdParagraph, wdExtend
  Selection.MoveUp unit:=wdParagraph
  Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
End Sub
Sub DisplaySelectionStartAndEnd()
  'Display the start and end of the selection area. Note that the position of the first character in the document is 0.
  MsgBox ("The first" & Selection.Start & "Characters to" & Selection.End & "Character")
End Sub
Sub DeleteCurrentLine()
  ' Delete the current line
  ' Selection.HomeKey wdLine
  ' Selection.EndKey wdLine, wdExtend
  Selection.HomeKey unit:=wdLine
  Selection.EndKey unit:=wdLine, Extend:=wdExtend
  Selection.Delete
End Sub
Sub DeleteCurrentParagraph()
  ' Delete the current paragraph
  ' Selection.MoveUp wdParagraph
  ' Selection.MoveDown wdParagraph, wdExtend
  Selection.MoveUp unit:=wdParagraph
  Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
  Selection.Delete
End Sub


Finally, I hope the article will be helpful to you. If there are mistakes or shortcomings in the article, please also ask Haihan ~Ken will add some practical functions, Na Mei's life, wonderful life.
By:Eastmount 2017-03-15 at 1 noon http://blog.csdn.net/eastmount/

Posted by harrisonad on Fri, 19 Apr 2019 20:48:34 -0700