Is there any way to delete all content in a selection of tasks without looping?
'Something like
Sub Select_Column()
ViewApply Name:="&Gantt Chart"
SelectColumn Column:=4, Extend:=False
Selection.Clearcontents
End Sub
Any hint, link or explanation would be useful.
EditClear is the method used in Project to clear the contents of the selection.
Sub Select_Column()
ViewApply Name:="&Gantt Chart"
SelectColumn Column:=4, Extend:=False
EditClear
End Sub
Related
I want to disable this Restrict Style Changes in protected Microsoft Word documents with VBA Code.
https://helpdeskgeek.com/office-tips/restrict-editing-on-word-documents/
This code doesn't work for me:
Sub DisableCheckBox()
ActiveDocument.Unprotect
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, enforcestylelock:=False
End Sub
Any ideas?
Thanks very much.
Edit:
OK. I try to explain my problem.
I have many documents. They have Text and Formular Fields to fill with variable short text. The documents are protected to fill only formulars fields and additionally - and this is the problem - there is activate “limit formating to a selection of styles.”
Complete Text with formular fields is formated to Arial 10 pt. Some formular fields are Arial 12 pt.
When user fill text in the protect document the text is Verdana 12 pt, because this is the default style.That’s the reason I want to deactivate the option “limit formating to a selection of styles.”
Next step, I try in my vba code in a new word document:
https://learn.microsoft.com/en-us/office/vba/api/word.document.protect
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, enforcestylelock:=False Result: “limit formating to a selection of styles” not activated
Then I try in another new document:
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, enforcestylelock:=True Result: “limit formating to a selection of styles” activated
The code works for new documents (checkbox “limit formating to a selection of styles” is on or off).
Now I try the code for my existing protected documents with activated option “limit formating to a selection of styles.”
ActiveDocument.Unprotect
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, enforcestylelock:=False
Result: “limit formating to a selection of styles” is NOT deactivated
I don’t know why?
It is only necessary for me to disable the checkbox option “limit formating to a selection of styles.” with VBA (you can see the checkbox in the picture).
Thank you.
enter image description here
There seems to be a bug with removing the Style restrictions. You could work around that via code like:
Sub DisableCheckBox()
Dim Stl As Style
With ActiveDocument
On Error Resume Next
.Unprotect
On Error GoTo 0
For Each Stl In .Styles
Stl.Locked = False
Next
.Protect NoReset:=True, Type:=wdAllowOnlyFormFields
End With
End Sub
Do note that unless you capture and store the names of the previously allowed or disallowed Styles, the process can't be undone in code later on.
I'm a beginner in word-vba macros (but I quite good for excel-vba) and I'm looking to update a "Table of Tables".
I've found out how to do so for "Table of Content" and "Table of Figures" (with ActiveDocument.TablesOfContents(1).Update) but the Collection TableOfTables doesn't exist.
Does someone know what I have to do?
Thanks in advance,
There isn't a "Table of Tables" object or a TableOfTables collection. A "Table of Tables" is really just a kind of "Table of Contents". Indeed, so too is a "Table of Figures". If you look at the field codes underlying these, you'll see all three use a TOC field - a "Table of Tables" and a "Table of Figures" would have field codes like { TOC \h \z \c "Table" } and { TOC \h \z \c "Figure" }, respectively. So, if you want to update any of these (or any custom types you create), but not necessarily all, you can simply loop through the TableOfContents collection and check what follows the \c switch, if present. Likewise, you can loop through the TableOfContents collection and update all items in it. Hence:
Sub Demo()
Application.ScreenUpdating = False
Dim TOC As TableOfContents
For Each TOC In ActiveDocument.TablesOfContents
TOC.Update
'Or depending on what you want to update:
'TOC.UpdatePageNumbers
Next
Application.ScreenUpdating = True
End Sub
A simpler way - for all fields - would be:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument
.Fields.Update
.PrintPreview
.ClosePrintPreview
End With
Application.ScreenUpdating = True
End Sub
Ok, thanks to #macropod I figure out how to solve my issue.
The 'Table of tables' is not another table of content but another table of figures
So here is my finale code :
Public Sub UpdateAllFiles()
With ActiveDocument
.TablesOfContents(1).Update
.TablesOfFigures(1).Update
.TablesOfFigures(2).Update
End With
End Sub
I'm looking to make a button where I can clear all the cells in a range (e.g. B6:M10000) as well as a single cell (ie C3)
This is what I have so far but it doesn't seem to work.
Sub ClearContents()
Range("B6:M100000" & "C3").Select
Selection.ClearContents
End Sub
Thanks in advance .
Sub ClearContents()
Range("B6:M100000, C3").ClearContents
End Sub
I have a text form field in a protected word 2011 for mac document, that triggers a macro to run upon exit. The macro populates the form with various information but is not dependent on the test that is entered into this particular form field. I have two ideas to solve this problem. The first option would be best if it can be done, otherwise the second option would be a reasonable work-around. Can someone please help find a macro that does one of the following?
a. Will prevent the macro from running a second time if the form field is entered into again and edited?
b. Checks to see upon entry of the form field, if there is text in the field already, and if there is, prevents editing and moves to the next form field without running the upon exit macro again?
I am very new to VBA, but I think I have a handle on the basics. Here is what i have come up with for the b. solution but it does not work.
A macro that checks to see if there is text in form field names "text9", if there is, then unprotect from, go to bookmark "text10" and protect form. else, allow the user to fill in the form field and run the macro upon exit.
Sub TestSkipField()
'
' TestSkipField Macro
'
'
With GetCurrentFF
If Len(.Result) = Null Then
End If
Else
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.UnProtect
End If
Selection.GoTo What:=wdGoToBookmark, Name:="Text10"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End With
End Sub
I think the easiest approach to your problem is to use a global variable that answers the question "has the macro already run?".
Let's say that you have a macro like follows:
Sub myMacro()
'your stuff here
End Sub
You don't want this macro to run twice. Then, you can define a global variable on the top of your module:
Dim hasRun As Boolean 'this will be False by default
Sub myMacro()
'your stuff here
End Sub
Sub myOtherMacro()
'some other stuff here
End Sub
'etc.
And then embed a check into your macro:
Sub myMacro()
If hasRun = False Then 'if hasRun is still False, it means we never ran this code yet
'do your stuff here
hasRun = True 'set hasRun = True, so from now we know that this code has already been executed once
End If
End Sub
This should allow you not to change the structure of your code or executing check on the data, but at the same time executing the macro only once. You can follow this direction to elaborate more the execution conditions: execute it only twice, only if something happened etc.
PLEASE NOTE: you better set hasRun = True at the very end of your macro's code, in order to make sure that the value hasRun will not be True if the execution didn't arrive until the end of your desired code.
I used this vba code in the ThisWorkbook module to disable the right click menu in an Excel workbook.
Private Sub Workbook_Activate()
With Application.CommandBars.FindControl(ID:=847)
.Visible = False
End With
End Sub
Private Sub Workbook_Deactivate()
With Application.CommandBars.FindControl(ID:=847)
.Visible = True
End With
End Sub
Works like a charm.
Problem is, I can't access the right click menu on tabs in ANY workbook now.
The second part of the code is supposed to turn it back on, I assumed? Yet it doesn't.
Even when I remove the code entirely, no workbook, not even a new one, has a menu when I click right on one of the tabs.
Is there a general vba codesnippet that "resets" excel maybe? Or a general "enable all menus" thing?
REVISION:
This code posted here doesn't disable the rightclick menu, it removes the "delete" option from that specific menu.
omg
Application.CommandBars("Ply").Enabled = True
-.-
Started googling different keywords after the last edit and BAM.
Late again as usual, but tackled with the same problem today. Here's the solution to get your right-click functionality back:
Option Explicit
'
Sub tester()
'
Dim cBar As CommandBar
'
For Each cBar In CommandBars
Debug.Print cBar.Name
If (cBar.Type = msoBarTypePopup) Then cBar.Enabled = True
Next
End Sub
Also note that the below also exist. Some macro from work had them all disabled in my Excel.
Application.CommandBars("Cell").Enabled = True
Application.CommandBars("Row").Enabled = True
Application.CommandBars("Column").Enabled = True