How to detect Weekends on Outlook Custom Form? - vba

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

Related

Change the Format of the Date in VBA

I think this is a basic question but I do not find a way to solve my problem
I created this code :
Sub test()
Range("A11") = CDate(Evaluate("WORKDAY(TODAY(),-1)"))
Range("A11").NumberFormat = "yyyymmdd"
MsgBox Range("A11")
End Sub
But my problem is that the MsgBox do not take into account the NumberFormat "yyyymmdd", it shows "3/5/2021" instead of "20210305". It there a way to change the code so that when I write MsgBox Range("A11") it shows directly "20210305" ?
Thank you for your help.
If you click the cell, you will most likely see 3/5/2021 as the value of the cell. Because it hasn't really changed, just the cell formatting.
MsgBox Range("A11") is the same as MsgBox Range("A11").Value, which will give you the value of the cell, without the formatting.
Try using MsgBox Range("A11").Text instead.

VBA macro for simulating Ctrl+click on a bookmark/hyperlink

I'm trying to write a VBA macro in Microsoft Word to do the same thing as Ctrl+click does (follow a link or go to the bookmark).
I've tried SendKeys but I don't think that works for left mouse click.
I've actually came up with a partially working solution involving the use of
Selection.GoTo What:=wdGoToBookmark, Name:=BLAbut this unfortunately means I can't use ctrl+< because it seems that the history of where the cursor previously was is not saved.
So instead of coming up with my own solution, is there actually a way to just bind the action of Ctrl+click to another button? Or is there a way to write a macro that'll do the same action including keeping track of the history of the cursor?
The following code should do what you want. Install it on a standard code module.
Option Explicit
Dim ReturnRange As Range
Sub GotoBookmark()
' 13 Sep 2017
With Selection
If .Hyperlinks.Count Then
Set ReturnRange = .Range
.Hyperlinks(1).Follow
End If
End With
End Sub
Sub ReturnToLink()
' 13 Sep 2017
If Not ReturnRange Is Nothing Then ReturnRange.Select
End Sub
For testing purposes, create a bookmark in your document and a hyperlink to it. Select the hyperlink and run Sub GotoBookmark. Then run procedure ReturnToLink to go back to where you came from. Note that you can return from anywhere as well as multiple times.
You may wish to create keyboard shortcuts to call the two subs.

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.

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

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

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.