Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-24-2012, 01:45 AM
jec1 jec1 is offline Sub or Function not defined error - GetParaIndex Word 2010 Mac OS X Sub or Function not defined error - GetParaIndex Word 2010 Office 2010 32bit
Advanced Beginner
Sub or Function not defined error - GetParaIndex Word 2010
 
Join Date: Jan 2012
Posts: 84
jec1 is on a distinguished road
Default Sub or Function not defined error - GetParaIndex Word 2010

Code Error Word 2010 tried LongPtr but GetParaiIndex seems to be culprit.
Many thanks for any ideas. I'm just seeing how it works in 2010.


Code:
Option Explicit
 
Sub MakeAutoXRef()
Dim sel As Selection
Dim rng As Range
Dim para As Paragraph
Dim doc As Document
Dim sBookmarkName As String
Dim sSelectionText As String
'Dim lSelectedParaIndex As Long
Set sel = Selection
Set doc = sel.Document
If sel.Range.Paragraphs.Count <> 1 Then Exit Sub
lSelectedParaIndex = GetParaIndex(sel.Range.Paragraphs.First) 'Sub or Function not defined ????
sel.MoveStartWhile cset:=(Chr$(32) & Chr$(13)), Count:=sel.Characters.Count
sel.MoveEndWhile cset:=(Chr$(32) & Chr$(13)), Count:=-sel.Characters.Count
sSelectionText = sel.Text
For Each para In doc.Paragraphs
  Set rng = para.Range
  rng.MoveStartWhile cset:=(Chr$(32) & Chr$(13)), _
    Count:=rng.Characters.Count
  rng.MoveEndWhile cset:=(Chr$(32) & Chr$(13)), _
    Count:=-rng.Characters.Count
  If rng.Text = sSelectionText Then
    If Not GetParaIndex(para) = lSelectedParaIndex Then
      sBookmarkName = GetOrSetXRefBookmark(para)
      If Len(sBookmarkName) = 0 Then
        MsgBox "Couldn't get or set bookmark"
        Exit Sub
      End If
      sel.InsertCrossReference _
      referencekind:=wdContentText, _
      referenceitem:=doc.Bookmarks(sBookmarkName), _
      referencetype:=wdRefTypeBookmark, _
      insertashyperlink:=True
      Exit Sub
    Else
      MsgBox "Can't self reference!"
    End If
  End If
Next para
End Sub
Code:
Function RemoveInvalidBookmarkCharsFromString(ByVal str As String) As String
Dim i As Integer
For i = 33 To 255
  Select Case i
    Case 33 To 47, 58 To 64, 91 To 96, 123 To 255
      str = Replace(str, Chr(i), vbNullString)
  End Select
Next i
RemoveInvalidBookmarkCharsFromString = str
End Function
Code:
Function ConvertStringRefBookmarkName(ByVal str As String) As String
str = RemoveInvalidBookmarkCharsFromString(str)
str = Replace(str, Chr$(32), "_")
str = "_" & str
str = "XREF" & CStr(Int(90000 * Rnd + 10000)) & str
ConvertStringRefBookmarkName = str
End Function
Code:
Function GetParagraphIndex(para As Paragraph) As Long
GetParagraphIndex = _
para.Range.Document.Range(0, para.Range.End).Paragraphs.Count
End Function
Code:
Function GetOrSetXRefBookmark(para As Paragraph) As String
Dim i As Integer
Dim rng As Range
Dim sBookmarkName As String
If para.Range.Bookmarks.Count <> 0 Then
  For i = 1 To para.Range.Bookmarks.Count
    If InStr(1, para.Range.Bookmarks(i).Name, "XREF") Then
      GetOrSetXRefBookmark = para.Range.Bookmarks(i).Name
      Exit Function
    End If
  Next i
End If
Set rng = para.Range
rng.MoveEnd unit:=wdCharacter, Count:=-1
sBookmarkName = ConvertStringRefBookmarkName(rng.Text)
para.Range.Document.Bookmarks.Add _
Name:=sBookmarkName, _
Range:=rng
GetOrSetXRefBookmark = sBookmarkName
End Function

Last edited by macropod; 05-24-2012 at 11:56 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 05-24-2012, 11:58 PM
macropod's Avatar
macropod macropod is offline Sub or Function not defined error - GetParaIndex Word 2010 Windows 7 64bit Sub or Function not defined error - GetParaIndex Word 2010 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi jec1,

It's all pretty elementary really, you're calling a Function named 'GetParagraphIndex' with 'GetParaIndex'

PS: When posting code, please use the code tages - they're accessed via 'Go Advanced'
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sub or Function not defined error - GetParaIndex Word 2010 FileSystemObject Compile Error: User-Defined Type Not Defined gsrikanth Excel Programming 2 03-28-2022 06:32 AM
undo error in Word 2007 and 2010 neil1967 Word 1 07-18-2014 10:25 AM
Sub or Function not defined error - GetParaIndex Word 2010 Application-defined or Object-defined error Manit Excel Programming 4 12-08-2011 07:35 PM
Compile error: sub or function not defined.. xena2305 Excel Programming 0 08-02-2011 10:17 AM
TOC printing Error Bookmark not Defined techexpressinc Word 0 12-14-2008 05:24 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:26 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft