2009-02-25 74 views
1

表示的位置生成Word 2003 programaticaly中的书签我有一个HTML页面,当单击打开到Word文档中的特定书签时有链接。我正在使用现有的Word 2003文档,并且没有预先存在的书签。我想使用宏或VBA脚本将书签添加到所有节号标题位置。例如我需要根据位置编号

3.1.4.2这里
STUFF
3.1.4.2.1这里再次
更多STUFF
3.1.4.2.1.1这里又再次
很多别的东西
3.1.4.2.2这里又一次
LOTS MORE STUFF

我想书签所有开始机智的行h X.X.X ... 以标准格式为名。

实施例(如上所述使用作为参考)

3.1.4.2该处线将具有再命名M_3_1_4_2
3.1.4.2.1这里一本书标记将具有书签标识名为M_3_1_4_2_1

我的问题是我需要采取什么方法来处理VBA脚本或宏。

回答

2

如果已经有范围对象,添加书签非常简单。

ActiveDocument.Bookmarks.Add Name:=rngBookmark.Text, Range:=rngBookmark 

获取范围往往是一项艰巨的任务。 现在你说这些是节标题。他们是真正的字节标题?他们是否以某种风格划定界限?它们在文档的正文中还是在页眉中?

您可以像这样循环浏览文档的各个部分,并将范围设置为该部分的开头。

Dim sectCurrent As Word.Section 
Dim rngCurrent As Word.Range 
For Each sectCurrent In ActiveDocument.Content.Sections 

    ' get range that refers to the whole section 
    Set rngCurrent = sectCurrent.Range.Duplicate 

    ' collapse the range to the start of the section 
    rngCurrent.Collapse wdCollapseStart 

    ' expand the range to hold the first "word" 
    ' you can also use other units here like wdLine 
    rngCurrent.MoveEnd Unit:=wdWord, Count:=1 

    ' now that you have the range you can add the bookmark 
    ' you can process the range and create your own name with a custom function GenerateBookmarkName. To get the string, just use rngCurrent.Text. 
    ActiveDocument.Bookmarks.Add Name:=GenerateBookmarkName(rngCurrent), Range:=rngCurrent 

Next sectCurrent 

现在,如果他们不实际的部分,你会经常要使用的查找对象通过所有这些物品查找文档和循环的东西。这里的诀窍是知道要搜索什么。下面是一个示例循环。

' setup range object for search results 
    Set rngFind = ActiveDocument.Content 

    ' cycle through search results looking for whatever 
    With rngFind.Find 

     ' search text 
     .Text = "FINDME" 
     .Format = False 
     .Wrap = wdFindStop 

     ' loop while find is successfull 
     Do While .Execute 

     ' get range you can modify based on found item 
     ' each time you call .Execute rngFind is changed to the found text 
     Set rngModifyMe = rngFind.Duplicate  


     Loop 

    End With 

更多字VBA帮助,您可以点击这里访问次数字的MVP网站:http://word.mvps.org

1
Public Sub HeadingsToBookmarks() 
     Dim strText As String 
     Dim heading As Range 
     Dim hpara As Range 
     Dim ln As Integer 
     Set heading = ActiveDocument.Range(Start:=0, End:=0) 
     Do 
      Dim current As Long 
      current = heading.Start 
      Set heading = heading.GoTo(What:=wdGoToHeading, Which:=wdGoToNext) 
      If heading.Start = current Then 
       Exit Do 
      End If 
      Set hpara = heading.Paragraphs(1).Range 
      strText = Trim(Left(hpara.Text, Len(hpara.Text) - 1)) 
      ln = Len(strText) 
      If ln > 0 Then 
       strText = Trim(RegExp_Replace(strText, "[0-9./-]*", "")) 
       strText = Trim(RegExp_Replace(strText, " +", "_")) 
       ActiveDocument.Bookmarks.Add Name:=strText, Range:=heading.Paragraphs(1).Range 
      End If 
     Loop 
    End Sub 
    Function RegExp_Replace(ReplaceIn, sPattern As String, ReplaceWith As String, Optional IgnoreCase As Boolean = False, _ 
     Optional GlobalMatch As Boolean = False, Optional bMultiLine As Boolean = False) 
     Dim RE 
     Set RE = CreateObject("vbscript.regexp") 
     RE.Pattern = sPattern 
     RE.IgnoreCase = True 
     RE.Global = True 
     RE.MultiLine = True 
     RegExp_Replace = RE.Replace(ReplaceIn, ReplaceWith) 
    End Function