I want to circle Invalid Data like this:
The circles should appear when a user saves the document, so I created this function:
Private Function clearInvalidCircles(sheetName)
With Sheets(sheetName)
.ClearCircles
ShapesBefore = .Shapes.Count
.CircleInvalid
ShapesAfter = .Shapes.Count
If ShapesAfter > ShapesBefore Then
MsgBox "Invalid entries are marked with a red circle, please change it to a valid input."
End If
End With
End Function
... and added it to the Workbook_BeforeSave function:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
clearInvalidCircles "Sheet1"
End Sub
But if I want to save the document, it doesn't show the invalid data...
I debugged my code and found out, that clearInvalidCircles worked and the circles only disappeared when the document is saved (after Workbook_BeforeSave)
Is there a better option to avoid this problem?
Thank you very much in advance for your answers...
Related
Does anyone know if its possible to make sure that Rich text content control object is filled? and if not the file will not be saved?
Code is embedded in a word document not a module:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
Dim richTextControl1 As Microsoft.Office.Tools.Word.RichTextContentControl
If richTextControl1.Value = "" Then a = MsgBox("File not saved fill Approval ID field ", vbInformation)
If a = vbInformation Then Cancel = True
Else
End Sub
This is what i got for now. The code seems to refer correctly to the context control element(when trying to format the text it spits out the code in the field) but apart from that nothing else happens. Any help or direction appreciated
I need your help to unlock me.
I would like to retrieve the name of the file saved by the user with Word in VBA. I'm using the BeforeSave method, but I can not seem to catch the name chosen by the user.
Thank you in advance for your help !
With the save as pane not triggering the SaveAs() sub, I think the only reliable way to do this is an OnTime event triggered by DocumentBeforeSave.
Something like:
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI = True Then
Word.Application.OnTime Now + TimeValue("00:00:01"), "AfterSave"
Else
DoStuff()
End if
End Sub
Sub AfterSave()
CheckSaveOccured()
DoStuff()
End Sub
See this post for a more detailed explanation.
In VBA for excel, I have a userform then I want this to show only for 1 instance. Even if the user re-open it, it won't open again. Is there any code for it? well, I'm also using this code for my login:
Private Sub UserForm_Initialize()
User.Caption = Environ("Username")
End Sub
I'm thinking if i can use this code in my problem. Hoping for a quick response. thanks guys, you're awesome!
Yes, it's possible.
You have to add new sheet. In a cell A1 type 0 (zero), then hide it. In a code which calls UserForm, use this:
Sub ShowMyForm()
If ThisWorkbook.Worksheets("HiddenSheet").Range("A1")=0 then MyUserForm.Show
End Sub
In a form:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
ThisWorkbook.Worksheets("HiddenSheet").Range("A1")=1
ThisWorkbook.Save()
DoEvents
End Sub
If you don't want to add an extra sheet just to store one bool, you can set a custom document property like this:
Private Sub Workbook_Open()
On Error Resume Next
Dim test As Boolean
test = Me.CustomDocumentProperties("UserFormShown").Value
If Err.Number = 0 Then Exit Sub
UserForm1.Show
Me.CustomDocumentProperties.Add "UserFormShown", False, msoPropertyTypeBoolean, True
End Sub
If the property hasn't been set yet it will throw an error, so trapping an error lets you know if you've set the property (and shown the form).
Q: What is recommended way of notifing user about loss of functionality unless User change macro security settings?
What I do now:
I display warning on the first sheet user see after opening Workbook, with explanation why things WONT work unless proper settings are set.
And I hide it on start up. (Which wont happen unless settings are OK)
But its not perfect solution:
That message is just one time. (While user could send that sheet to somebody else with different settings...)
Hiding and showing those few rows is treaded by Excel as changing document. (So just opening and closing excel will generate Save changed warning!)
Ok, here is best answer I could think off
(And big thx to #brettdj for suggestion!)
Sub HideMacroSecWarning()
Status = ThisWorkbook.Saved
Help.Range("Warning").rows.hidden = True
ThisWorkbook.Saved = Status
End Sub
Sub ShowMacroSecWarning()
Status = ThisWorkbook.Saved
Help.Range("Warning").rows.hidden = False
ThisWorkbook.Saved = Status
End Sub
Those are for showing and hiding macros. Saved state is preserved so only user actions can trigger "unsaved changes" dialogbox.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call ShowMacroSecWarning
End Sub
Private Sub Workbook_Open()
Call HideMacroSecWarning
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call ShowMacroSecWarning
End Sub
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
Call HideMacroSecWarning
End Sub
Those assure that no matter the state of user settings warning will ALWAYS be displayed in SAVED FILE. (So even if for user Exel do not display that warning, somebody else who will open it on different machine will see that warning.)
I'm having a slight problem with some VBA coding on a multi-page userform I'm creating. I have some textboxes on each page of the userform, and I had code that I had been using with regular userforms to clear the textboxes provided in the following answer to another thread (https://stackoverflow.com/a/8921247/2477891)
The code looks like this:
Sub TB_enter(TB_name)
If Len(Me.Controls(TB_name).Tag) = 0 Then
Me.Controls(TB_name).Tag = Me.Controls(TB_name).Value
Me.Controls(TB_name).Value = vbNullString
End If
End Sub
Sub TB_exit(TB_name)
'When you click out of the textbox and no information has been entered, returns original text
If Len(Me.Controls(TB_name).Value) = 0 Then
Me.Controls(TB_name).Value = Me.Controls(TB_name).Tag
Me.Controls(TB_name).Tag = vbNullString
End If
End Sub
Along with the following code used for the textboxes to clear them:
Private Sub AdNtbx_Enter()
TB_enter ActiveControl.Name
End Sub
Private Sub AdNtbx_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TB_exit ActiveControl.Name
End Sub
My problem is that they are no longer working because they are on multi-pages, and the following line comes up with an error:
Me.Controls(TB_name).Value = vbNullString
Does anyone have any suggestions as to what could be the problem/solution?
I'd really appreciate it.
Thanks!
I believe this is what you are looking for :
Set Page1 = ThisWorkbook.VBProject.VBComponents("UserForm1").Designer.Controls("Multipage1").Pages("Page1")
Page1.textbox1.Text = vbNullString
You can edit ThisWorkbook, UserForm1 etc.. properties to suit your needs, but with this code you will be able clear textboxes on multipage object