首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

急 !Cells.Find,参数用Word搜索的Selection.Text,搜索无结果

2012-01-11 
急, 在线等!Cells.Find,参数用Word搜索的Selection.Text,搜索无结果Set rngFind Cells.Find(What:docAp

急, 在线等!Cells.Find,参数用Word搜索的Selection.Text,搜索无结果
Set rngFind = Cells.Find(What:=docApp.Selection.Text, ……
上面代码docApp.Selection.Text监视时发现值是:"#测试#",但结果是Nothing
而下面直接用字符串,就能搜索到,为什么啊。
Set rngFind = Cells.Find(What:= "#测试#", ……

代码如下:
也可以直接下载附件来调试,高手们快来吧,紧要关头了
http://club.excelhome.net/attachment.php?aid=908373&k=4787b4bfc0b669a8b8aeeaff0bb94a29&t=1303094717

Sub Macro2()
'
' Macro2 Macro
'

  Dim myFile As String
  Dim myRange As Range
  Dim docApp As Word.Application
  Dim docRange As Word.Range

  myFile = ThisWorkbook.Path & "\管理报告模板.docx" '指定Word文档
  Set docApp = New Word.Application
  docApp.Documents.Open myFile, Visible:=True
   
With docApp.Selection.Find
.Text = "#"
.Wrap = wdFindContinue
End With
docApp.Selection.Find.Execute
'docApp.Selection.MoveRight Unit:=wdCharacter, Count:=1
'docApp.Selection.TypeParagraph
'docApp.Selection.MoveLeft Unit:=wdCharacter, Count:=1
docApp.Selection.EndKey Unit:=wdLine, Extend:=wdExtend

Dim nic As String
nic = CStr(docApp.Selection.Text)
'myFile = "#测试#"
Dim rngFind As Range
Set rngFind = Worksheets("Sheet1").Cells.Find(What:=nic, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
  :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
  False, MatchByte:=False, SearchFormat:=False)


 If Not rngFind Is Nothing Then

  Range(ActiveCell.Next.Text).Copy
docApp.Selection.PasteExcelTable False, False, False
  End If

End Sub



[解决办法]
感觉你的问题出现在选取##中的内容上
假如word中的#都是成对出现的.
我给你选取##的代码

VB code
Sub this()'' this Macro' 宏在 2011-4-18 选取##中的内容'Dim myrange As RangeDim aStart, aEnd As LongaStart = aEnd = 0Set myrange = ActiveDocument.RangeDo While aEnd <> ActiveDocument.Range.EndWith myrange.Find.Text = "#".ExecuteaStart = myrange.Start.ExecuteEnd WithaEnd = myrange.Endmyrange.SetRange Start:=aStart, End:=aEnd'此时myrangge.text="#测试#"'将word中的内容用excel单元格内容替换掉(你自己写)myrange.SetRange Start:=aEnd, End:=ActiveDocument.Range.EndLoopEnd Sub 

热点排行