Get time in filename save in word macro - vba

I have a word macro that is saving the doc as a pdf and emailing it. The only problem is that it will overwrite itself on the network drive. I can't figure out how to get the seconds into the filename with the code below.
Thanks for the help!
dte = Date
savedName = "Night Orders - " & Format(dte, "yyyy-mm-dd")
Response = MsgBox("Save and email the current document?", vbOKCancel, "Are you sure?")
If Response = vbOK Then
savePath = saveDir & saveName & ".pdf"
ActiveDocument.ExportAsFixedFormat OutputFileName:=savePath, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, _
OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
From:=1, To:=1, Item:=wdExportDocumentContent, IncludeDocProps:=True, _
KeepIRM:=True, CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False

Try:
savedName = "Night Orders - " & Format(Now, "YYYY-MM-DD-hh-mm-ss")

Related

Problem with saving as PDF, problem with file name

I have such problem with my Code, I have error like this below:
Selection.MoveDown Unit:=wdLine, Count:=20
Selection.Expand wdLine
Selection.Font.Color = -603914241
strDocName = (Selection.Text)
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"\\tsclient\D\" & strDocName & ".pdf", ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _
True, UseISO19005_1:=False
ChangeFileOpenDirectory "\\tsclient\D\"
I have error like this
Run time error 2147467259 (8004005)
Really need help this is my last step and it's failing

VBA: Export Word Doc to PDF with a different file name

Currently I am using the code below which saves the word doc into a PDF with its default file name. I want to change it such that I can modify the PDF file name. Appreciate it!
Code:
Sub Silent_save_to_PDF()
'
' Silent Save_to_PDF Macro
'
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
Replace(ActiveDocument.FullName, ".docx", ".pdf") , _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, Item:= _
wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub
Just set the file name explicitly.
Sub Silent_save_to_PDF()
Dim extra_text As String
Dim file_name As String
extra_text = "123"
file_name = ActiveDocument.Path & "\" & Left(ActiveDocument.Name, InStrRev(ActiveDocument.Name, ".") - 1) & extra_text & ".pdf"
ActiveDocument.ExportAsFixedFormat OutputFileName:=file_name, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, Item:= _
wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub

How to indicate location of file when sending email using VBA

seems to almost work but the strlocation is showing me that there is no "\" between name of file and it's location. How do I add that.
Sub Macro1()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Range("f6").Text, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Set Mail_Object = CreateObject("Outlook.Application")
With Mail_Object.CreateItem(o)
.Subject = Range("f6").Text
.To = "EMAIL"
.Body = "Daily movement file attached" & Chr(13) & Chr(13) & _
"Regards," & Chr(13) & "SCD" & Chr(13) & "AAAAAA"
strlocation = "C:\Users\User\Desktop\macro save" & Range("f6").Text & ".pdf"
.Attachments.Add (strlocation)
.Send
End With
Set Mail_Object = Nothing
End Sub
So you're only missing a /?
Change this:
strlocation = "C:\Users\User\Desktop\macro save" & Range("f6").Text & ".pdf"
...to this:
strlocation = "C:\Users\User\Desktop\macro save\" & Range("f6").Text & ".pdf"

Save Word file in multiple locations at once

I am trying to save a word file in 5 different locations. My main problem is that my code only works if I specify a name in the save as part. I tried this, but with no luck:
ChangeFileOpenDirectory _
"O:\xxxx"
ActiveDocument.SaveAs FileName:=
"O:\xxxx" & Split(ActiveDocument.Name, ".")(0) & ".doc", _
, FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=15
ChangeFileOpenDirectory _
"O:\xxx"
ActiveDocument.SaveAs2 FileName:= _
O:\xxxx" & Split(ActiveDocument.Name, ".")(0) & ".doc", _
, FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=15
ChangeFileOpenDirectory _
"O:\xxx"
ActiveDocument.SaveAs2 FileName:= _
O:\xxxx" & Split(ActiveDocument.Name, ".")(0) & ".doc", _
, FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=15
ChangeFileOpenDirectory _
"O:xxxx"
ActiveDocument.SaveAs2 FileName:= _
O:\xxxx" & Split(ActiveDocument.Name, ".")(0) & ".doc", _
, FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=15
ChangeFileOpenDirectory _
"O:\xxx"
ActiveDocument.SaveAs2 FileName:= _
O:\xxxx" & Split(ActiveDocument.Name, ".")(0) & ".doc", _
, FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=15
The file paths are all different but the filename should be the same. It basically should just take the name of the opened document.
well, I would do it in a loop:
Sub daf()
Dim docCopy As Document
Dim sPath(4) As String
Dim sFileName As String
Dim i As Long
sPath(0) = "C:\zzz"
sPath(1) = "c:\ddd"
sPath(2) = "C:\ttt"
sPath(3) = "C:\yyy"
sPath(5) = "C:\ooo"
sFileName = Split(ActiveDocument.Name, ".")(0)
Set docCopy = Application.Documents.Add(ActiveDocument.FullName)
For i = 0 To UBound(sPath)
docCopy.SaveAs2 sPath & "\" & sFileName & ".doc", 12
Next i
End Sub
You can add more arguments to saveAs2 if you wish, as you did in your original macro.

Mailmerge - connection settings vba

I have some problem with my mailmerge macro in this part:
wdocSource.Mailmerge.OpenDataSource _
Name:=strWorkbookName, _
AddToRecentFiles:=False, _
Revert:=False, _
Format:=wdOpenFormatAuto, _
Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
SQLStatement:="SELECT * FROM `Mailing$`"
Can somebody help me change the connection settings? I am try many ways but always dont run correctly.