Printing excel files that match cell, but first file doesnt print2019 Community Moderator ElectionHow to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?What is correct content-type for excel files?How to add excel range as a picture to outlook message bodyExcel 2010 VBA code to reference multiple cells from one worksheet into the summary workshetHow to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loopsVBA to split multi-line text in a excel cell into separate rows and keeping adjacent cell valuesExport multiple (specific) worksheets to CSV files in a specified directoryexcel replace function in access vbaExcel VBA macro to send emails to unique users in rangeHow to stop certain rows from printing if one single cell is blank
Matrix using tikz package
Can other pieces capture a threatening piece and prevent a checkmate?
Can you move over difficult terrain with only 5 feet of movement?
Pronounciation of the combination "st" in spanish accents
Recruiter wants very extensive technical details about all of my previous work
Light propagating through a sound wave
Should I use acronyms in dialogues before telling the readers what it stands for in fiction?
Fewest number of steps to reach 200 using special calculator
Why didn't Héctor fade away after this character died in the movie Coco?
World War I as a war of liberals against authoritarians?
How to generate binary array whose elements with values 1 are randomly drawn
Why is there so much iron?
Print a physical multiplication table
Help rendering a complicated sum/product formula
Suggestions on how to spend Shaabath (constructively) alone
Does .bashrc contain syntax errors?
What are substitutions for coconut in curry?
I got the following comment from a reputed math journal. What does it mean?
What favor did Moody owe Dumbledore?
Could Sinn Fein swing any Brexit vote in Parliament?
Deletion of copy-ctor & copy-assignment - public, private or protected?
What is the significance behind "40 days" that often appears in the Bible?
Do US professors/group leaders only get a salary, but no group budget?
Asserting that Atheism and Theism are both faith based positions
Printing excel files that match cell, but first file doesnt print
2019 Community Moderator ElectionHow to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?What is correct content-type for excel files?How to add excel range as a picture to outlook message bodyExcel 2010 VBA code to reference multiple cells from one worksheet into the summary workshetHow to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loopsVBA to split multi-line text in a excel cell into separate rows and keeping adjacent cell valuesExport multiple (specific) worksheets to CSV files in a specified directoryexcel replace function in access vbaExcel VBA macro to send emails to unique users in rangeHow to stop certain rows from printing if one single cell is blank
I have a macro that runs through the filenames in col B and if it finds a text string that matches a file from a directory then it will export it as a pdf.
For some reason the the loop doesn't print the first file in the list. When i step thru then they all print.
Does anyone see anything in my code that could solve this problem?
Thanks
EDIT: problem was that enabling the PrintCommunication
was making the first sheet fail. PrintCommunication
is needed off tho so the PageSetup works as needed.
Code is updated below for anyone to use. All you would have to change is the MyFolder location or use With Application.FileDialog(msoFileDialogFolderPicker)
to pick the folder.
Sub ExcelPrint()
Application.DisplayAlerts = False
Dim OpenFile As Variant
Dim MyFile As String, MyFolder As String, AorB As String
Dim sourceFolder As String, destFolder As String
Dim LR As Long
MyFolder = ThisWorkbook.ActiveSheet.Range("B1").Value
LR = Cells(Rows.Count, "B").End(xlUp).Row
sourceFolder = Environ("Userprofile") & "DesktopPDF"
destFolder = sourceFolder & "_Temp"
If Len(Dir(sourceFolder, vbDirectory)) = 0 Then
MkDir sourceFolder
End If
If Len(Dir(destFolder, vbDirectory)) = 0 Then
MkDir destFolder
End If
On Error GoTo eHandler
With Worksheets("Print")
For i = 3 To LR
MyFile = ThisWorkbook.ActiveSheet.Cells(i, 2)
AorB = Mid(Cells(i, 2), 4, 1)
OpenFile = MyFolder & MyFile & ".xlsx"
Application.StatusBar = "Printing " & MyFile & ".xlsx"
Workbooks.Open _
FileName:=OpenFile, _
ReadOnly:=True
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.FitToPagesTall = 1
.FitToPagesWide = 1
If AorB = "A" Then
.PaperSize = xlPaperLetter
ElseIf AorB = "B" Then
.PaperSize = xlPaperTabloid
End If
End With
'This was the problem
'Application.PrintCommunication = True
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=destFolder & MyFile & ".pdf", _
Quality:=xlQualityStandard, _
IgnorePrintAreas:=False, _
From:=1, To:=1, _
OpenAfterPublish:=True
ActiveWorkbook.Close
Application.StatusBar = vbNullString
'Green Highlight if printed
With ThisWorkbook.Sheets("Print")
Application.ScreenUpdating = True
If Not Dir(destFolder & MyFile & ".pdf", vbDirectory) = vbNullString Then
Sheets("Print").Cells(i, 2).Interior.ColorIndex = 10
Sheets("Print").Cells(i, 2).Font.Color = vbWhite
End If
Application.ScreenUpdating = False
End With
nextLine:
Next i
End With
Exit Sub
eHandler:
If err.Number = 1004 Then
Resume nextLine
ElseIf err.Number = 424 Then
Resume nextLine
ElseIf err.Number = 9 Then
Resume nextLine
End If
MsgBox err.Number & vbCrLf & err.Description
Application.StatusBar = vbNullString
Application.DisplayAlerts = True
End Sub
excel vba
|
show 2 more comments
I have a macro that runs through the filenames in col B and if it finds a text string that matches a file from a directory then it will export it as a pdf.
For some reason the the loop doesn't print the first file in the list. When i step thru then they all print.
Does anyone see anything in my code that could solve this problem?
Thanks
EDIT: problem was that enabling the PrintCommunication
was making the first sheet fail. PrintCommunication
is needed off tho so the PageSetup works as needed.
Code is updated below for anyone to use. All you would have to change is the MyFolder location or use With Application.FileDialog(msoFileDialogFolderPicker)
to pick the folder.
Sub ExcelPrint()
Application.DisplayAlerts = False
Dim OpenFile As Variant
Dim MyFile As String, MyFolder As String, AorB As String
Dim sourceFolder As String, destFolder As String
Dim LR As Long
MyFolder = ThisWorkbook.ActiveSheet.Range("B1").Value
LR = Cells(Rows.Count, "B").End(xlUp).Row
sourceFolder = Environ("Userprofile") & "DesktopPDF"
destFolder = sourceFolder & "_Temp"
If Len(Dir(sourceFolder, vbDirectory)) = 0 Then
MkDir sourceFolder
End If
If Len(Dir(destFolder, vbDirectory)) = 0 Then
MkDir destFolder
End If
On Error GoTo eHandler
With Worksheets("Print")
For i = 3 To LR
MyFile = ThisWorkbook.ActiveSheet.Cells(i, 2)
AorB = Mid(Cells(i, 2), 4, 1)
OpenFile = MyFolder & MyFile & ".xlsx"
Application.StatusBar = "Printing " & MyFile & ".xlsx"
Workbooks.Open _
FileName:=OpenFile, _
ReadOnly:=True
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.FitToPagesTall = 1
.FitToPagesWide = 1
If AorB = "A" Then
.PaperSize = xlPaperLetter
ElseIf AorB = "B" Then
.PaperSize = xlPaperTabloid
End If
End With
'This was the problem
'Application.PrintCommunication = True
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=destFolder & MyFile & ".pdf", _
Quality:=xlQualityStandard, _
IgnorePrintAreas:=False, _
From:=1, To:=1, _
OpenAfterPublish:=True
ActiveWorkbook.Close
Application.StatusBar = vbNullString
'Green Highlight if printed
With ThisWorkbook.Sheets("Print")
Application.ScreenUpdating = True
If Not Dir(destFolder & MyFile & ".pdf", vbDirectory) = vbNullString Then
Sheets("Print").Cells(i, 2).Interior.ColorIndex = 10
Sheets("Print").Cells(i, 2).Font.Color = vbWhite
End If
Application.ScreenUpdating = False
End With
nextLine:
Next i
End With
Exit Sub
eHandler:
If err.Number = 1004 Then
Resume nextLine
ElseIf err.Number = 424 Then
Resume nextLine
ElseIf err.Number = 9 Then
Resume nextLine
End If
MsgBox err.Number & vbCrLf & err.Description
Application.StatusBar = vbNullString
Application.DisplayAlerts = True
End Sub
excel vba
Is the first document name in B3 ? or is it in B2? You loop fromi = 3 to LR
Just verifying this isn't the issue.
– Emily Alden
Mar 7 at 17:20
my range of files are always after row 2. In this case the first excel file is in row 7. rows 3 - 6 are autocad files that the script is pulling the 1004 error which i tell it to resume nextline to continue
– Matt Taylor
Mar 7 at 17:33
When you run it does it not print any of the files or does it just skip the fist 'valid' file?
– Emily Alden
Mar 7 at 17:49
it skips the first valid file, prints the other perfectly then when it finished the first file is just sitting there in the background
– Matt Taylor
Mar 7 at 18:17
Have you tried changing which file is first to ensure it is a problem with that line and not the filename?
– Emily Alden
Mar 7 at 18:51
|
show 2 more comments
I have a macro that runs through the filenames in col B and if it finds a text string that matches a file from a directory then it will export it as a pdf.
For some reason the the loop doesn't print the first file in the list. When i step thru then they all print.
Does anyone see anything in my code that could solve this problem?
Thanks
EDIT: problem was that enabling the PrintCommunication
was making the first sheet fail. PrintCommunication
is needed off tho so the PageSetup works as needed.
Code is updated below for anyone to use. All you would have to change is the MyFolder location or use With Application.FileDialog(msoFileDialogFolderPicker)
to pick the folder.
Sub ExcelPrint()
Application.DisplayAlerts = False
Dim OpenFile As Variant
Dim MyFile As String, MyFolder As String, AorB As String
Dim sourceFolder As String, destFolder As String
Dim LR As Long
MyFolder = ThisWorkbook.ActiveSheet.Range("B1").Value
LR = Cells(Rows.Count, "B").End(xlUp).Row
sourceFolder = Environ("Userprofile") & "DesktopPDF"
destFolder = sourceFolder & "_Temp"
If Len(Dir(sourceFolder, vbDirectory)) = 0 Then
MkDir sourceFolder
End If
If Len(Dir(destFolder, vbDirectory)) = 0 Then
MkDir destFolder
End If
On Error GoTo eHandler
With Worksheets("Print")
For i = 3 To LR
MyFile = ThisWorkbook.ActiveSheet.Cells(i, 2)
AorB = Mid(Cells(i, 2), 4, 1)
OpenFile = MyFolder & MyFile & ".xlsx"
Application.StatusBar = "Printing " & MyFile & ".xlsx"
Workbooks.Open _
FileName:=OpenFile, _
ReadOnly:=True
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.FitToPagesTall = 1
.FitToPagesWide = 1
If AorB = "A" Then
.PaperSize = xlPaperLetter
ElseIf AorB = "B" Then
.PaperSize = xlPaperTabloid
End If
End With
'This was the problem
'Application.PrintCommunication = True
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=destFolder & MyFile & ".pdf", _
Quality:=xlQualityStandard, _
IgnorePrintAreas:=False, _
From:=1, To:=1, _
OpenAfterPublish:=True
ActiveWorkbook.Close
Application.StatusBar = vbNullString
'Green Highlight if printed
With ThisWorkbook.Sheets("Print")
Application.ScreenUpdating = True
If Not Dir(destFolder & MyFile & ".pdf", vbDirectory) = vbNullString Then
Sheets("Print").Cells(i, 2).Interior.ColorIndex = 10
Sheets("Print").Cells(i, 2).Font.Color = vbWhite
End If
Application.ScreenUpdating = False
End With
nextLine:
Next i
End With
Exit Sub
eHandler:
If err.Number = 1004 Then
Resume nextLine
ElseIf err.Number = 424 Then
Resume nextLine
ElseIf err.Number = 9 Then
Resume nextLine
End If
MsgBox err.Number & vbCrLf & err.Description
Application.StatusBar = vbNullString
Application.DisplayAlerts = True
End Sub
excel vba
I have a macro that runs through the filenames in col B and if it finds a text string that matches a file from a directory then it will export it as a pdf.
For some reason the the loop doesn't print the first file in the list. When i step thru then they all print.
Does anyone see anything in my code that could solve this problem?
Thanks
EDIT: problem was that enabling the PrintCommunication
was making the first sheet fail. PrintCommunication
is needed off tho so the PageSetup works as needed.
Code is updated below for anyone to use. All you would have to change is the MyFolder location or use With Application.FileDialog(msoFileDialogFolderPicker)
to pick the folder.
Sub ExcelPrint()
Application.DisplayAlerts = False
Dim OpenFile As Variant
Dim MyFile As String, MyFolder As String, AorB As String
Dim sourceFolder As String, destFolder As String
Dim LR As Long
MyFolder = ThisWorkbook.ActiveSheet.Range("B1").Value
LR = Cells(Rows.Count, "B").End(xlUp).Row
sourceFolder = Environ("Userprofile") & "DesktopPDF"
destFolder = sourceFolder & "_Temp"
If Len(Dir(sourceFolder, vbDirectory)) = 0 Then
MkDir sourceFolder
End If
If Len(Dir(destFolder, vbDirectory)) = 0 Then
MkDir destFolder
End If
On Error GoTo eHandler
With Worksheets("Print")
For i = 3 To LR
MyFile = ThisWorkbook.ActiveSheet.Cells(i, 2)
AorB = Mid(Cells(i, 2), 4, 1)
OpenFile = MyFolder & MyFile & ".xlsx"
Application.StatusBar = "Printing " & MyFile & ".xlsx"
Workbooks.Open _
FileName:=OpenFile, _
ReadOnly:=True
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.FitToPagesTall = 1
.FitToPagesWide = 1
If AorB = "A" Then
.PaperSize = xlPaperLetter
ElseIf AorB = "B" Then
.PaperSize = xlPaperTabloid
End If
End With
'This was the problem
'Application.PrintCommunication = True
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=destFolder & MyFile & ".pdf", _
Quality:=xlQualityStandard, _
IgnorePrintAreas:=False, _
From:=1, To:=1, _
OpenAfterPublish:=True
ActiveWorkbook.Close
Application.StatusBar = vbNullString
'Green Highlight if printed
With ThisWorkbook.Sheets("Print")
Application.ScreenUpdating = True
If Not Dir(destFolder & MyFile & ".pdf", vbDirectory) = vbNullString Then
Sheets("Print").Cells(i, 2).Interior.ColorIndex = 10
Sheets("Print").Cells(i, 2).Font.Color = vbWhite
End If
Application.ScreenUpdating = False
End With
nextLine:
Next i
End With
Exit Sub
eHandler:
If err.Number = 1004 Then
Resume nextLine
ElseIf err.Number = 424 Then
Resume nextLine
ElseIf err.Number = 9 Then
Resume nextLine
End If
MsgBox err.Number & vbCrLf & err.Description
Application.StatusBar = vbNullString
Application.DisplayAlerts = True
End Sub
excel vba
excel vba
edited Mar 7 at 20:21
Matt Taylor
asked Mar 7 at 17:15
Matt TaylorMatt Taylor
1571315
1571315
Is the first document name in B3 ? or is it in B2? You loop fromi = 3 to LR
Just verifying this isn't the issue.
– Emily Alden
Mar 7 at 17:20
my range of files are always after row 2. In this case the first excel file is in row 7. rows 3 - 6 are autocad files that the script is pulling the 1004 error which i tell it to resume nextline to continue
– Matt Taylor
Mar 7 at 17:33
When you run it does it not print any of the files or does it just skip the fist 'valid' file?
– Emily Alden
Mar 7 at 17:49
it skips the first valid file, prints the other perfectly then when it finished the first file is just sitting there in the background
– Matt Taylor
Mar 7 at 18:17
Have you tried changing which file is first to ensure it is a problem with that line and not the filename?
– Emily Alden
Mar 7 at 18:51
|
show 2 more comments
Is the first document name in B3 ? or is it in B2? You loop fromi = 3 to LR
Just verifying this isn't the issue.
– Emily Alden
Mar 7 at 17:20
my range of files are always after row 2. In this case the first excel file is in row 7. rows 3 - 6 are autocad files that the script is pulling the 1004 error which i tell it to resume nextline to continue
– Matt Taylor
Mar 7 at 17:33
When you run it does it not print any of the files or does it just skip the fist 'valid' file?
– Emily Alden
Mar 7 at 17:49
it skips the first valid file, prints the other perfectly then when it finished the first file is just sitting there in the background
– Matt Taylor
Mar 7 at 18:17
Have you tried changing which file is first to ensure it is a problem with that line and not the filename?
– Emily Alden
Mar 7 at 18:51
Is the first document name in B3 ? or is it in B2? You loop from
i = 3 to LR
Just verifying this isn't the issue.– Emily Alden
Mar 7 at 17:20
Is the first document name in B3 ? or is it in B2? You loop from
i = 3 to LR
Just verifying this isn't the issue.– Emily Alden
Mar 7 at 17:20
my range of files are always after row 2. In this case the first excel file is in row 7. rows 3 - 6 are autocad files that the script is pulling the 1004 error which i tell it to resume nextline to continue
– Matt Taylor
Mar 7 at 17:33
my range of files are always after row 2. In this case the first excel file is in row 7. rows 3 - 6 are autocad files that the script is pulling the 1004 error which i tell it to resume nextline to continue
– Matt Taylor
Mar 7 at 17:33
When you run it does it not print any of the files or does it just skip the fist 'valid' file?
– Emily Alden
Mar 7 at 17:49
When you run it does it not print any of the files or does it just skip the fist 'valid' file?
– Emily Alden
Mar 7 at 17:49
it skips the first valid file, prints the other perfectly then when it finished the first file is just sitting there in the background
– Matt Taylor
Mar 7 at 18:17
it skips the first valid file, prints the other perfectly then when it finished the first file is just sitting there in the background
– Matt Taylor
Mar 7 at 18:17
Have you tried changing which file is first to ensure it is a problem with that line and not the filename?
– Emily Alden
Mar 7 at 18:51
Have you tried changing which file is first to ensure it is a problem with that line and not the filename?
– Emily Alden
Mar 7 at 18:51
|
show 2 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049466%2fprinting-excel-files-that-match-cell-but-first-file-doesnt-print%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049466%2fprinting-excel-files-that-match-cell-but-first-file-doesnt-print%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is the first document name in B3 ? or is it in B2? You loop from
i = 3 to LR
Just verifying this isn't the issue.– Emily Alden
Mar 7 at 17:20
my range of files are always after row 2. In this case the first excel file is in row 7. rows 3 - 6 are autocad files that the script is pulling the 1004 error which i tell it to resume nextline to continue
– Matt Taylor
Mar 7 at 17:33
When you run it does it not print any of the files or does it just skip the fist 'valid' file?
– Emily Alden
Mar 7 at 17:49
it skips the first valid file, prints the other perfectly then when it finished the first file is just sitting there in the background
– Matt Taylor
Mar 7 at 18:17
Have you tried changing which file is first to ensure it is a problem with that line and not the filename?
– Emily Alden
Mar 7 at 18:51