(被问题卡住了)选择两个节点之间的范围 ,删除此范围之间的数据和者表格,然后插入数据。
选择两个节点之间的范围 ,删除此范围之间的数据或者表格,然后插入数据。
比如:将“作业日志如下"和”作业日志如上"之间的信息删除,然后加入“"hello world"”
------------修改之前卫-----------
作业日志如下
TABLE_A
DATA_B
作业日志如上
-------------修改之后为------
作业日志如下
hello world
作业日志如上
--------思路所得来自于word 帮助代码 ,附上代码 begin --------
Dim rng As Word.Range
If Me.Sentences.Count >= 2 Then
Dim startLocation As Object = Me.Sentences(2).Start
Dim endLocation As Object = Me.Sentences(2).End
' Supply a Start and End value for the Range.
rng = Me.Range(Start:=startLocation, End:=endLocation)
' Select the Range
rng.Select()
End If
--------思路所得来自于word 帮助代码 ,附上代码 end --------
----我的代码 begin------------
---错误见加红出
---问题应该不单出在加红出,可以的话,请调试出正确结果
Sub btnWorkSummaryUpLoad()
Dim objWordApp As Object
Dim objWord As Object
'Dim objWord As Word.Document
Dim myRange As Object
' Dim mySelection As Word.Selection
Dim mySelection As Object
' Dim mySelection As Selection
'范围开始处
Dim startLocation As Object
'范围结束处
Dim endLocation As Object
Set objWordApp = CreateObject("Word.Application")
Set objWord = objWordApp.Documents.Open("d:\TEST.doc")
''''''''''''''''''''''定义范围开始处 begin '''''''''''''''''
Set myRange = objWord.Content
' Debug.Print myRange.Text
myRange.Find.ClearFormatting
myRange.Find.Execute findText:="作业日志如下", Forward:=True
If myRange.Find.Found = True Then
myRange.Select
Set mySelection = objWord.ActiveWindow.Selection
With mySelection
.Collapse Direction:=wdCollapseEnd '光标指向行尾
.TypeParagraph '回车换行
End With
Set startLocation = mySelection.Range
Debug.Print startLocation.Text
End If
''''''''''''''''''''''定义范围开始处 end '''''''''''''''''
Set myRange = Nothing
''' ''''''''''''''''''''''定义范围结束处 begin '''''''''''''''''
Set myRange = objWord.Content
' Debug.Print myRange.Text
myRange.Find.ClearFormatting
myRange.Find.Execute findText:="作业日志如上", Forward:=True
If myRange.Find.Found = True Then
myRange.Select
mySelection = objWord.ActiveWindow.Selection
With mySelection
.Collapse Direction:=wdCollapseStart '光标指向行首
.MoveLeft Unit:=wdWord, Count:=1
End With
Set endLocation = mySelection.Range
Debug.Print endLocation.Text
End If
''''''''''''''''''''''定义范围结束处 end '''''''''''''''''
Set myRange = Nothing
Set myRange = objWord.Range(Start:=startLocation, End:=endLocation)'error 类型不匹配
myRange.Delete
myRange.InsertAfter "hello world"
objWord.Save
objWord.Close
objWordApp.Quit
End Sub
----我的代码 end------------
[解决办法]
数字也不行
我不明白楼主为什么倒来倒去
直接用objWord.Range(Start:=startLocation, End:=endLocation).Delete
试试有没有Delete方法不就完了么?
Sub btnWorkSummaryUpLoad() Dim objWordApp As Object Dim objWord As Object Set objWordApp = CreateObject("Word.Application") Set objWord = objWordApp.Documents.Open("D:\test.doc") Dim sRange As Word.Range, myRange As Word.Range Dim mySelection As Word.Selection Dim s As Long, e As Long '范围开始处,结束处 Set sRange = objWord.Content: Set myRange = objWord.Content sRange.Find.ClearFormatting sRange.Find.Execute findText:="作业日志如下", Forward:=True If sRange.Find.Found = True Then s = sRange.End End If sRange.Find.ClearFormatting sRange.Find.Execute findText:="作业日志如上", Forward:=True If sRange.Find.Found = True Then e = sRange.Start End If myRange.SetRange s, e myRange.Select Set mySelection = objWord.ActiveWindow.Selection mySelection.Delete sRange.InsertAfter Chr(13) & "hello world" Set sRange = Nothing: Set myRange = Nothing objWord.Save objWord.Close objWordApp.QuitEnd Sub