Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #5  
Old 05-28-2024, 11:06 AM
gmaxey gmaxey is offline Copy/Paste of unmodified VBA Code in Word returns an error Windows 10 Copy/Paste of unmodified VBA Code in Word returns an error Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,636
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

May not be perfect for your requirement, but should get you close:


Code:
Option Explicit
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oTbl As Table
Dim lngIndex As Long
Dim colBMs As New Collection
Dim oBM As Bookmark
Dim strBMName As String
Dim oRNg As Range
Dim bValid As Boolean
  'Get the collection of existing bookmarks
  For Each oBM In ActiveDocument.Bookmarks
    colBMs.Add oBM.Name, oBM.Name
  Next oBM
  Set oTbl = Selection.Tables(1)
  bValid = True
  For lngIndex = 1 To oTbl.Rows.Count
    Set oRNg = oTbl.Cell(lngIndex, 1).Range
    oRNg.End = oRNg.End - 1
    strBMName = fcnValidateBMName(oRNg.Text)
    On Error Resume Next
    colBMs.Add strBMName, strBMName
    If Err.Number = 0 Then
      ActiveDocument.Bookmarks.Add strBMName, oRNg
    Else
       oTbl.Cell(lngIndex, 1).Range.Shading.BackgroundPatternColor = wdColorRed
       bValid = False
    End If
    On Error GoTo 0
  Next lngIndex
  If bValid Then
    MsgBox "Processing complete."
  Else
    MsgBox "Processing complete. One or more rows could not be bookmarked due to a duplicate name."
  End If
lbl_Exit:
  Exit Sub
End Sub

Function fcnValidateBMName(strIn As String) As String
  strIn = Trim(strIn)
  strIn = Replace(strIn, " ", "_")
  If IsNumeric(Left(strIn, 1)) Then
    strIn = "_" & strIn
  End If
  fcnValidateBMName = strIn
End Function
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Code to copy data from two sheet ,paste into corresponding sheet through cmdbutton jackfruit88 Excel Programming 1 07-08-2022 09:27 PM
paste returns the wrong not the latest cut Peborgh Word 2 11-23-2021 03:31 AM
Copy/Paste of unmodified VBA Code in Word returns an error Error 4605 while trying to copy and paste with same document Ilmari Word VBA 2 05-05-2020 12:08 AM
Copy/Paste of unmodified VBA Code in Word returns an error Copy/Paste error stuart255 Office 5 05-06-2019 02:13 AM
VBA Newbie (Trying to write a code to copy and paste info from one sheet to another) Forum2000s Excel Programming 2 05-07-2017 04:51 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:54 PM.


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