Creating Strikethrough Macro in Excel - vba

I'm a novice to VBA and I'm trying to make a simple macro where one can highlight a set of cells, click a button, and strikethrough the selected sells. After, you can select the cell again, click the same button, and remove the strikethrough.
I have been looking for decent documentation but, have yet to find anything.
Here's some code.
Also, I would love to know where the best documentation is on VBA.
Sub strikeOut()
Selection.Font.Strikethrough = True
End Sub
I also need help with the command button.
Thank you.

It looks like you're on the right path. Based on your code, I'm assuming you already have a command button created. If so try this:
Sub strikeOut()
With Selection.Font
.Strikethrough = Not .Strikethrough
End With
End Sub
To create a command button:
Excel 2003 and earlier:
Open up the Visual Basic toolbar and activate the Control Toolbox button. Another box/toolbar should appear with different control options.
Select the Button option and place it in the desired location.
Excel 2007 and later:
Click on the Developer tab/ribbon.
Select Insert and select Button and place it in the desired location.
*The steps below apply to all versions from this point forward.
Right-click on your new button and select Properties to give your button a name/caption.
Right-click again and select View Code.
In the ButtonName_Click() sub, add the strikeOut() call using either:
Call strikeOut()
or simply
strikeOut
To answer the second part of your question, it's hard to say what is the 'best' but here are some links that may help:
Chip Pearson's site
MSDN
OZgrid

Related

Using VBA to allow a checkbox to hide/show content in Microsoft Word

I've gone through a number of tutorials and instructional videos trying to achieve my intended result of simply allowing a checkbox in my form to hide content when selected, or re-show it when being de-selected, but nothing seems to be working.
Currently, I've created a bookmark for the content I want hidden, and try to call his this in VBA with the following statement - which a number of resources indicated as the solution:
Private Sub CheckBox1_Click()
ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = CheckBox1.Value
End Sub
But despite doing this, selecting the checkbox has no affect on the content.
Is there something additional I'm missing (I'm using Microsoft Word 2013).
Your code worked fine when I tested it, but I ran into a few issues since I've never messed with userforms/checkboxs in Word VBA and I suspect you may have the same.
For instance, the first thing I did was create Insert --> Module. This is incorrect. What you want to do is Insert --> Userform then drag a checkbox over from the ToolBox
https://smallbusiness.chron.com/use-check-boxes-word-54673.html
Then double click the checkbox to bring up the "module" it would run, notice there is no module in the side pane! Edit the module, then go back to the userform and press F5. You should be presented with a checkbox that will hide/unhide your text.
Here is my module:
Public Sub CheckBox1_Click()
ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = CheckBox1.Value
End Sub
Here is an image:
Note: I didn't test how to insert the checkbox into the word doc, I'll leave you some of the learning!
Update:
This sub will make the CheckBox appear when run, but I'm not sure the logic you would use to call it, perhaps an event like opening of document?
Sub Loadform()
Load UserForm1
UserForm1.Show
End Sub
This could be called via a keyboard shortcut or event, but this will cause a "pop-up window". If you want an inform checkbox you may need to look into using this Legacy Form/Checkbox. I was following the URL from above but I think it's dated.
Update:
You could also add this easily to a toolbar, but that isn't likely what you want. I found the best way is to use a "field code" see --> https://word.tips.net/T001571_Assigning_a_Macro_to_a_Button_in_Your_Text.html
I was able to get this to work by pressing CTRL + F9 then typing { MacroButton Loadform ClickMe} then clicking this text. This may be the best bet as using an image seems not to be a great idea.. (https://answers.microsoft.com/en-us/msoffice/forum/all/using-graphic-with-macrobutton/a9c1ef3b-cf1f-48ba-92a8-c44bffcdc131) & (http://www.addbalance.com/usersguide/parts/macrobutton_fields.htm#Different_behavior)
Gif Example:

Word VBA: Make macro easy to run

I've made a form-letter document with a macro that performs the mail merge. I don't want the user to have run it from the menus, and I want this to be portable. If there's a way for a button to appear on each user's ribbon or quick command menu, I'm not familiar with it.
So I put a button in the document itself. Unfortunately, every form-letter created has the same button in it. I suppose I could write the code to delete every one, but I think that would be slow.
Is there a way to assign a shortcut key to an existing macro, and have it reside in the document?
I had to implement something pretty similar to what you were referring to some 10 staff. My solution (by no means as portable as desired) was to export the macros and forms from my Normal template to the other users, I coupled this with Ribbon customization and it worked well. Unfortunately, when a change was needed, I had to trudge over to everyone's machine individually.
I would suggest you stick with your solution of deleting button after the merge is complete. Here's some code to help with that:
Sub DeleteCommandButton()
For Each o In ActiveDocument.InlineShapes
If o.OLEFormat.Object.Name = "CommandButton1" Then
o.Delete
End If
Next
End Sub
Good luck, hopefully this helps.

Changing ActiveX Component Name Properties Dynamically in Word

I am trying to develop a dynamic user form in a Word document that will create an ActiveX radio button with a generic name and then rename that created radio button to make it have a specific number based on its location in the form.
So far I believe I have all the components to write the code because I am able to create the button. I create the button by using VBA to import a quick part that has the radio button with a defined radioButtonX name and I am able to change the name property of that radio button to radioButton1 for example. The problem that I am running into is that my code will insert the quick part with the generic radioButtonX name and then fail to change the radio button name because it says that radioButtonX does not exist. However, I can manually run the second part of the code after radioButtonX has been inserted and I am able to successfully change the radio button name giving me the end result that I am looking for in two separate steps.
I am a self taught VBA scripter for the last year so I am far from an expert at coding. A few of the strategies that I have taken so far were to:
Put in waits (thinking that pausing the script would give the script time to recognize the new radio button)
Separated the code into different subs having one sub create the radio button and then call a sub to change the name (hoping this would update VBA)
Separated the code into different modules having one modules create the radio button and then call a modules to change the name (hoping this would update VBA)
I was hoping for some advice, strategies, or sample codes that I can use to get VBA to recognize the new radio button that I have inserted while the code was running. Another thought that I had was maybe there is a command that exists that will have VBA refresh its knowledge of variables, but I have not have any luck finding something like that in my web searches to this point.
I tried to simplify the code that I was working on and pull out the code that would perform the function that I was requesting. Running the simpler code was successful and listed below.
Private Sub AddRadioButton()
Selection.TypeText Text:="TestRadioButton"
Selection.Range.InsertAutoText
ActiveWindow.View.ShowXMLMarkup = wdToggle
ChangeButtonName
End Sub
Private Sub ChangeButtonName()
ThisDocument.radioButtonX.Name = "radioButton1"
End Sub
For this script to work you must create a quick part or a radio button that is named TestRadioButton. I think now the root issue has to deal with timing that looked like it wasn't working because the script that this code came from is using a quick part with multiple generic radio buttons in it so the script executing does not recognize the new radio buttons while running.

Word add-in, custom layout

Is it possible to create a custom layout, existing ones are:
Print layout
Full Screen reading
Web layout
Outline
Draft
These can be found in the View Ribbon under the group Document Views.
My aim is to get my own layout button in either the existing View Ribbon (if it is possible to modify it) or add a new layout to my custom Ribbon.
Thanks in advance!
This answer is going to provide information on how to change standard settings of any view type control and associate these changes with certain document. This will not work with all documents and will not change the control action for whole Word Application but for one document. Operation could be repeated for few document and almost all Word button.
Important! I'm not using English version of Office application therefore some description will not match exactly to what you have. Tried and tested for Word 2010.
There are following steps to go:
Open new document- one where control should work according to your private expectations.
Go to View >> Macros >> Show list of macros
In the combo-box below middle of the Macro window choose something like Word application commands (or Word macros or similar). As a result you get list of lots of macros names.
You need to guess which of the macro is associated with ribbon control you are going to change. Use common sense and logic to find it. Sometimes two or three seems to match and possibly you will need to make a try.
A) let's try to change behaviour of draft/pending/working view ribbon control. one rounded red below:
B) find macro ViewNormal (but not ViewDraft)
C) select this macro on the list
Change back on the combo-box list to your document (while keeping your chosen macro selected)
Press Create button on the right in the macro window. You will be moved to VBA Editor to the following code:
Sub ViewNormal()
'
' ViewNormal Makro
' Zmienia widok edycji na normalny
'
If ActiveWindow.View.SplitSpecial = wdPaneNone Then
ActiveWindow.ActivePane.View.Type = wdNormalView
Else
ActiveWindow.View.Type = wdNormalView
End If
End Sub
This code is responsible for working of chosen ribbon control.
First, let's check if we can take control of ribbon button- add MsgBox "Control taken" at the end of the code, before End sub. Back to Word App and press button on the ribbon which result should be- setting of chosen view and our message box.
Now you need to change your code accordingly to set your view as you need. Use VBA for that.
Save document as .Docm and all the changes will be applied to the document each time you press chosen ribbon button.

Excel macro - open macro without going into Visual Basic?

I've finished my macro! But is there an easier way in runnning it?
I know two ways
Open Visual Basic, select the correct macro and run!
View Macro's, select the correct macro and run!
Is there an easier way, or are these the only two ways?
Thanks
Place a command button on your sheet, view its code in the VB editor, and edit it so it looks something like this:
Private Sub CommandButton1_Click()
Call MyMacro
End Sub
You can then just click your button every time you want to run your macro.
you can even create little smiley icon in your Toolbar so that it stays there and you can run it by clicking it or have shortcut keys attached to it.
There are several options:
Toolbar or Menu (Excel 2003 & earlier)
http://peltiertech.com/WordPress/how-to-assign-a-macro-to-a-toolbar-or-menu/
ActiveX control
http://peltiertech.com/WordPress/how-to-assign-a-macro-to-an-activex-control/
Button (Forms menu control) or Shape
http://peltiertech.com/WordPress/how-to-assign-a-macro-to-a-button-or-shape/