Batch file code for a vba program - vba

I have a vba code which works fine, but I want to create the same code as batch file which can do the same thing the vba code is doing.
I have created the code which sends all files in a folder to a specified email address and after sending delete the file.
Can anyone help me in creating the same thing with a batch file which can do the same thing.
Below is the VBA code:
Private Sub Click()
Dim mess_body As String, StrFile As String, StrPath As String
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
'~~> Change path here
StrPath = "\Project\New folder\New folder\"
With MailOutLook
.BodyFormat = olFormatRichText
.To = "test#sdm.com"
.Subject = "test"
.HTMLBody = "test"
'~~> *.* for all files
StrFile = Dir(StrPath & "*.*")
Do While Len(StrFile) > 0
.Attachments.Add StrPath & StrFile
StrFile = Dir
Loop
'.DeleteAfterSubmit = True
.Send
End With
Kill "\Project\New folder\New folder\*.*"
MsgBox "Reports have been sent", vbOKOnly
End Sub

U can use cell ("A1") value as folder reference.
Dim objFolder As Object
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
'~~> Change path here
VAR1 = Range("A1").Value
If VAR1 = False Then MsgBox "Cell is empty"
If VAR1 = False Then Exit Sub
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(VAR1)
With MailOutLook
.BodyFormat = olFormatRichText
.To = "test#test.com"
.Subject = "test"
.HTMLBody = "test"
'~~> *.* for all files
StrFile = Dir(objFolder & "*.*")
...
'.DeleteAfterSubmit = True
.Send
End With
'delete files
Kill objFolder & "\*.*"

Related

Save INK annotation on PPT using VBA or lock specific parts of the presentation not to be editable

PPT How to save Ink Annotations on live show.
Good evening, i am working on something but i am stuck, really need a hand
I am using a VBA code to Save the presentation after i sign with the pen as PDF and then prepare an email with the attachment. The code is working fine so far. The only issue witch i have is that not to be editable i saved as Macro Enabled Show but is not exporting the file with the below code
`
Sub START()
LastSlide = ActivePresentation.Slides.Count
ActivePresentation.Slides(LastSlide).SlideShowTransition.Hidden = msoFalse
FileName = CreateObject("Scripting.FileSystemObject").GetBaseName(ActivePresentation.Name)
Path = Environ("USERPROFILE") & "\Downloads\"
'DELETE ALL PPT FILES
Set FSO = CreateObject("scripting.filesystemobject")
On Error Resume Next
FSO.deletefile Path & "*" & ".ppt*"
ActivePresentation.SaveAs Path & FileName
SlideShowWindows(1).View.GotoSlide 2
End Sub
Sub FINISH()
sReturn = InputBox("PLEASE ENTER NAME AND SURNAME:")
DoEvents
LastSlide = ActivePresentation.Slides.Count
ActivePresentation.Slides(1).SlideShowTransition.Hidden = msoTrue
ActivePresentation.Slides(LastSlide).SlideShowTransition.Hidden = msoTrue
FileName = CreateObject("Scripting.FileSystemObject").GetBaseName(ActivePresentation.Name)
Path = Environ("USERPROFILE") & "\Desktop\"
PdfFileNm = sReturn & " - " & FileName
ActivePresentation.SlideShowWindow.View.Exit
Set ppt = CreateObject("PowerPoint.Application")
ppt.ActivePresentation.SaveAs Path & PdfFileNm & ".pdf", 32
ActivePresentation.Slides(1).SlideShowTransition.Hidden = msoFalse
ActivePresentation.Slides(LastSlide).SlideShowTransition.Hidden = msoFalse
ActivePresentation.RemoveDocumentInformation (ppRDIInkAnnotations)
Dim txtbx As TextBox
Dim chkBox As CheckBox
Dim shp As Shape
Dim sld As Slide
For n = 1 To LastSlide
Set sld = ActivePresentation.Slides(n)
For Each shp In sld.Shapes
If shp.Type = msoOLEControlObject Then
If shp.Name Like "TextBox?" Then
Set txtbx = shp.OLEFormat.Object
txtbx.Text = ""
End If
End If
If shp.Type = msoOLEControlObject Then
If shp.Name Like "CheckBox?" Then
Set chkBox = shp.OLEFormat.Object
chkBox.Value = False
End If
End If
Next
Next
DoEvents
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "<BODY style=;font-family:Calibri>Hi,<br><br> Please see attached " & FileName & ".<br>"
On Error Resume Next
With OutMail
.display
.To = ""
.CC = ""
.Subject = FileName
.HTMLBody = strbody & .HTMLBody & Signature
.Attachments.Add Path & PdfFileNm & ".PDF"
.display
End With
MsgBox "EMAIL ATTACHMENT HAS BEEN UPLOADED", vbSystemModal
OutMail.display
Set OutMail = Nothing
Set OutApp = Nothing
Dim FSO As Object
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefile Path & PdfFileNm & ".pdf", True
End Sub
`
If i use a simple code , is not saving the ink
`Sub SaveandExportq()
Application.ActivePresentation.Export "C:\Users\danio\Desktop\New folder", "pdf", 32
End Sub`
I also tried to do the file read only but i cannot run the code on read only . with the first code the issue is that they can edit the file and with the second is not saving the ink .
Thank you in advance :)

How to attach PDF files from a folder?

I need to attach variable PDFs from a transport folder.
Sub send_attachent()
Dim OutApp As Object
Dim OutMAil As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMAil = OutApp.CreateItem(0)
strbody = "<BODY style = font-size:12pt; font-familt:Arial>" & _
"Please find attached High Risk Defect:<br><br> AT300-HRD-00<br><br> Issue<br><br>" & _
"Regards,<br>"
On Error Resume Next
With OutMAil
.TO = "julia.naydenova#hitachirail.com"
.CC = "jean.ash#hitachirail.com"
.BCC = ""
.Subject = "AT300-HRD-00"
.Display
.HTMLBody = strbody & .HTMLBody
.Attachments.Add "I:\ServiceDelivery\MaintenanceManuals\AT300\TRANSPORT FOLDER\AT300-HRD-00031 Test.pdf"
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
End With
MsgBox "Email Sent"
On Error GoTo 0
Set OutMAil = Nothing
End Sub
I need to send variable files, so whatever I put in the folder to be attached on the email. With the file name in the macro I can only send one file.
The Outlook object model doesn't provide anything for monitoring files in a folder on the disk. You need to create a file-watcher which can monitor files in a folder and create a new mail item when a file is added to the folder. See VBA monitor folder for new files for more information on that.
Loop through files in a folder with Dir.
Option Explicit
Sub send_all_PDF_folder()
Dim outApp As Object
Dim outMail As Object
Dim strbody As String
Dim filePath As String
Dim fileName As String
Set outApp = CreateObject("Outlook.Application")
Set outMail = outApp.CreateItem(0)
strbody = "<BODY style = font-size:12pt; font-familt:Arial>" & _
"Please find attached High Risk Defect:<br><br> AT300-HRD-00<br><br> Issue<br><br>" & _
"Regards,<br>"
filePath = "I:\ServiceDelivery\MaintenanceManuals\AT300\TRANSPORT FOLDER"
With outMail
.To = "someone#somewhere.com"
.CC = "someoneCC#somewhere.com"
.Subject = "AT300-HRD-00"
.Display
.HtmlBody = strbody & .HtmlBody
fileName = dir(filePath & "\*.pdf")
Do While fileName <> ""
.Attachments.Add filePath & fileName
fileName = dir
Loop
End With
Set outMail = Nothing
Set outApp = Nothing
End Sub

Attach all CSV files in a folder

I'm running a script to automatically send emails with attachments. All attachments will have a .csv extension.
I do not know the name of the files before hand. I am using the Dir statement.
I tried breaking the Dir statement into different strings, but that was not working either.
Dim cAttachment As String
Dim Folder As String
Dim fileCriteria As String
Folder = "C:\Users\____\Desktop\Test Folder"
fileCriteria = ".csv"
cAttachment = Dir(Folder & "\*" & fileCriteria)
I also tried:
Dim cAttachment As String
cAttachment = Dir("C:\Users\___\Desktop\Test Folder\*.csv")
I get
expected end of statement
on the leading parenthesis of my Dir statement.
You can easily achieve your result without having to use the old Dir() Function. To do that you need to use "Scripting.FileSystemObject".
This is the code to discover all files with .csv extension in a specific folder:
Dim oFile As Object
Dim oFSO As Object
Dim oFolder As Object
Dim oFiles As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:\my\Folder\") 'Set this accordingly
Set oFiles = oFolder.Files
'For all files in the folder
For Each oFile In oFiles
If (oFile Like "*.csv") Then
'Add this file to attachments
objMessage.AddAttachment oFile.Path
End If
Next
Hope this helps.
Should be
Folder = "C:\Users\____\Desktop\Test Folder\"
cAttachment = Dir(Folder & "*.csv")`
'// Loop to attch
Do While Len(cAttachment ) > 0
.Attachments.Add Folder & cAttachment
Atmt_File = Dir
Loop
Full Example code
Option Explicit
Private Sub Example()
Dim olMsg As Outlook.MailItem
Dim olRecip As Outlook.Recipient
Dim Atmt_Path As String
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim rng As Object
Dim Atmt_File As String
'// Attachments Path.
Atmt_Path = "C:\Temp\"
'// Create the message.
Set olMsg = Application.CreateItem(olMailItem)
With olMsg
.Display '// This line must be retained
Atmt_File = Dir(Atmt_Path & "*.csv")
'// Loop to attch
Do While Len(Atmt_File) > 0
.Attachments.Add Atmt_Path & Atmt_File
Atmt_File = Dir
Loop
'// Cancell email if no files to send
If .Attachments.Count = 0 Then
'MsgBox "There are no reports to attach.", vbInformation
.Close 0
.Delete
Else
'// Add the To recipient(s)
Set olRecip = .Recipients.Add("0m3r#email.com")
Set olRecip = .Recipients.Add("0m3r#email.com")
olRecip.Type = olTo
'// Add the CC recipient(s)
Set olRecip = .Recipients.Add("0m3r#email.com")
olRecip.Type = olCC
'// Set the Subject, Body, and Importance of the message.
.Subject = "Reports - " & Format(Now, "Long Date")
.Importance = olImportanceHigh '// High importance
.BodyFormat = olFormatHTML
'// Edit the message body.
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
'// Set message body (to retain the signature)
Set rng = wdDoc.Range(0, 0)
'// add the text to message body
rng.Text = "Files are Attached, Thank you" & vbCrLf & vbCrLf
'// Resolve each Recipient's name.
For Each olRecip In .Recipients
olRecip.Resolve
If Not olRecip.Resolve Then
olMsg.Display
End If
Next
' .Send '//This line optional
End If
End With
End Sub

Difficulties sending worksheet through email via SendWorksheet button

I have an Excel 2016 worksheet with a "Send Worksheet" button purposed to email the worksheet to all the designated recipients. When I run the following code (most of which came from another program and tweaked), I receive the following errors:
Runtime Error 429: ActiveX component can't create object.
at Set OutlookApp = CreateObject("Outlook.Application")
as well as
Runtime Error 91: Object variable or With block variable not set.
in the With block at .To = "email address".
Option Explicit
Private Sub cmdSendWorksheet_Click()
Dim xFile As String
Dim xFormat As Long
Dim Wb As Workbook
Dim Wb2 As Workbook
Dim FilePath As String
Dim FileName As String
Dim OutlookApp As Object
Dim OutlookMail As Object
'On Error Resume Next
Application.ScreenUpdating = False
Set Wb = Application.ActiveWorkbook
ActiveSheet.Copy
Set Wb2 = Application.ActiveWorkbook
Select Case Wb.FileFormat
Case xlOpenXMLWorkbook:
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
Case xlOpenXMLWorkbookMacroEnabled:
If Wb2.HasVBProject Then
xFile = ".xlsm"
xFormat = xlOpenXMLWorkbookMacroEnabled
Else
xFile = ".xlsm"
xFormat = xlOpenXMLWorkbook
End If
End Select
FilePath = Environ$("temp") & "\"
FileName = Wb.Name & Format(Now, "dd-mmm-yy h-mm-ss")
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
With OutlookMail
.To = "email address"
.CC = ""
.BCC = ""
.Subject = "Worksheet Attached"
.Body = "Please see attached worksheet"
.cmdSendWorksheet.Enabled = True
.Attachments.Add Wb2.FullName
.Send
End With
Wb2.Close
Kill FilePath & FileName & xFile
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True
End Sub
this code should do the job you need. But you need to go in Tools / References and check the following reference :
Microsoft Scripting Runtime
Microsoft Outlook 14.0 Object Library
Private Sub cmdSendWorksheet_Click()
Dim Wb As Workbook
Dim FilePath As String
Dim FileName As String
Dim FileExtensionName As String
Dim FileFullPath As String
Dim OutlookApp As New Outlook.Application
Dim OutlookMail As Outlook.MailItem
Dim fso As New FileSystemObject
'On Error Resume Next
Application.ScreenUpdating = False
Set Wb = ThisWorkbook
FilePath = Environ$("temp") & "\"
FileName = fso.GetBaseName(Wb.Path & "\" & Wb.Name) & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtensionName = fso.GetExtensionName(Wb.Path & "\" & Wb.Name)
FileFullPath = FilePath & FileName & "." & FileExtensionName
fso.CopyFile Wb.Path & "\" & Wb.Name, FileFullPath
'Sending the email
Set OutlookMail = OutlookApp.CreateItem(olMailItem)
With OutlookMail
.To = "email address"
.CC = ""
.BCC = ""
.Subject = "Worksheet Attached"
.Body = "Please see attached worksheet"
.Attachments.Add FileFullPath
.Display
'.Send You can chose .Send or .Display, as you wish
End With
Kill FileFullPath
'Free the memory
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Set fso = Nothing
Application.ScreenUpdating = True
Application.Quit
End Sub

Outlook: Attach a file with a dynamic name

Argh, Object doesn't support this property or method error 438!
I haven't tried this but, I think it might work:
objFS.System.IO.Path.GetFileName(fileName)
if objFS.System.IO.Path.GetFileName(fileName) = "VS12_WID1" Then
fileName = AFile.Name
getFileName = filePath & "/" & fileName
I should simplfy it more then try to rename the file.
Sub AddAttachment()
Dim myAttachments As Outlook.Attachments
Dim getFile, fileName, filePath As String
Set filePath = "F:\"
Set fileName = "V_W_*_*_.pdf"
Set getFile = "filePath" & "fileName"
Set MyApp = CreateObject("Outlook.Application")
Set myItem = MyApp.CreateItem(0)
Set myAttachments = myItem.Attachments
With myItem
.To = "email#mail.com"
.CC = ""
.Subject = "test"
myAttachments.Add getFile
.ReadReceiptRequested = False
.HTMLBody = "Report(s) Attached"
End With
myItem.Send
End Sub
I'm getting a compile error: Object required highlighting both Sub AddAttachment() and Set filePath. I feel so close to making this work!
UPDATED CODE:
Sub AddAttachment()
Dim myAttachments As Outlook.Attachments
Dim getFileName, fileName, filePath As String
Dim objFS: Set objFS = CreateObject("Scripting.FileSystemObject")
Set filePath = "F:\"
Set getFileName = filePath & fileName
Set MyApp = CreateObject("Outlook.Application")
Set myItem = MyApp.CreateItem(0)
Set myAttachments = myItem.Attachments
For Each fileName In filePath
If fcase(objFS.GetExtensionName(fileName)) = "VS111111_WID111A" Then
fileName = "VS111111_WID111A.pdf"
Exit For
End If
Next
With myItem
.To = "email#mail.com"
.CC = ""
.Subject = ""
myAttachments.Add getFileName
.ReadReceiptRequested = False
.HTMLBody = "Report(s) Attached"
End With
myItem.Send
End Sub
I have enough knowledge to read the script to understand what is going on. The code I made can only find a fixed file name. How can the file name be made dynamic?
Sub AddAttachment()
Dim myAttachments As Outlook.Attachments
Set MyApp = CreateObject("Outlook.Application")
Set myItem = MyApp.CreateItem(0)
Set myAttachments = myItem.Attachments
With myItem
.To = "email#address.com"
.CC = "email#address.com"
.Subject = ""
myAttachments.Add "F:\constantFilenameHas8char_constantFilenameHas7char_variableHas5Int_todaysModifiedDate_variableHas6Int.pdf"
.ReadReceiptRequested = False
.HTMLBody = "Report(s) Attached"
End With
myItem.Send
End Sub
Your query is not clear on how you want to get the filename.Think of using
a variable and pass the filepath and name as you required.
dim FileToAttach as string
FileToAttach ="FilePath" & "Filename"
myAttachments.Add FileToAttach
For your updated code
Sub AddAttachment()
Dim myAttachments As Outlook.Attachments
Dim getFileName, filename
Dim filePath As Object
Dim objFS As FileSystemObject
Set objFS = New FileSystemObject
Set filePath = objFS.GetFolder("C:\Users\Dinesh\Desktop\")
Set MyApp = CreateObject("Outlook.Application")
Set myItem = MyApp.CreateItem(0)
Set myAttachments = myItem.Attachments
For Each AFile In filePath.Files
Debug.Print UCase(objFS.GetExtensionName(fileName))
If UCase(objFS.GetExtensionName(AFile)) = "PDF" Then
fileName = AFile.Name
getFileName = filePath & "/" & fileName
Exit For
End If
Next
With myItem
.To = ""
.CC = ""
.Subject = ""
myAttachments.Add getFileName
.ReadReceiptRequested = False
.HTMLBody = "Report(s) Attached"
.Display
End With
'myItem.Send
End Sub