Trouble:
During office usage, many annoying copy pastework.for example, there is such text in the.doc file to ensure that multiple files are consistent with the content of a specific information, xiao Bai and Xiao Zhang are responsible for project planning and execution *. when this information is mentioned in the DOC file or multiple Word documents at the same time, this brings about a potential headache, that is, when the person in charge changes, such as a small white because of the sudden situation, what should I do if I cannot be the owner of the entire project? Generally, the staff need to find all the documents about the project owner and do a lot of copy and paste. this is undoubtedly a catastrophic task, and such things happen around us silently. you will find that the flattening rate of this phenomenon is so high. A friend who is engaged in administrative affairs wants to commit suicide.This article records how to liberate office staff from a technical point of view,To make a specific solution, you also need to add other flower shells.
Solution:
There is a saying: where there is a trouble, there is a solution. Lol
OK. Make a joke. to solve the problem above, let's imagine our solution. do you know the template mechanism in office. that is *. dot and *. dotm files. every file on the computer has a template-based derived product, which is usually normal. dotm, in office2010, we can go to the info attribute on the file tab and click show all properties to view the template. dotm, which is the default template of word. if we need to keep all documents away from the trouble mentioned above, you only need to modify the template used to create files in your company.
We will not elaborate on office templates and other things here. This article only provides solutions. to solve this problem, we can take two steps. The first step is to implement replacement, and then cross-file operations.
I. Replacement:
I used to select and use the template method for replacement, for example, <Name> and then use the regular expression in VBA, the regular expression uses <([^ \>] +)>, probably because I amProgramBecause of the clerk, I didn't consider using bookmarks first. Of course, I also considered using bookmarks from the user's perspective. After all, I found a question when using bookmarks, after a replacement, the symbol "<" disappears. finally, after countless struggles, we finally achieved the goal of using bookmarks. We directly thought of using bookmarks to replace them.
Activedocument. bookmarks ("bookmarkname"). range. Text = "Hello World"
However, although the text under bookmarkname is replaced by the magic office, the bookmarkname is missing, that is, it cannot be replaced twice.
After trying to use range, I found that it still cannot be solved.ArticleFinally found the solution,
Dim bmrange as range
Set bmrange = activedocument. bookmarks ("bookmarkname"). Range
Bmrange. Text = "inserted text"
Activedocument. bookmarks. Add _
Name: = "bookmarkname ",_
Range: = bmrange
This sectionCodeCan change youReplace the contents of the bookmarks without deleting them,This is a core function of this solution. OK, the solution is OK.
Ii. Cross-file operations:
Cross-file operations I originally imagined that when we replace other questions, the word program interface will be one by one, and the memory usage will increase step by step, when there are more than 10 files, it will basically crash, but when I practice it, I was surprised to find that,The word application window does not pop up, and the resource usage is not high.Oh, my God, it's nice to replace multiple files so smoothly !!
Next I will post a piece of code for cross-file operations. replacebookmark (location as string, bookmarkname as string, value as string) Where location can be a folder, this method will find all * in this folder *. DOC file (if you save *. docx, which needs to be changedSource code), And then you can replace the content according to the provided bookmarks, And you can replace the content n times. The bookmarks will never be lost.
'Location is like "C: \ Users \ Administrator \ Desktop \ testpro \"
Sub replacebookmark (location as string, bookmarkname as string, value as string)
Dim sfilename as string
Dim Spath as string
Dim wappli as object
Dim wordoc as document
On Error resume next
Spath = Location 'set the folder path
Sfilename = Dir (Spath & "*. Doc") 'specifies the file format in the folder.
Application. screenupdating = false
Set wappli = Createobject ("word. application") 'create a word object
Do While sfilename <> "" 'if the specified file is found
Set wordoc = wappli. Documents. Open (Spath & sfilename) 'Open this file
Set bmrange = wordoc. bookmarks (bookmarkname). Range
Bmrange. Text = Value
Wordoc. bookmarks. Add _
Name: = bookmarkname ,_
Range: = bmrange
Wordoc. save' save the file
Wordoc. Close 'close the file
Sfilename = dir' re-specify the file name
Loop
Set wordoc = nothing 'clear object content
Wappli. Quit
Set wappli = nothing
Application. screenupdating = true
End sub
The above code logic must be correct and may need to be modified, but it should not be a problem. I can pass the test at the company. I just modified it to write an article, I don't know whether to work or not, but the basic things are all here.
Based on the above two steps, we can actually develop more applications to make the process smoother. It may be due to my incomprehension. I found that secondary development of office can really do everything.