Automatically folder open after save excel vba - 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

Related

Problem with SendKeys for Running Accessibility Checker on Initial Document_Open

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

Protected VBA Code - Way to write own Code without manually entering a password

I am currently working on a VBA-Project where I want to protect the Code with a password.
Furthermore, I want to use the following lines of code:
Public Sub main()
'Dim CodeModuleSoure As Variant
Dim CodeModuleDestination As Variant
With Application.VBE.ActiveVBProject.VBComponents
'Set CodeModuleSoure = Nothing
Set CodeModuleDestination = .Item(.Count)
CodeModuleDestination.codemodule.addfromstring "Private Sub Worksheet_Activate()" _
& vbCrLf & "Call RefreshRibbon" _
& vbCrLf & "End Sub"
End With
End Sub
This basically adds
Private Sub Worksheet_Activate()
Call RefreshRibbon
End Sub
to the code.
As this does not work while the project is still protected I had a "little" research and found this: Is there a way to crack the password on an Excel VBA Project
Lastly I created a function where I first call the "crack" and then the "code-writer":
Sub otherfuncts()
unprotected
Call Sheet4.main
End Sub
When I try to run the otherfuncts function with a Button it gives me this Error:
If I open the VBA-Project it actually is "cracked" and if I rerun the code after viewing the Project the button does not give me an error. Is there a solution that it works in the first try?

Create a backup of a document

My macro should make a backup (in a specified path) of a file that is currently being saved, a procedure that is often discussed since it is a feature that Word lacks.
Private Sub Document_Close()
Dim blank As Range
For Each blank In ActiveDocument.StoryRanges
If Len(blank.Text) = 1 Then Exit Sub
Next
End Sub
Sub FileSave()
Dim BackupPath As String, objF As Object, retVal As Long, Rslt
BackupPath = "C:\Users\" & Environ("UserName") & "\Documents\BackupWord\"
With ActiveDocument
If .Path = "" Then: If Application.Dialogs(wdDialogFileSaveAs).Show <> -1 Then Exit Sub
If Len(Trim(.Range.Text)) = 1 Then Exit Sub
.Save
If Dir(BackupPath, vbDirectory) = "" Then
MkDir BackupPath
MsgBox "Backup folder has been created.", vbInformation
End If
If .Path & "\" = BackupPath Then
MsgBox "WARNING! Backup folder is the same as the source folder", vbExclamation
Exit Sub
End If
Set objF = CreateObject("Scripting.FileSystemObject")
retVal = -1
On Error Resume Next
retVal = objF.CopyFile(.FullName, BackupPath & .Name, True)
On Error GoTo 0
Set objF = Nothing
If retVal <> 0 Then MsgBox "Backup has not been copied to folder " BackupPath, vbExclamation
End With
End Sub
I will describe what the macro currently does.
FileSave procedure is intercepted.
The macro checks if an active document is saved. If it is, no extra action is required and the macro closes.
If an active document in not saved, the usual "Save As" dialogue appears. If the user chooses to not save the file then the macro closes.
If document is not saved, the macros saves it.
The macro looks for a backup folder. If it is not found, the macro creates it and shows a message box.
Then the macro checks if the source folder is the same as the backup folder. If they are the same, the macro shows a message and closes.
The active (current) document is copied to the backup folder. If it fails, a message box is displayed.
My macros fail to make a backup on two occasions.
When I open Word (no document opened, just blank page), modify it
and choose to close Word, a SaveAs dialog is shown. Then I choose to
save and the document is saved correctly but a backup copy is not
created.
When a document exists on, say, harddrive, pendrive etc.
and I will modify it and choose to close Word a SaveAs dialog is shown.
Then I choose to save and the document is saved correctly but a
backup copy is not created.
You would need a class, i believe at addin level to track all sheets, something like...
Option Explicit
Private WithEvents wd As Word.Application
Public Sub initialise(w As Word.Application)
Set wd = w
End Sub
Private Sub wd_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
' Your code here
End Sub
In your addin (.dotm) file you'd have the following
Option Explicit
Public c As clsCustomWord
Sub AutoExec()
Set c = New clsCustomWord
c.initialise Application
End Sub
Hope this helps

How to disable Auto_Open after first save

When opening up a template a macro that is Auto_Open runs this code:
Sub Auto_Open()
UserForm.Show
End Sub
This then brings up a userform that says please save as and a Ok command button.
When Ok is clicked it has the this code.
Private Sub SaveAs_Click()
Dim bFileSaveAs As Boolean
bFileSaveAs = Application.Dialogs(xlDialogSaveAs).Show
If Not bFileSaveAs Then MsgBox "User cancelled", vbCritical
Unload Me
End Sub
Problem is after the Auto_Open is ran for the first SaveAs i want it to never run again. Because I want to be able to open it later with out the Userform popping up. So how do I disable the Auto_Open once its run and then save it disabled
I cant disable all macros because there are others in the workbook that still need to work.
Thanks
You have to use the Workbook.SaveAs method MSDN Found Here after you get the SaveAsFilename...
As for deleting a sub after it runs (I'd do this before saving) See here... You'll need your Auto Open Sub in a different module so you can delete the module before saving.
Private Sub SaveAs_Click()
Dim x As Object
Set x = Application.VBE.ActiveVBProject.VBComponents
x.Remove VBComponent:=x.Item("TestModule") 'Where TestModule is the module that holds the Auto Open script
Set NewBook = Workbooks.Add
Do
fName = Application.GetSaveAsFilename
Loop Until fName <> False
NewBook.SaveAs Filename:=fName
fName = False
Unload Me
End Sub
Exit auto_open if file name without specific wording.
For example: If file name is not starting from "N", auto_open will be exit.
Sub auto_open()
VBA_CODE = ActiveWorkbook.Name
If Left(VBA_CODE, 1) <> "N" Then Exit Sub

how to get path input from textbox and use in command button in vba macros

I have textbox and command button in the form1.
I will input path in the textbox
After the clicking the command button, workbook from the path location should open and need to the require macros code like copying , etc
when i tried using the . I getting error saying that file1.xlsx not found . plz help
Private Sub CommandButton1_Click()
Set wb1 = Workbooks.Open("file1")
End Sub
Private Sub TextBox1_Change()
Dim file1 As String
file1 = TextBox1.Value
End Sub
Private Sub UserForm_Click()
End Sub
The suggestions in the comments above will get you where you need to go, but if I may suggest... rather than using a textbox for the user to enter the name in, use the GetOpenFilename dialog. In this way you can ensure that the path is valid and the file actually exists. It also gives the user a nice GUI which is more like what they're used to for a File -> Open dialog. Something like this:
Private Sub CommandButton1_Click()
Dim vnt As Variant
On Error GoTo ErrorHandler
vnt = Application.GetOpenFilename("Excel Files (*.xlsx; *.xls; *.xlsm),*.xlsx;*.xls;*.xlsm", 1, "Please select the file to open")
If vnt = False Then Exit Sub
Application.Workbooks.Open (vnt)
ExitPoint:
Exit Sub
ErrorHandler:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description
Resume ExitPoint
End Sub