Macro Printing error

OKCIrish

New Member
Joined
Dec 3, 2022
Messages
32
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I have a workbook that has a print report button on it, and when that button is pushed a macro runs and it consolidates the tabs into set print areas and titles and gives me a built PDF report with cover pages and graphs and the consolidated information that is in the tabs, it also saves that PDF on the desktop. However anytime this title page has a / on in the billing information area, it throws the macro and gives me this error,

ActiveWorkbook.ExportAsFixedFormat 0, Environ("USERPROFILE") & "\Desktop\" & _
Sheets("General").Range("C3").Value & " - " & Sheets("General").Range("C4").Value & " Drill Pipe Inspection Report " & " (" & Format(Date, "mm-dd-yyyy") & ")" & ".pdf", OpenAfterPublish:=True

Is there a way to fix this error or is there a way where I can make it to where the columns reject the input if a / is detected?

DRILCO DP Report V4.4.xlsb
ABCDEFGHIJKLMNOPQR
1Billing InformationInspection Performed (dropdown list)Specification Used (dropdown list)
2Date:January 2, 2022
3Customer:
4Rig:H/P 22
5Well/Location:
6Ordered By:
7Tube Specs
8OCSG:Size
9PO #:Grade
10Quote #:Weight
11New Wall
12Reports AvailableInclude Report? (dropdown list)Min Wall
13JOB#:OverviewYesTool Joint Specs
14Inspected at:Drill PipeYesNew OD
15ImagesYesMin OD
16Personnel InfoAppendixYesMax Pin ID
17Lead InspectorSummaryYesMin TS Box
18MonitorDS1 ReferenceYesMin TS Pin
19Inspector Helper(s)Recommend HB
20Range
21Connection
22Box Length Min
23Box Length Max
24Pin Length Min
25Pin Length Max
26
General
Cells with Data Validation
CellAllowCriteria
J13:J19List=Lists!$G$3:$G$4
N2:R5List=Lists!$H$1:$H$14
F2:L10List=Lists!$A$9:$A$19
O8List=Lists!$H$23:$H$30
O9List=Lists!$H$17:$H$20
O20List=Lists!$H$33:$H$34
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi OKCIrish,

what about if C3 is empty like in your sample?

VBA Code:
Dim strC3 As String
Dim strC4 As String

With Sheets("General")
  strC3 = Replace(.Range("C3").Value, "/", "_")
  strC4 = Replace(.Range("C4").Value, "/", "_")
End With
ActiveWorkbook.ExportAsFixedFormat 0, Environ("USERPROFILE") & "\Desktop\" & _
    strC3 & " - " & strC4 & " Drill Pipe Inspection Report " & " (" & Format(Date, "mm-dd-yyyy") & ")" & ".pdf", OpenAfterPublish:=True

Ciao,
Holger
 
Upvote 0
Solution
Hi OKCIrish,

what about if C3 is empty like in your sample?

VBA Code:
Dim strC3 As String
Dim strC4 As String

With Sheets("General")
  strC3 = Replace(.Range("C3").Value, "/", "_")
  strC4 = Replace(.Range("C4").Value, "/", "_")
End With
ActiveWorkbook.ExportAsFixedFormat 0, Environ("USERPROFILE") & "\Desktop\" & _
    strC3 & " - " & strC4 & " Drill Pipe Inspection Report " & " (" & Format(Date, "mm-dd-yyyy") & ")" & ".pdf", OpenAfterPublish:=True

Ciao,
Holger
That fixed the error. Thank you.
 
Upvote 0
I have encountered another issue. I got a new computer and had to re-download all of the applications and PDF readers. Now when I hit the print page macro, I get this error and it tells me this is the line I need to debug/fix.
MVB Printing error.PNG


VBA Code:
ActiveWorkbook.ExportAsFixedFormat 0, Environ("USERPROFILE") & "\Desktop\" & _
    strC3 & " - " & strC4 & " Drill Pipe Inspection Report " & " (" & Format(Date, "mm-dd-yyyy") & ")" & ".pdf", OpenAfterPublish:=True

Is the whole code for context.
VBA Code:
Dim strC3 As String
Dim strC4 As String

With Sheets("General")
  strC3 = Replace(.Range("C3").Value, "/", "_")
  strC4 = Replace(.Range("C4").Value, "/", "_")
End With

ActiveWorkbook.ExportAsFixedFormat 0, Environ("USERPROFILE") & "\Desktop\" & _
    strC3 & " - " & strC4 & " Drill Pipe Inspection Report " & " (" & Format(Date, "mm-dd-yyyy") & ")" & ".pdf", OpenAfterPublish:=True
 
Upvote 0
Hi,

please check the path. Do you use OneDrive? Using the code provided the path is

VBA Code:
C:\Users\holger\Desktop\

on my system.

When using

VBA Code:
Sub DesktopWSH()
  Dim objWSH As Object
  Dim strDesktopPath As String
  Set objWSH = CreateObject("wscript.Shell")
  strDesktopPath = objWSH.SpecialFolders.Item("Desktop")
  Debug.Print strDesktopPath & Application.PathSeparator
End Sub
it's

VBA Code:
C:\Users\holger\OneDrive\Desktop\

Ciao,
Holger
 
Upvote 0
Hi,

please check the path. Do you use OneDrive? Using the code provided the path is

VBA Code:
C:\Users\holger\Desktop\

on my system.

When using

VBA Code:
Sub DesktopWSH()
  Dim objWSH As Object
  Dim strDesktopPath As String
  Set objWSH = CreateObject("wscript.Shell")
  strDesktopPath = objWSH.SpecialFolders.Item("Desktop")
  Debug.Print strDesktopPath & Application.PathSeparator
End Sub
it's

VBA Code:
C:\Users\holger\OneDrive\Desktop\

Ciao,
Holger
Yes, my OneDrive just got synchronized to my new PC. So I need to modify the code in order for it to save properly? Will this effect other users that use this macro function who do not have OneDrive set up?
 
Upvote 0
Hi OKCIrish,

I would recommend to use an altered macro which should avoid to take any user to the IDE due to an error:

VBA Code:
Sub MrE_1228513_1701E14_Update()
' https://www.mrexcel.com/board/threads/macro-printing-error.1228513/
Dim strC3 As String
Dim strC4 As String
Dim strDTEnv As String
Dim strWSHEnv As String
Dim strPathToUse As String
Dim objWSH As Object

'checking for existence of folder
strDTEnv = Environ("USERPROFILE") & "\Desktop"
If Dir(strDTEnv, vbDirectory) = "" Then
  Set objWSH = CreateObject("wscript.Shell")
  strWSHEnv = objWSH.SpecialFolders.Item("Desktop") & Application.PathSeparator
  Set obhwsh = Nothing
  If Dir(strDTEnv, vbDirectory) = "" Then
    MsgBox "Could neither find " & strDTEnv & vbCrLf & _
        "nor " & strWSHEnv & ", please check!", vbInformation, "End here"
    Exit Sub
  End If
End If

'make up the path for the export
If Len(strDTEnv) > 0 Then
  strPathToUse = strDTEnv
Else
  strPathToUse = strDTEnv
End If
strPathToUse = strPathToUse & Application.PathSeparator

With Sheets("General")
  strC3 = Replace(.Range("C3").Value, "/", "_")
  strC4 = Replace(.Range("C4").Value, "/", "_")
End With

'in case of error proceed at the given line instead of breaking code
On Error GoTo err_here
ActiveWorkbook.ExportAsFixedFormat 0, strPathToUse & strC3 & " - " & strC4 & " Drill Pipe Inspection Report " & _
    " (" & Format(Date, "mm-dd-yyyy") & ")" & ".pdf", OpenAfterPublish:=True

Exit Sub

err_here:
  MsgBox "Error occurred in 'MrE_1228513_1701E14' creating PDF on " & Now & vbCrLf & _
      "Error number: " & Err.Number & vbCrLf & _
      "Error description: " & Err.Description, vbInformation, "Could not create PDF"

End Sub

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top