
No script que estou usando eu tenho
Sub Saveaspdfandsend()
Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range
Set xSht = ActiveSheet
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
If xFileDlg.Show = True Then
xFolder = xFileDlg.SelectedItems(1)
Else
MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
Exit Sub
End If
xFolder = xFolder + "\" + xSht.Name + Format(Now, " yyyy-mm-dd hmmAM/PM") + ".pdf"
'Check if file already exist
If Len(Dir(xFolder)) > 0 Then
xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
vbYesNo + vbQuestion, "File Exists")
On Error Resume Next
If xYesorNo = vbYes Then
Kill xFolder
Else
MsgBox "if you don't overwrite the existing PDF, I can't continue." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
Exit Sub
End If
If Err.Number <> 0 Then
MsgBox "Unable to delete existing file. Please make sure the file is not open or write protected." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
Exit Sub
End If
End If
Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
'Save as PDF file
xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard
'Create Outlook email
Set xOutlookObj = CreateObject("Outlook.Application")
Set xEmailObj = xOutlookObj.CreateItem(0)
With xEmailObj
.Display
.To = ""
.CC = "[email protected]"
.Subject = xSht.Name + ".pdf"
.Attachments.Add xFolder
If DisplayEmail = False Then
'.Send
End If
End With
Else
MsgBox "The active worksheet cannot be blank"
Exit Sub
End If
End Sub
que mostrará 2018 04 06 245PM. Tentei colocar um : entre h e mm mas deu um erro ao executar o script. Posso usar um espaço ou um arquivo . mas não um: Isso não é possível?
Responder1
No Windows, os nomes dos arquivos não podem conter o caractere :
. É por isso que ocorre um erro em tempo de execução quando você tenta salvar o arquivo.
Você também deve usar o formato 24h, em vez de AM/PM: hhmm
parece mais seguro para mim.
VerificarFunção de formato (Visual Basic para aplicativos)Para maiores informações.
Responder2
Para um carimbo de data e hora adequado, sua linha de comando VB deve ser,
Format(now(), "yyyy-MM-dd hh:mm:ss")
ou
Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")
Nota: DateTime.Now
retorna um valor do tipo de dados Data. Variáveis de data exibem datas de acordo com o formato abreviado de data e hora definido em seu sistema.
Para exibir AM/PM, altere sua string de formato para isto:
Format(TimeValue(Now), 'hh:mm AM/PM')