Command Button/Form button Fails To Invoke Macro but Step execution works

Black Mamba

New Member
Joined
Feb 2, 2017
Messages
5
Dear all,

I have written some code to save an excel sheet as a pdf (WIN 10 using MVB for Applications 7.1, Excel 2013). Stepping into the code executes perfectly but when I assign the code to a command button or form button the code fails. It seems to fail at the section starting at ActiveSheet.ExportAsFixedFormat. Can someone please tell me why the code fails with buttons.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Sorry I thought I had attached the code. Here it is:

Code:
Sub SaveAsPDF()
    
    Dim strBasePath As Variant, PdfFileName As Variant, strPtName As Range, strPtID As Range, strPlanName As Range, strMach As String
    
    Set strPtName = Worksheets("Dose Measurement").Range("d8")
    Set strPtID = Worksheets("Dose Measurement").Range("d10")
    Set strPlanName = Worksheets("Dose Measurement").Range("k8")
    strMach = Worksheets("Dose Measurement").Range("k10")
    PdfFileName = strPtName & "  " & strPtID
    
    If IsEmpty(strPtName) Or IsEmpty(strPtID) Or IsEmpty(strPlanName) Or strMach = "Select here" Or strMach = "" Then
        MsgBox "Please enter all Patient Demographics" & vbCrLf & vbCrLf & "Click Ok to continue", Buttons:=vbInformation, Title:="FOR YOUR INFORMATION"
        strPtName.Select
        Exit Sub
    End If
    
    With ActiveDocument
        On Error GoTo Errhandler
        StrPath = GetFolder & ""
        Application.PrintCommunication = False  'this temporarily turns off the communication to the printer to avoid Run-time error
'‘1004’: Unable to set the FitToPagesTall property of the PageSetup class
        If PdfFileName <> False Then
            With ActiveSheet.PageSetup
                .Orientation = x1Portrait
                .PrintArea = "$b$2:$n$91"
                .FitToPagesWide = 1
                .FitToPagesTall = 0
            End With
            
            Application.PrintCommunication = True
            
            ActiveSheet.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=StrPath & PdfFileName & " " & "Point Dose", _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
            MsgBox "PDF file: " & vbCrLf & vbCrLf & PdfFileName & " " & "Point Dose" & (".pdf") & vbCrLf & vbCrLf & "has been created.", Buttons:=vbInformation, Title:="FOR YOUR INFORMATION"
            
        End If
exitHandler:
        Exit Sub
Errhandler:
        MsgBox "Could not create PDF file"
        Resume exitHandler
    End With
End Sub
Function GetFolder() As String
    Dim oFolder As Object, strBasePath As Variant
    strBasePath = "[URL="file://\\hneahs.nswhealth.net\shares\Tamworth"]\\hneahs.nswhealth.net\shares\Tamworth[/URL] NWCC\Medical Physics\Treatment Planning\01 Plan Checking\02 Dynamic Treatments\01 Treated Patients"
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0, strBasePath)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
End Function
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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