以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- 如何把字典中的文件与WORD一一匹配和替换? (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=159252) |
-- 作者:南望 -- 发布时间:2020/12/18 17:13:00 -- 如何把字典中的文件与WORD一一匹配和替换? 想把EXCEL文件中(词典,两列N多行)一 一匹配到WORD,找到符合的就替换,请问题老师下列代码咋改?不会了。 Dim dic As new Dictionary(of String,String) Dim dicfile As String = Forms("WORD工具").Controls("TextBox4").value Dim Book As New XLS.Book(dicfile) \'定义一个Excel工作簿 Dim Sheet As XLS.Sheet = Book.Sheets(0) \'引用工作簿的第一个工作表 For i As Integer = 0 To Sheet.Rows.Count-1 If dic.ContainsKey(Sheet(i, 0).Text.ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) = False Then dic.add(Sheet(i, 0).Text.ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), ""),Sheet(i, 1).Text) End If Next For Each file As String In FileSys.GetFiles(path) Dim Ifo As new FileInfo(file) If (file.EndsWith(".doc") OrElse file.EndsWith(".rtf") OrElse file.EndsWith(".docx") OrElse file.EndsWith(".DOC") OrElse file.EndsWith(".RTF") OrElse file.EndsWith(".DOCX")) AndAlso Ifo.hidden = False Then Dim app As New MSWord.Application try Dim doc = app.Documents.Open(file) \'For Each k As object In Doc.Paragraphs app.Selection.Find.ClearFormatting app.Selection.Find.Replacement.ClearFormatting With app.Selection.Find .Text = "委托合同" .Replacement.Text = "委托合同" & Chr(13) & "WeiTuoHeTong" .Forward = True .Wrap = MSWord.WdFindWrap.wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchByte = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With app.Selection.Find.Execute(Replace:= MSWord.WdReplace.wdReplaceAll) \'app.Visible = True doc.save() app.quit \'Next \'doc.save() \'app.quit catch ex As exception msgbox(ex.message) app.quit End try End If Next [此贴子已经被作者于2020/12/18 17:13:49编辑过]
|
-- 作者:南望 -- 发布时间:2020/12/18 17:14:00 -- 现在单个替换好使,如何根据EXCEL批量替换? |
-- 作者:有点蓝 -- 发布时间:2020/12/18 17:21:00 -- for each key as string in dic.keys app.Selection.Find.ClearFormatting app.Selection.Find.Replacement.ClearFormatting With app.Selection.Find .Text = "委托合同" .Replacement.Text = dic(key) .Forward = True .Wrap = MSWord.WdFindWrap.wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchByte = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With app.Selection.Find.Execute(Replace:= MSWord.WdReplace.wdReplaceAll) next
|