Excel VBA: How to programably turn on Snap-To-Grid - vba

How to turn on snap to grid in Excel using VBA ?

Try Application.CommandBars.FindControl(ID:=549).Execute (tested up to excel 2010)

More precisely, first checking whether Snap-grid is already enabled or not
Sub Snap_to_grid()
With Application.CommandBars.FindControl(ID:=549)
If Not (.Enabled) Then: .Execute
End With
End Sub

The solutions posted don't work in Office 365. Solution for 365:
Sub TurnOnSnapToGrid
If Not Application.CommandBars.GetPressedMso("SnapToGrid") Then
Application.CommandBars.ExecuteMso ("SnapToGrid")
End If
End Sub

Related

How to detect Weekends on Outlook Custom Form?

I am talking about Outlook custom Form and not VBA Userform.
I use olkdatecontrol tool as datepicker. Is there any way to detect weekends using this tool?
I tried the following.
Sub cmdbutTest_Click
getdate = Item.UserProperties.Find("Start").Value
If(getdate = vbSunday or vbSaturday)then
MsgBox "Date may not start on the weekend"
End If
End Sub
This one worked for me.
Got this from https://stackoverflow.com/a/33115260/10711853
MsgBox Weekday(MyDate, vbMonday) > 5

Adding Auto-Complete like feature to ComboBox in MS Word using VBA

I have populated a ComboBox with a list of strings and since that list is pretty long, I'm struggling to add an Auto-Complete feature like to the ComboBox who will display matching strings as the user types in characters.
An idea of what I want can be found here Auto-Complete with only text and not numbers ComboBox Excel VBA but it's done using VBA Excel.
Here is the code I got so far
Private Sub ComboBox1_Click()
Dim i As Long
Static found As Boolean
If found Then
found = False
Exit Sub
End If
With FormDialog.ComboBox1
.DropDown
'.MatchEntry = fmMatchEntryFirstLetter
If .Text = "" Then Exit Sub
For i = 0 To .ListCount
If InStr(.List(i), .Text) > 0 Then
found = True
If found Then
' the suggestion code will go here I think
End If
Exit For '<--| exit loop
End If
Next i
End With
End Sub
If anyone can help me with this, I will be thankful.
Following #jsotola comment, I've found after trials that the kind of answer provided here Auto-Complete with only text and not numbers ComboBox Excel VBA by #Ralph is useful to answer my question. You have to only focus on the txtSearchTerm_Change() method and adapt it to your program requirements.
Don't forget the Option Compare Text at the beginning to disbale case sensitivity on the search.
I hope it helps.

Maro to always copy and paste as formulas by default in Excel

I need help to check this macro that intends to copy and paste without formatting. It doesn't work fine.
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
On Error Resume Next
Target.PasteSpecial xlPasteFormulas
Application.CutCopyMode = True
End Sub
How do I make Ctrl-V to paste without any format? I want to keep the excel clean and tidy, so users can copy and paste values and formulas without formatting.
Thanks!
If you want to make your own, custom Ctrl-V, well, you can achieve it this way:
' Code Module ThisWorkbook
Private Sub Workbook_Open()
Application.OnKey "^v", "ThisWorkbook.PasteWithoutFormat"
End Sub
Private Sub PasteWithoutFormat()
If Application.CutCopyMode Then Application.Selection.PasteSpecial xlPasteFormulas
End Sub
Try this: Assigning Macros to Short-Cut Keys in Excel
You'll need to work out how users have access to the macro - there are a few ways to do this and I'm not so sure what will work in your environment, nor your access level to make these options work.
I suppose I'd build a very simple ADD-IN in which I'd embed the macro and there also, assign the macro shortcut to override CTRL + V. Then deploy the ADD-IN according to your company policies.
However, we need more details from you to fully implement the solution.
I went for the easiest way. I set up Ctr-v (in macro's options) to run the following code:
Sub Pstfrmul()
Selection.PasteSpecial xlPasteFormulas
End Sub
Many many thanks for all your answers and comments. Best,
GerĂ³nimo

VBA. What is incorrect in this code?

Searching found code like this
Sub Workbook_Activate()
Application.OnKey "+^{RIGHT}", "YourMacroName"
End Sub
However, when I tried, I got
How to create procedure?
I did this
Sub YourMacroName()
Selection.Copy
Sheets("V").Select
End Sub
Sub Workbook_Activate()
Application.OnKey "+^{RIGHT}", "YourMacroName"
End Sub
Got the same error
What would be correct code? Or where would be tutorial for dummies? Found some examples, but they does not work
I see my tags were modified to excel and excel-vba. But I do not use excel. Use Kingsoft Office
Changed Application.OnKey "+^{RIGHT}", "YourMacroName" to .OnKey Key:="^+M", Procedure:="YourMacroName"
and got
Then changed to OnKey Key:="^+M", Procedure:="YourMacroName" (removed .) and got error Named argument not found. And get selected Key:=
In "ThisWorkbook", you will run code that is triggered by an event. I suggest you also put it in Workbook_Open instead of Workbook_Activate as you only need to store the shortcut once.
So, in the VB Editor, open the "Project Explorer" if it is not (CTRL+R) and find "ThisWorkbook" in the folder "Microsoft Excel Objects".
Here the code should look like this
Private Sub Workbook_Open()
' CTRL + SHIFT + RIGHT
Application.OnKey "+^{RIGHT}", "YourMacroName"
End Sub
Since, inside a Module (In the Project Explorer, right click on the folder "Modules" and select Insert > Module), put the macro "YourMacroName"
Sub YourMacroName()
Selection.Copy
Sheets("V").Select
End Sub
Oh, and you should probably rename your procedure "YourMacroName" for something more obvious...

In Outlook 2010 VBA run a macro whose name was passed as a parameter

I am in Outlook 2010 in Windows 7 writing in VBA and want to pass the name of a macro (or sub routine) as a string variable to another sub routine and have that routine run the macro. In Word you can do this with
Application.Run MacroName:=strMacroName
Where strMacroName is a string variable with the name of the macro. That approach does not work in Outlook 2010. How can I accomplish the same thing?
I tried
Call Application.strMacroName
Call strMacroName
strMacroName on its own line
Outlook.Application.strMacroName
None of those things worked.
I just upgraded to Outlook 2010 and so can no longer use keyboard shortcuts to run custom code for handling email. So to restore some version of that functionality I have created code to present a dialog box with my most common macros. The code is fairly clean to modify as time goes along and pass along the name of the macro I want to run but I used to be able to run that routine in one command (Application.Run MacroName:=strMacroName).
Now I have to include a long switch statement to accomplish the same thing. Not nearly as simple.
Thanks!
CallByName seems the only way to go.
With this code in ThisOutlookSession:
Public Sub TestFoo()
Dim testClass As New TestClass1
CallByName testClass, "TestMethod1", VbMethod
End Sub
And this code in TestClass1:
Public Sub TestMethod1()
MsgBox "huzzah!"
End Sub
Calling ThisOutlookSession.TestFoo gives you the expected message box.
As far as I can tell, the only way to run a named macro programmatically in Outlook (without using custom Classes, as the other answer here does) is to create a temporary CommandBarButton, execute it, and immediately delete it. This works in Outlook 2013 even with the Ribbon:
Public Sub runProc(procToRun As String)
With Application.ActiveExplorer.CommandBars.Add("Custom", temporary:=True)
With .Controls.Add(msoControlButton, 1, , , True)
.OnAction = procToRun
.Execute
.Delete
End With
.Delete
End With
End Sub
I know this is an old question, but I was unable to find this exact answer myself in late 2017, so hopefully it will help someone else. Please note that this will NOT run macros that are in ThisOutlookSession...your macro needs to be in a code module.