|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
还是给你写一个,附件,打开Excel文件,点击身份证号码!!!- Private Sub Worksheet_SelectionChange(ByVal Target As Range)
- Dim rng As Range, s$, fso As New FileSystemObject, pf$, i As Object
- Dim wdapp As Object, Docx As Object, rg As Object
- Dim odoc As Object
- Set rng = Sheet1.Range("a1:a" & Sheet1.Cells(Rows.Count, 1).End(3).Row)
- If rng.Count = 1 Then MsgBox "没有身份证号码哦!": Exit Sub
- If Intersect(rng, Target) Is Nothing Then
- MsgBox "说好了的点身份证,点其它干嘛呢?": Exit Sub
- Else
- s = Target.Value
- End If
- pf = ThisWorkbook.Path & "\登记表.doc"
- If fso.FileExists(ThisWorkbook.Path & "" & s & ".doc") Then MsgBox "已经有这个文件了,别再点了!": Exit Sub
- Set wdapp = CreateObject("Word.Application")
- wdapp.Visible = False
- Set Docx = wdapp.Documents.Open(pf, Visible:=True)
- With CreateObject("VBScript.Regexp")
- .Pattern = s
- For Each i In Docx.Paragraphs
- For Each mt In .Execute(i.Range)
- m = mt.FirstIndex: n = mt.Length
- Set rg = Docx.Range(i.Range.Start + m, i.Range.Start + m + n)
- Next
- Next
- End With
- If Not rg.Information(12) Then MsgBox "文档表格中无身份证号码!": GoTo 100
- rg.Expand 15: rg.Copy
- Set odoc = wdapp.Documents.Add
- odoc.Content.Paste
- odoc.SaveAs ThisWorkbook.Path & "" & s & ".doc"
- odoc.Close
- 100 Docx.Close
- wdapp.Quit
- MsgBox "操作完毕!"
- End Sub
复制代码
|
|