VB prints the Word file, and VB searches for and replaces the Word file

Source: Internet
Author: User

The previous article mentioned how to add and operate the word component in VB. This article separately discusses the function of printing Word files and searching and replacing in VB.

In printing, many times the word standard template is used, and it is expected to be printed after the template is changed. Many netizens suggest using bookmarks to insert the required content before the bookmarks. However, I think it is more convenient to replace the search.

Since the printing function is extremely simple, the printing function is described first. The Code is as follows:
Dim filename as string 'defines the absolute path of the file to be printed (including the file name)
Filename = "C:/temp.doc"
Dim wodapp as word. Application 'create word component
Set wodapp = new word. Application
Wodapp. application. Documents. Open filename: = filename 'Open the Word file at the specified location
Wodapp. activedocument. printout 'print it using the component printing method.
Wodapp. Quit 'exit the word. (the word process cannot be seen in the Process Manager)
Set wodapp = nothing

The above code can send the word file to the printer. If the printer status does not work, the program will not crash, you can reference the API to query the printer status and handle the problem. As per your needs, we will not describe more here.

The following describes how to search and replace word in VB.
Dim filename as string 'defines the file path
Filename = "C:/temp.doc"
Dim wodapp as word. Application
Set wodapp = new word. Application
Wodapp. application. Documents. Open filename: = filename 'open the file

With wodapp. selection. Find 'to find and replace
. Clearformatting
. Text = "content to be searched"
. Replacement. clearformatting
. Replacement. Text = "content to be replaced"
. Execute Replace: = wdreplaceall, forward: = true, wrap: = wdfindcontinue 'sets the replacement method. (search and replace the entire text)
End with (if you need to replace multiple with blocks, you can use this with block multiple times)

Wodapp. Quit
Set wodapp = nothing

The code above allows you to search and replace Word files. This function can be used to change the required content to a template and add the printing function. You can automatically search, replace, and print the word.

This article is written to this, if you have any questions, please contact QQ: 398175615 Email: jomakming@tom.com

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.