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
Related
I have 2 sheets in a workbook
Sheet 1 - Where A2 and onwards I have numbers
Sheet named "LOC" , where i put 1 number at a time recalculate and formate and save it
the process has to repeat for all number entered in Sheet 1 - A2 and Below till the end of Column
Please help me loop this
I have to copy each number from Sheet 1 and paste it in heet named "LOC" in C2 and repeat the process again
Sub MultipleSOA()
'1st SOA
Sheets("Sheet1").Select
Range("A2").Select
Selection.Copy
Sheets("Loc ").Select
Range("C2").Select
ActiveSheet.Paste
ActiveSheet.Calculate
Range("B9:G9").Select
Cells.Replace What:="PCL-", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="SCL-", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="PSI-", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="CL-", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Range("B9:G9").Select
Range("C4").Select
Columns("C:C").ColumnWidth = 44.29
Range("C4").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\XXXX\Desktop\SOA\" & ActiveSheet.Range("B9").Value & " - " & ActiveSheet.Range("C2").Value & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
End Sub
I haven't tested it, but try the following code...
Option Explicit
Sub MultipleSOA()
Dim varItemsToReplace As Variant
Dim varItem As Variant
Dim wksSource As Worksheet
Dim wksDest As Worksheet
Dim rngSource As Range
Dim rngCell As Range
varItemsToReplace = Array("PCL-", "SCL-", "PSI-", "CL-")
Set wksSource = Worksheets("Sheet1")
Set wksDest = Worksheets("Loc")
With wksSource
Set rngSource = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With
For Each rngCell In rngSource
With wksDest
.Range("C2").Value = rngCell.Value
.Calculate
For Each varItem In varItemsToReplace
.Range("B9:G9").Replace _
What:=varItem, _
Replacement:="", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Next varItem
.Columns("C:C").ColumnWidth = 44.29
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="C:\Users\XXXX\Desktop\SOA\" & .Range("B9").Value & " - " & .Range("C2").Value & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With
Next rngCell
End Sub
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.
I have a file has 4 columns (A B C D) with the "Manager" in column A and the Employee numbers in Column B with example info per employee in C & D. What I would like to do is to be able to have a print macro that would print all the employees under each manager on a PDF with the headers repeating at the top. So when the print macro is clicked on, it would ask, "Which Manager"? Then it would be clicked and then it would print the PDF.
Is this possible? I've been researching loops and I was thinking that would be the way to do it but cannot seem to figure out how to replace my "Range" below. It is example code that I use in another file with static ranges.
Any help is much appreciated! Thank you. P.S. sorry about the code formatting, I cannot seem to get used to Stack Overflow's way of editing it.
Sub Dept_BGT_Print()
Range("C14:R45").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"John Dept BGT.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
Range("C47:R59").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"HR Dept BGT.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
Range("C62:R126").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Marketing & Sales Dept BGT.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
Range("C128:R207").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Skip Dept BGT.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
Range("C209:R250").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Amy Dept BGT.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End Sub
If you're interested in printing one manager at a time, your best option is probably to just go with an AutoFilter.
Sub Dept_BGT_Print()
Dim Sel_Manager As String
'Specify headers to be repeated at the top
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
.PrintTitleColumns = ""
End With
'Manager selection through simple Inputbox
Sel_Manager = InputBox("Which manager?")
'Insert autofilter for worksheet
Cells.Select
Selection.AutoFilter
'Select manager defined in inputbox
ActiveSheet.Range("A1", Range("D1").End(xlDown)).AutoFilter Field:=1, Criteria1:=Sel_Manager
'Select range to be printed and specify manager in filename
ActiveSheet.Range("A1", Range("D1").End(xlDown)).Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Employees for manager " & manager & ".pdf", Quality:=xlQualityStandard,
_IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
'remove autofilter
ActiveSheet.ShowAllData
End Sub
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
I am trying to print an active sheet as a PDF, is it also possible to print page 1 and 2 separately? i have this so far. i want the name of the export to use the values from cells B1 I1 and J1
Sub PDFActiveSheet()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler
Set ws = ActiveSheet
'enter name and select folder for file
' start in current workbook folder
strFile = Replace(Replace(ws.Name, " ", ""), ".", "_") _
& "_" _
& Range("B1"&"I1"&"J1") _
& ".pdf"
strFile = ThisWorkbook.Path & "\" & strFile
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
If myFile <> "False" Then
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
MsgBox "PDF file has been created."
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
Just use the From and To parameters of the ExportAsFixedFormat function:
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False, _
From:=1, _
To:=1
and the same again with page 2