Mailmerge - connection settings vba - 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.

Related

Get time in filename save in word macro

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")

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

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.

saving excel as pdf in active directory

I have some code to save a specific excel worksheet in a specific location. I'd like to change the code in order to save in the same directory that the file was originally opened in. Currently it's set to save in my C:\ but if I had opened the file from a different directly, I would like it saved there.
Sheets("Lease Charts").Activate
With Sheets("Lease Charts")
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="C:\Temp " & Format(Range("L1"), "mm-dd-yyyy"), _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With
Just you ThisWorkbook.Path Like so:
With Sheets("Lease Charts")
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "\Temp " & Format(Range("L1"), "mm-dd-yyyy"), _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With
Figured it out by just removing the specific directory from the filename !
Sheets("Lease Charts").Activate
With Sheets("Lease Charts")
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="Temp " & Format(Range("L1"), "mm-dd-yyyy"), _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With

trouble with 2010 save as dbf

Below is the code I'm trying to fix. It works in Excel 2007 but not in Excel 2010.
I believe it has to do with the XlDBF4 part. Please help.
Range("A1").Select
ActiveWorkbook.SaveAs Filename:=Range("SetUp!L34") & Range("SetUp!D28") & "_" & Range("SetUp!F28") & ".dbf", FileFormat:= _
xlDBF4, CreateBackup:=False
ActiveWorkbook.SaveAs Filename:=Range("Setup!L28") & Range("SetUp!D28") & "_" & Range("SetUp!F28") & " " & Range("SetUp!D30") & ".xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
Try this code instead:
Range("A1").Select
ActiveWorkbook.SaveAs Filename:=Range("SetUp!L34") & Range("SetUp!D28") & "_" & Range("SetUp!F28") & ".dbf", FileFormat:= _
xlDBF4, CreateBackup:=False
ActiveWorkbook.SaveAs Filename:=Range("Setup!L28") & Range("SetUp!D28") & "_" & Range("SetUp!F28") & " " & Range("SetUp!D30") & ".xls", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
I think the case is xlNormal: if you want xls - you should use xlExcel8 instead. It's strange your code works - there's no xlNormal constant at all: XlFileFormat Enumeration (Excel)