|
我想写一个给word文档一级二级标题自动插入书签的功能,代码如下:
***********
Sub bookmark()
Dim a As String
Dim strTxt As String
For i = 819 To 820
With Selection
.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=i
.Expand Unit:=wdLine
End With
If Selection.Style = "PRTM Heading 1" Or Selection.Style = "标题 2" Or Selection.Style = "标题 3" Then
strTxt = Selection.Range.Text '获得当前标题的文本
a = Replace(strTxt, " ", "") '报错的语句
'a = “test” '成功的语句
'******建立书签
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:=a
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End If
Next
end sub
*********************
当我用 a = Replace(strTxt, " ", "") 这句代码,就会报错,错误为:“运行时错误 5828:错误的书签名称”,而当用a = “test”这句代码时,就没任何问题。但实际上,这两句中a的内容是完全一样的,均为“test”文本型。
想请教一下问题出在哪里?
|
|