Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-26-2015, 07:50 AM
Tejas.T Tejas.T is offline Document_ContentControlBeforeDelete Event Windows 8 Document_ContentControlBeforeDelete Event Office 2010 32bit
Novice
Document_ContentControlBeforeDelete Event
 
Join Date: Feb 2015
Posts: 6
Tejas.T is on a distinguished road
Default Document_ContentControlBeforeDelete Event

I am developing a MS Word Add-in where Content Control text is locked and is changed automatically based on their sequence in document.

On Content Control delete event, I need to iterate on each control and change it's Range Text.

I am able to do it but the problem is the Content Contorl which is just deleted is also part of ActiveDocument.ContentControls and when the event tries to unlock it and change the Range.Text, it throws run time error 5825 saying "Object has been deleted"



Below is the my Document_ContentControlBeforeDelete event.

Code:
Private Sub Document_ContentControlBeforeDelete(ByVal OldContentControl As ContentControl, ByVal InUndoRedo As Boolean)
    
    Dim ContentCtrl As ContentControl
    Dim Counter As Long
    Counter = 1
    
    For Each ContentCtrl In ActiveDocument.ContentControls
        ContentCtrl.LockContents = False
        ContentCtrl.Range.Text = Counter
        ContentCtrl.LockContents = True
        
        Counter = Counter + 1
    Next
    
End Sub

Is there any way to identify though code that a Content Control has been deleted so I can skip it in my loop.

And attached is the sample document with this event.

Thanks,
Tejas
Attached Files
File Type: docm CC Delete Issue.docm (26.8 KB, 11 views)
Reply With Quote
  #2  
Old 02-26-2015, 08:11 AM
gmaxey gmaxey is offline Document_ContentControlBeforeDelete Event Windows 7 32bit Document_ContentControlBeforeDelete Event Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

The quick and dirty way would be to use error handling. Here is another way:

Code:
Private Sub Document_ContentControlBeforeDelete(ByVal OldContentControl As ContentControl, ByVal InUndoRedo As Boolean)
Dim lngIndex As Long
Dim oCC As ContentControl
Dim ContentCtrl As ContentControl
Dim Counter As Long
  Counter = 1
  For lngIndex = ActiveDocument.ContentControls.Count To 1 Step -1
    Set oCC = ActiveDocument.ContentControls(lngIndex)
    If Not oCC.ID = OldContentControl.ID Then
    With oCC
      .LockContents = False
      .Range.Text = Counter
      .LockContents = True
      Counter = Counter + 1
    End With
    End If
  Next lngIndex
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 02-26-2015, 02:23 PM
Tejas.T Tejas.T is offline Document_ContentControlBeforeDelete Event Windows 8 Document_ContentControlBeforeDelete Event Office 2010 32bit
Novice
Document_ContentControlBeforeDelete Event
 
Join Date: Feb 2015
Posts: 6
Tejas.T is on a distinguished road
Default

Hi Greg,

Thanks for your reply.

Actually this solution I have already done but I am not allowed to use On Error.
Is there any property or any other kind of indication by which I can figure this out?
Reply With Quote
  #4  
Old 02-26-2015, 03:21 PM
Guessed's Avatar
Guessed Guessed is offline Document_ContentControlBeforeDelete Event Windows 7 32bit Document_ContentControlBeforeDelete Event Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Tejas

Did you actually read Greg's macro? There is no On Error used there.

The code is running when a CC is being deleted and it passes in the identity of that CC. Greg's code makes use of that information to exclude that particular CC.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 02-27-2015, 05:40 AM
Tejas.T Tejas.T is offline Document_ContentControlBeforeDelete Event Windows 8 Document_ContentControlBeforeDelete Event Office 2010 32bit
Novice
Document_ContentControlBeforeDelete Event
 
Join Date: Feb 2015
Posts: 6
Tejas.T is on a distinguished road
Default

Hi Andrew,

Sorry about that. I have checked code again.

Actually I have tried this approach already and it doesn't work on multiple Content Control delete. Do you have any suggestion to make it work on multiple Content Controls as well?

Thanks.
Reply With Quote
  #6  
Old 02-27-2015, 08:01 PM
Guessed's Avatar
Guessed Guessed is offline Document_ContentControlBeforeDelete Event Windows 7 32bit Document_ContentControlBeforeDelete Event Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

What is the end game for this macro?

Is this an academic exercise to make all the CCs have an incrementing number or is there actually a real world use for this?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Tags
content control, content control events, events



Similar Threads
Thread Thread Starter Forum Replies Last Post
File beforeSave Event Megazoid Word VBA 3 09-14-2014 01:41 AM
Hyperlinks in Calendar Event? komobu Outlook 0 07-16-2014 08:09 AM
Workbook_Open event do not working? beginner Excel Programming 8 04-11-2013 02:37 PM
Duplicate event reminders rgarneau Outlook 0 01-23-2012 08:58 AM
Event Log - Outlook Crashed Davva Outlook 0 11-10-2009 01:35 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:17 PM.


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