2009-02-25 78 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