Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-03-2023, 11:27 AM
aj92 aj92 is offline MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Windows 11 MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Office 2021
Novice
MS Word Macro - Infinite Spinning Wheel - Thanks Maxey!
 
Join Date: Nov 2023
Posts: 9
aj92 is on a distinguished road
Default MS Word Macro - Infinite Spinning Wheel - Thanks Maxey!

Greg Maxey was kind enough to provide the last portion of code that I've since modified for the reasons stated below. Thanks Greg if you see this! So right now, I have this code for a VBA macro in the Normal>Module section of the VBA editor named "CntCntrls" that does the following:


-allows me to add rich text content control
-allows me to add a tag/title to the rich text content control -allows me to edit an existing tag/title to a rich text content control
-allows me to delete any rich text content control that does not have "Me" as the tag. This will also delete the page of the affected rich text content control so that there isn't excess space from the deleted rich text content controls


--


The problem I'm having is that I'm getting a spinning wheel that loads infinitely but never actually runs the macro despite the document being only 10 pages that I'm using for testing.


Is there something you guys can pick up on that may be potentially causing this issue?


--


Some potentially useful specs: -MacOs Monterey 12.6.8 on 2021 M1 Macbook pro -Office Home & Business 2021 License


--


Sub A_A_AddRichTextControl()
Dim rngSelection As Range

Set rngSelection = Selection.Range
ActiveDocument.ContentControls.Add Type:=wdContentControlRichText, Range:=rngSelection
End Sub


Sub A_B_EditTagAndTitleOfRichTextControl()
Dim cc As ContentControl
Dim newTag As String

If Selection.Range.ContentControls.Count = 1 Then
Set cc = Selection.Range.ContentControls(1)

' Prompt for new tag
newTag = InputBox("Enter new tag for the rich text content control:", "New Tag")

' Set new tag and title
If newTag <> "" Then
cc.tag = newTag
cc.Title = newTag ' Set title equal to the tag
End If
Else
MsgBox "Select a single rich text content control to edit tag and title.", vbExclamation
End If
End Sub


Sub A_C_EditContentControlTagAndTitle()
Dim cc As ContentControl
Dim newTag As String

' Check if the selection contains a content control
If Selection.Range.ContentControls.Count > 0 Then
' Store a reference to the first content control in the selection
Set cc = Selection.Range.ContentControls(1)



' Prompt the user for new tag and title values
newTag = InputBox("Enter the content control property tag:", "Edit Tag", cc.tag)

' Update the tag and title
cc.tag = newTag
cc.Title = newTag

' Optional: Refresh the content control to apply changes
cc.LockContentControl = True
cc.LockContentControl = False
Else
MsgBox "Please select a content control before running this macro.", vbExclamation
End If
End Sub


Sub B_A_RemoveRT_ExceptMe()

Dim oCC As ContentControl
Dim arrTagParts() As String
Dim lngIndex As Long
Dim bDelete As Boolean
Dim oPage As Range

For Each oCC In ActiveDocument.ContentControls
bDelete = True
If oCC.Type = 0 Then 'wdContentControlText
arrTagParts = Split(oCC.tag, " ")
For lngIndex = 0 To UBound(arrTagParts)
If arrTagParts(lngIndex) = "Me" Then
bDelete = False
Exit For
End If
Next lngIndex
If bDelete Then
' Get the page range of the content control
Set oPage = oCC.Range.GoTo(wdGoToPage, wdGoToAbsolute, oCC.Range.Information(wdActiveEndPageNumber))
' Delete the content control
oCC.Delete True
' Delete the entire page
oPage.Delete
End If
End If
Next oCC
lbl_Exit:
Exit Sub
End Sub
Reply With Quote
  #2  
Old 12-03-2023, 01:38 PM
Italophile Italophile is online now MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Windows 11 MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Cross-posted at: vba - Infinite Spinning Wheel - Stack Overflow
For cross-posting etiquette please see A message to forum cross posters - Excelguru
Reply With Quote
  #3  
Old 12-03-2023, 02:09 PM
aj92 aj92 is offline MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Windows 11 MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Office 2021
Novice
MS Word Macro - Infinite Spinning Wheel - Thanks Maxey!
 
Join Date: Nov 2023
Posts: 9
aj92 is on a distinguished road
Default

So sorry about this! Never even considered the points made in that article. Thanks for educating me on this so that moving forward, I do it in the correct manner


I do apologize to anyone I've affected with how I've posted this issue I'm having. Please be patient with me as forum etiquette is something I'm very new to


I'll be better moving forward


I'm going to post this same reply on StackOverflow as well so that I can apologize to that user base as well


Again, my sincere apologies!
Reply With Quote
  #4  
Old 12-03-2023, 03:53 PM
Guessed's Avatar
Guessed Guessed is offline MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Windows 10 MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Office 2016
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

I haven't used a Mac version of Word recently. Does that version now support Content Controls?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 12-03-2023, 04:00 PM
aj92 aj92 is offline MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Windows 11 MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Office 2021
Novice
MS Word Macro - Infinite Spinning Wheel - Thanks Maxey!
 
Join Date: Nov 2023
Posts: 9
aj92 is on a distinguished road
Default

It does. You need to address it with a macro but it once you do, it does give you the ability to add the rich text content control. It also reads content controls that may have been added from a windows version of MS Word


Let me know if there's anything I could help answer for you as I am extremely open minded to try any suggestions you may have


Thanks for taking the time to reply!
Reply With Quote
  #6  
Old 12-03-2023, 04:06 PM
aj92 aj92 is offline MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Windows 11 MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Office 2021
Novice
MS Word Macro - Infinite Spinning Wheel - Thanks Maxey!
 
Join Date: Nov 2023
Posts: 9
aj92 is on a distinguished road
Default

Another peculiar thing I've noticed while testing is the following:

-the only code getting "stuck" in infinite load is the last macro in the code above: Sub B_A_RemoveRT_ExceptMe()


-And when I run the VB editor in debug mode, moving through each line of code as a step input, the code works just fine.



--


For some reason it's getting stuck when I fully execute the macro


Maybe that sheds light on some possible solutions?
Reply With Quote
  #7  
Old 12-03-2023, 07:51 PM
Guessed's Avatar
Guessed Guessed is offline MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Windows 10 MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Office 2016
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

For starters I would say that macros often run into problems if they are progressing forward through a collection of objects and deleting some along the way. The standard practice would be to step backwards through them so that deleting one doesn't change the remainder of the list positions.

However, in this particular macro, there could be considerable other issues. If you find a CC you want to remove, you aren't just deleting that one CC but the entire page. The rest of the page could contain other CCs which again throws off your loop. For that matter, the CCs could be nested so a CC could contain CCs inside it.

The above issues could explain the error in your test file. Without seeing your actual document you are testing on it can be hard to work out the specific problem but that will give you specific areas to look at if you want a solution that fits.

Greg's code is searching for tags that contain the word "Me" but may also contain other words. Did you intend for it to find a tag that was "Me" or did it also need to find CCs with a tag of "Me and Bobby McGee" or "Mean Girls". The former would be found, the latter not by that code. If it was just "Me" by itself in your tags, it would be somewhat easier to restrict the search and simplify the code.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 12-03-2023, 08:51 PM
aj92 aj92 is offline MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Windows 11 MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Office 2021
Novice
MS Word Macro - Infinite Spinning Wheel - Thanks Maxey!
 
Join Date: Nov 2023
Posts: 9
aj92 is on a distinguished road
Default

Wow, what an incredibly detailed reply Andrew. I appreciate the time you took to explain that all to me.


Allow me to explain some things:


1. Greg's code was intended to target the tags of CC that contain the word "Me" in it. It wouldn't target "Mean Girls" but it would indeed target "Me" and "Me and Bobby McGee". And in my testing, that seems to be working okay.


2. In my document, on any single page I would not have more than one CC


3. The document as I have it now contains only one CC and that CC is tagged "Ass.1" and thus given the code, when ran, it should delete that CC as it will delete all CCs that do not contain the word "Me" in it


--


Again, unfortunately, the code is stuck in an infinite load that I can only stop by force quitting


--


Given that information, does that give you any other clues as to what could be happening? Any ideas on how you would modify the code?


--


Thanks again for giving time to this Andrew. Let me know how else I could help out as I'd be more than happy to do so!
Reply With Quote
  #9  
Old 12-03-2023, 11:02 PM
Guessed's Avatar
Guessed Guessed is offline MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Windows 10 MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Office 2016
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

Try this version
Code:
Sub B_A_RemoveRT_ExceptMe()
  Dim i As Integer, oCC As ContentControl, sTag As String
  
  For i = ActiveDocument.ContentControls.Count To 1 Step -1
    Set oCC = ActiveDocument.ContentControls(i)
    If oCC.Type = wdContentControlRichText Then
      sTag = " " & oCC.Tag & " "
      If InStr(sTag, " Me ") > 0 Then
        oCC.LockContentControl = False
        oCC.Range.Bookmarks("\Page").Range.Delete
      End If
    End If
  Next i
lbl_Exit:
  Exit Sub
End Sub
Note that there are scenarios where you might see unintended results - such as you delete a section break which changes page setups and therefore page content gets deleted that you didn't think about. Also, if the Rich Text Content Control spans across a page then I would expect an error or only some of its contents getting deleted.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #10  
Old 12-04-2023, 07:11 AM
aj92 aj92 is offline MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Windows 11 MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Office 2021
Novice
MS Word Macro - Infinite Spinning Wheel - Thanks Maxey!
 
Join Date: Nov 2023
Posts: 9
aj92 is on a distinguished road
Default

Wow, thanks so much for taking the time to write that code up Andrew. Upon running it, I did unfortunately get this error in a very simple document that doesn't have any CC spanning across multiple pages:

"Error 5941 The requested member of the collection does not exist."


Any ideas?
Reply With Quote
  #11  
Old 12-04-2023, 03:25 PM
Guessed's Avatar
Guessed Guessed is offline MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Windows 10 MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Office 2016
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

Which line is highlighted when you choose to debug?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #12  
Old 12-04-2023, 09:25 PM
aj92 aj92 is offline MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Windows 11 MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Office 2021
Novice
MS Word Macro - Infinite Spinning Wheel - Thanks Maxey!
 
Join Date: Nov 2023
Posts: 9
aj92 is on a distinguished road
Default

Thanks for your reply Andrew


When I ran the debug it went straight to this line:

oCC.Range.Bookmarks("\Page").Range.Delete


--


Let me know if there's anything else I could help you with!
Reply With Quote
  #13  
Old 12-05-2023, 12:07 AM
Guessed's Avatar
Guessed Guessed is offline MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Windows 10 MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Office 2016
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

I'm guessing that Bookmarks("\Page) doesn't work on a Mac.

Try using Greg's code to select the page for deletion instead of that line.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Tags
macro, ms-word, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with G Maxey's Content Control Custom Event Irimi-Ai Word VBA 1 11-19-2020 02:20 PM
MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! Infinite loops occurring in find and replace functions in word macro Thefirstfish` Word VBA 5 04-06-2017 07:18 PM
MACRO in infinite loop when it encounters user defined figure label photoval Word VBA 3 02-02-2016 08:26 PM
POWER POINT 97-2003 - I need to make a spinning wheel PANTECH PowerPoint 0 05-29-2014 08:16 AM
MS Word Macro - Infinite Spinning Wheel - Thanks Maxey! wildly flashing insertion point with spinning wheel joannetk Word 4 03-10-2011 04:26 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:29 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