Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create Document objects in Word documents by VB.NET

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces VB.NET how to achieve Word documents to create Document objects, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Creation of VB.NET Word document

1. Create a Document object in Visual Basic

All the work starts with the Document object, first creating an instance of the Document object in Visual Basic, and then you can control the instance in various ways:

Dim newDoc As Word.Document Set newnewDoc = new Word.Document

Once you have created the Document object, you can set the default format of the document by setting the font, line spacing, and other properties of the content sub-object:

With newDoc

.Content.Font.Name = "Song style"

.Content.Font.size = 12

.Content.Paragraphs.LineSpacing = 15.5

End With

This results in an empty Word document.

2. Add text to the Word document

Next, add text to the empty document, which uses the Paragraphs collection object to do this. The InsertAfter method inserts text after the Selection or Range object; the InsertBefore method inserts the text in front of the Selection or Range object. The following code adds a paragraph to the end of the document and formats it:

With newDoc .Content.InsertAfter "sample paragraph …" .Paragraph (NewDoc.Paragraphs.count). Alignment = wdAlignParagraphCenter. Paragraphs (NewDoc.Paragraphs.count). Range.Font.Bold = True. Paragraphs (NewDoc.Paragraphs.count). Range.Font.Size = 28 End With

You can use Paragraphs (index) to return a Paragraph object, where index is the index number; the value of the count attribute indicates the number of Paragraph objects in the main text section of the document. NewDoc.Paragraphs.count happens to be the index number of the text paragraph you are currently adding.

If you need to insert a table, you can use the Table object. The Add method can add a table within the specified range. The following example adds a 3 x 4 table at the beginning of the active document.

Set myRange = ActiveDocument.Range (Start:=0, End:=0) ActiveDocument.Tables.Add Range:=myRange, NumRows:=3, NumColumns:=4

The table can be obtained by using tabulation characters. The following code inserts a 1x4 table at the end of the document.

NewDoc.Content.InsertAfter "score" & vbTab & "& vbTab &" Paper reviewer "& vbTab &" & vbCr

After all the paragraphs have been added to the document, you can use the PageSetup object for page setup, which is done by setting various properties of the PageSetup object.

1) set columns and column spacing:

NewDoc.PageSetup.TextColumns.SetCount NumColumns:=2 newDoc.PageSetup.TextColumns.Spacing = CentimetersToPoints (2)

2) set the margins:

With newDoc.PageSetup. TopMargin = CentimetersToPoints (4.5) .BottomMargin = CentimetersToPoints (1.5) .LeftMargin = CentimetersToPoints (0.8) .RightMargin = CentimetersToPoints (2) End With

3) set the paper size:

With newDoc.PageSetup. PageHeight = CentimetersToPoints (28.1) .PageWidth = CentimetersToPoints (38.9) End With

Other setup items are not discussed in detail.

3. Output document

For the created document, we can choose to save it as a disk file or print it out directly:

NewDoc.SaveAs FileName:= "Sample.doc" newDoc.PrintOut

The test paper generated by this system is required to be directly used in the examination of the examination and selection of grass-roots cadres. First, the parameters of the test paper, that is, the rules for organizing the test paper, are input by the questioner, and then the test paper is automatically generated by the software and output the test paper in Word format. The software system developed by the author using the above methods fully meets the needs of users. The system has been put into operation, which has greatly improved the work efficiency and won the praise of users.

Thank you for reading this article carefully. I hope the article "VB.NET how to create Document objects in Word documents" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report