Vba 删除项目后删除回车
我正在以编程方式从目录中删除项目,但不会删除回车符,因此我收到一个带有空格的目录。 我的代码有一小段:Vba 删除项目后删除回车,vba,ms-word,Vba,Ms Word,我正在以编程方式从目录中删除项目,但不会删除回车符,因此我收到一个带有空格的目录。 我的代码有一小段: For i = cntContentsFields To bngAppendixArray(1) step-1 selection.Fields(i).Delete Next 我正在研究word 2007 以下是完整的代码: On Error GoTo ErrHndl Dim i As Integer Dim iStep
For i = cntContentsFields To bngAppendixArray(1) step-1
selection.Fields(i).Delete
Next
我正在研究word 2007
以下是完整的代码:
On Error GoTo ErrHndl
Dim i As Integer
Dim iStep As Integer
Dim ipos As Integer
Dim ipos_2 As Integer
Dim cntTables As Integer
Dim myFontSize As Integer
Dim cntWords As Integer
Dim cntEnglishWords As Integer
Dim cntContentsFields As Integer
Dim cntContentsSeif As Integer
Dim lneFeedPos As Integer
Dim strContents As String
Dim bgnAppendixArray() As Integer
Dim arrIndex As Integer
With ActiveDocument
If .Range.LanguageID = wdEnglishUS Then
gDocLang = wdEnglishUS
Else
gDocLang = wdHebrew
End If
End With
If ActiveDocument.TablesOfContents.Count >= 1 Then
ActiveDocument.TablesOfContents(1).Range.Select
ActiveDocument.TablesOfContents(1).Update
Else
Selection.EndKey Unit:=wdLine
Selection.TypeParagraph
Selection.Range.Style = ActiveDocument.Styles(wdStyleNormal)
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
myFontSize = Selection.Font.SizeBi + 1
If gDocLang = wdEnglishUS Then
Selection.Font.Size = 14
Selection.TypeText Text:="Index"
Else
Selection.TypeText Text:="???? ????????"
End If
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.SizeBi = Selection.Font.SizeBi + 2
Selection.Font.Bold = True
Selection.Font.BoldBi = True
Selection.EndKey Unit:=wdLine
Selection.TypeParagraph
Selection.Range.Style = ActiveDocument.Styles(wdStyleNormal)
Selection.TypeParagraph
With ActiveDocument
.TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
LowerHeadingLevel:=1, IncludePageNumbers:=True, AddedStyles:="", _
UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _
False
.TablesOfContents(1).TabLeader = wdTabLeaderSpaces
End With
End If
For cntTables = 1 To ActiveDocument.TablesOfContents.Count
ActiveDocument.TablesOfContents(cntTables).Range.Select
Selection.Range.Style = ActiveDocument.Styles("TOC 1")
If gDocLang = wdEnglishUS Then Selection.LtrPara
Next
ActiveDocument.TablesOfContents(1).Range.Select
cntContentsFields = Selection.Fields.Count
iStep = 2
arrIndex = 0
cntContentsSeif = 0
strContents = Selection.Fields(1).Result
lneFeedPos = InStr(1, Selection.Fields(1).Result, Chr(13))
Do While lneFeedPos > 0
cntContentsSeif = cntContentsSeif + 1
strContents = Mid(strContents, lneFeedPos + 1)
lneFeedPos = InStr(1, strContents, Chr(13))
Loop
If cntContentsSeif * 2 <> cntContentsFields - 1 Then GoTo DocumentfromW2000
For i = 4 To cntContentsFields - 1 Step 2
iStep = i
ipos = InStr(1, Selection.Fields(iStep).Result, Chr(46))
ipos_2 = InStr(1, Selection.Fields(iStep - 2).Result, Chr(46))
If ipos <= 1 Then
MsgBox CONTENT_ERR_MSG, vbInformation
GoTo DocumentfromW2000
Else
If Not IsNumeric(Mid(Selection.Fields(iStep).Result, 1, ipos - 1)) Then
MsgBox CONTENT_ERR_MSG, vbInformation
GoTo DocumentfromW2000
End If
End If
On Error GoTo DocumentfromW2000
If CInt(Mid(Selection.Fields(iStep).Result, 1, ipos - 1)) < CInt(Mid(Selection.Fields(iStep - 2).Result, 1, ipos_2 - 1)) Then
ReDim Preserve bgnAppendixArray(arrIndex)
bgnAppendixArray(arrIndex) = iStep
arrIndex = arrIndex + 1
End If
Next
If arrIndex > 1 Then
For i = cntContentsFields To bgnAppendixArray(1) Step -1
Selection.Expand wdSentence
Selection.Fields(i).Delete
Next
End If
If arrIndex > 0 Then
For i = bgnAppendixArray(0) - 1 To 2 Step -1
Selection.Expand wdSentence
Selection.Fields(i).Delete
Next
End If
Exit Sub
DocumentfromW2000:
Exit Sub
ErrHndl:
MsgBox "ERROR: " + CStr(Err.Number) + " - " + Err.DESCRIPTION, vbCritical
错误转到ErrHndl
作为整数的Dim i
Dim iStep作为整数
将IPO作为整数进行调整
作为整数的Dim ipos_2
将表设置为整数
将myFontSize设置为整数
将单词设置为整数
将英文单词设置为整数
将cntContentsFields设置为整数
Dim cntContentsSeif作为整数
作为整数的Dim lneFeedPos
作为字符串的Dim STRCONTENT
Dim bgnAppendixArray()作为整数
将索引设置为整数
使用ActiveDocument
如果.Range.LanguageID=wd,则
gDocLang=wdEnglishUS
其他的
gDocLang=希伯来语
如果结束
以
如果ActiveDocument.TablesOfContents.Count>=1,则
ActiveDocument.TablesOfContents(1).Range.Select
ActiveDocument.TablesOfContents(1).更新
其他的
Selection.EndKey单位:=wdLine
选择.类型段落
Selection.Range.Style=ActiveDocument.Style(wdStyleNormal)
Selection.ParagraphFormat.Alignment=wdAlignParagraphCenter
myFontSize=Selection.Font.SizeBi+1
如果gDocLang=wd,则
Selection.Font.Size=14
Selection.TypeText文本:=“索引”
其他的
Selection.TypeText文本:=“??”
如果结束
Selection.HOME键单位:=wdLine,Extend:=wdExtend
Selection.Font.SizeBi=Selection.Font.SizeBi+2
Selection.Font.Bold=True
Selection.Font.BoldBi=True
Selection.EndKey单位:=wdLine
选择.类型段落
Selection.Range.Style=ActiveDocument.Style(wdStyleNormal)
选择.类型段落
使用ActiveDocument
.TablesOfContents.Add范围:=选择范围,右对齐页码:=_
True,UseHeadingStyles:=True,UpperHeadingLevel:=1_
LowerHadingLevel:=1,IncludeAgentNumber:=True,AddedStyles:=“”_
UseHyperlinks:=True,HidePageNumbersInWeb:=True,UseOutlineLevel:=_
错误的
.TablesOfContents(1).TabLeader=wdtableaderspace
以
如果结束
对于cntTables=1到ActiveDocument.TablesOfContents.Count
ActiveDocument.TablesOfContents(cntTables).Range.Select
Selection.Range.Style=ActiveDocument.Style(“TOC 1”)
如果gDocLang=wdEnglishUS,则Selection.LtrPara
下一个
ActiveDocument.TablesOfContents(1).Range.Select
cntContentsFields=Selection.Fields.Count
iStep=2
指数=0
cntContentsSeif=0
strContents=Selection.Fields(1.Result)
lneFeedPos=InStr(1,选择字段(1)。结果,Chr(13))
当lneFeedPos>0时执行此操作
cntContentsSeif=cntContentsSeif+1
strContents=Mid(strContents,lneFeedPos+1)
lneFeedPos=仪器(1,结构内容,Chr(13))
环
如果cntContentsSeif*2 cntContentsFields-1,则从W2000转到文档
对于i=4到cntContentsFields-1步骤2
iStep=i
ipos=InStr(1,选择字段(iStep).Result,Chr(46))
ipos_2=InStr(1,选择字段(iStep-2)。结果,Chr(46))
如果IPO是1,那么
对于i=cntContentsFields到bgnAppendixArray(1)步骤-1
选择。展开句子
选择.字段(i).删除
下一个
如果结束
如果索引>0,则
对于i=bgnappendix阵列(0)-1到2步骤-1
选择。展开句子
选择.字段(i).删除
下一个
如果结束
出口接头
来自W2000的文档:
出口接头
ErrHndl:
MsgBox“错误:+CStr(错误编号)+”-“+Err.DESCRIPTION,vbCritical
试试:
在删除之前。请尝试:
删除前。回车未删除,删除的项目之间有空格。还有其他想法吗?没有,没有更多的代码和说明Word的哪个版本。回车没有删除,删除的项目之间有空格。还有其他想法吗?没有,没有更多的代码和关于Word版本的说明。
Selection.Expand wdSentence