Problem with SendKeys for Running Accessibility Checker on Initial Document_Open - vba

On a button click I am trying to open an existing document, and upon the documents open, use send keys to simulate going to Review > Check Accessibility. I suspect that it is possibly something with the syntax for sendkeys, as trying the Document_Open alone within a document will not work (possibly need a window selection first?), or the fact that it cannot call the Document_Open after the Shell opens file explorer and a file is selected (again is a window selection needed for the new word document?).
Any help is appreciated!
Private Sub CommandButton2_Click()
Dim Foldername As String
Foldername = "\\server\Instructions\"
Shell "C:\WINDOWS\explorer.exe """ & Foldername & "", vbNormalFocus
Call Document_Open
End Sub
Private Sub Document_Open()
SendKeys "%RA1"
End Sub

Related

How can I catch the Print option being selected in the File menu/tab in Word 2016 (backstage view)?

The following code opens the screen I want to detect being opened:
Sub Test()
Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
End Sub
It does not seem to activate a CommandBarButton object I can create an event handler for. Any thoughts on how to do this without having to change the structure of the File Tab because this is only relevant for one document. The goal is to be able to set duplex printing before the document is printed while the standard File->Print option can still be used.
You can go old school. Before there were events to trap it was common to intercept commands simply by creating a routine with the same name.
For example:
Public Sub PrintPreviewAndPrint()
MsgBox "Hello!"
End Sub
Alternatively, you can trap the DocumentBeforePrint event, though this will only fire after the dialog has been executed.
There is no way to repurpose backstage UI controls, but you can create your own UI instead. So, a better solution would be to consider hiding the built-in UI and rebuilding it fully with custom commands. The Introduction to the Office 2010 Backstage View for Developers article explains the basics of building a custom backstage UI.
The Application.DocumentBeforePrint event allows canceling the default action, so you can programmatically print the document in the way you want by using the PrintOut method.
Public WithEvents appWord as Word.Application
Private Sub appWord_DocumentBeforePrint _
(ByVal Doc As Document, _
Cancel As Boolean)
Dim intResponse As Integer
intResponse = MsgBox("Have you checked the " _
& "printer for letterhead?", _
vbYesNo)
If intResponse = vbNo Then Cancel = True
End Sub

How to detect if user tried to click on save/save as option or pressed ctrl-S in Microsoft word using VBA code?

I want to detect when user press ctrl-S or click on Save option of Microsoft word using VBA excel macro code.
I found related links about Save changes while closing and Detecting if a document is getting close but I not able to find some example code for detecting saving of word document.
Any help would greatly appreciated.
Thanks
Unfortunately (or fortunately) Word does not work like excel and there the keybinding follows different logic. Thus, in Word, you should try something like this, to bind Ctrl + S. The idea is that on openning of the file you tell the application to bind Ctrl + S to the SaveMe Sub.
Option Explicit
Private Sub Document_Open()
With Application
.CustomizationContext = ThisDocument
.KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyS), KeyCategory:=wdKeyCategoryCommand, Command:="SaveMe"
End With
End Sub
Public Sub SaveMe()
MsgBox "User Saved"
End Sub
Put the code in ThisDocument:
Until now, it works this way only for Ctrl+S. If you go for the Save through the menu, it will not follow this logic. This is what you should do then:
For the general solution, follow these instructions:
I. Create a clsWord and put the following code inside:
Option Explicit
Public WithEvents appWord As Word.Application
Private Sub appWord_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
Call SaveMe
End Sub
II. Change the code in ThisDocument to this:
Option Explicit
Dim myWord As New clsWord
Private Sub Document_Open()
With Application
.CustomizationContext = ThisDocument
.KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyS), _
KeyCategory:=wdKeyCategoryCommand, Command:="SaveMe"
End With
Set myWord.appWord = Word.Application
End Sub
III. In a module:
Option Explicit
Public Sub SaveMe()
MsgBox "User Saved"
End Sub
Parts of the ideas are taken from the MSDN here - https://msdn.microsoft.com/en-us/library/office/ff838299.aspx But the code there was not complete :)

Automatically folder open after save excel vba

below is my code for the file to be saved to specific folder. my question is how can I make the folder of the location is open automatically after save complete. I google about "aftersave event" but nothing come out .
Private Sub savebr_Click()
Dim saveas As String
saveas = "C:\user\file"
Application.Dialogs(xlDialogSaveAs).Show saveas
End Sub
So you want to open the folder where the current workbook was saved automatically after saving. Paste this code in the ThisWorkbook code in the VB Editor
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
Call Shell("explorer.exe" & " " & ThisWorkbook.Path, vbNormalFocus)
End Sub
Thisworkbook.path open every time same workbook path(i.e. your macro file path)
If your are adding many excel workbooks and save it on different path and want
to open this path so you should use below code.
Not necessary to use event for this, you can simply write code after saving workbook.
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
Call Shell("explorer.exe" & " " & Activeworkbook.Path, vbNormalFocus)
End sub

How to run a macro in Word before save?

How to make Microsoft Word to run a VBA macro every time before any document is saved? Could it be done without adding macros into the document itself?
You can subscribe to application events in Document_Open by using WithEvents variable and conventional method names (VariableName_EventName). Works in templates as well.
You can put this code into ThisDocument object, or make a separate class module as described here.
Private WithEvents App As Word.Application
Private Sub Document_Open()
Set App = Word.Application
End Sub
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
MsgBox("BeforeSave")
End Sub
List of all application events.
You must add the code bits at the correct place.
This must be at the top of your code page amongst your public variables or constant declerations
Private WithEvents App As Word.Application
Then add this as an open document event.
Private Sub Document_Open()
Set App = Word.Application
End Sub
This is the event that fires on save command Ctrl+s or save icon. I added my own save format and print as I saw It most useful in the case of people filling out forms and you don't want them to overwrite the initial template.
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
''save file with the saveas2 command.
ActiveDocument.SaveAs2 FileName:="YourDocumentNameORVariable" + _
" Date_" + Format(Now(), "yy-mm-dd"), _
FileFormat:=wdFormatDocumentDefault, _
SaveFormsData:=True
''addition to print file upon save
ActiveDocument.PrintOut Background:=True, Range:=wdPrintAllDocument, Copies:=1, Collate:=True
End Sub
Read more about Printout methods: Microsoft VBA - PrintOut
Read more about SaveAs2: Microsoft VBA - SaveAs2
Read more about FileFormat for Saving: Microsoft VBA - FileFormat for Saving
try saving your file in .xlsm, then close, open and save it again. it should work fine.

How to make a command button in word open a file using VB

So basically, I opened Microsoft Word and I went to the developer tab, drew a command button, double clicked it to open VB to write code, I want it to open a specific file. This is the code I wrote
Does not work...
My code I got so far is
Private Sub CommandButton1_Click()
Process.Start ("C:\Program Files\Google\Chrome\Application\chrome.exe")
End Sub
When I click run it comes up with a error saying:
Run-time error '424':
Object required
Can you please fix for me because I have no idea... I just need this one button and that's all.
ok this is what you need my friend...
CREATE a new module - if you don't already have one:
to do this - its simply done in visual basic
then, press: ALT > select: Insert (menu) > select: Module
Note: you can change the module name via the properties window - for this example left it default 'Module1'
WRITE up a PUBLIC sub (as below):
Public Sub StartExeWithArgument()
Dim strProgramName As String
Dim strArgument As String
strProgramName = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
strArgument = "/G"
Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)
End Sub
CALL the sub in the click command private sub:
Private Sub CommandButton1_Click()
Module1.StartExeWithArgument
End Sub
ok, enjoy ;)