Save Word file in multiple locations at once - vba

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.

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

Save multiple sheets into PDF

im trying to save multiple worksheets as single pdf file.
Following macro is using Sheets names, but it does not fit me well as my sheets have dynamic names. Was trying to use Sheets(1) addressing but did not work. Does anyone have any idea?
Sub export_to_pdf()
Sheets(Array("Configuration", "chart")).Copy
ActiveWorkbook.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="filename.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
ActiveWorkbook.Close (False)
End Sub
use
Sheets(1).ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="filename.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
or if you have more
Sheets(Array(1, 2, 5)).Copy
ActiveWorkbook.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="filename.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
ActiveWorkbook.Close (False)
Your program code works for me with minor changes indicate here-under.
Sub export_to_pdf()
Sheets(Array("Sheet1", "Sheet2")).Copy 'Change to your sheets
ActiveWorkbook.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="filename.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _ 'changed to True
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
ActiveWorkbook.Close (False)
End Sub
In such cases I set reference to Microsoft Word Object Library also in Tools Reference

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

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.