Problem with saving as PDF, problem with file name - vba

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

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.

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

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.