+ Reply to Thread
Results 1 to 3 of 3

VBA Macro to Save as PDF - Getting Runtime Error 91

  1. #1
    Registered User
    Join Date
    10-07-2022
    Location
    Alberta, Canada
    MS-Off Ver
    Microsoft 365 Apps for Business
    Posts
    2

    VBA Macro to Save as PDF - Getting Runtime Error 91

    I have a spreadsheet for quotes. I created a macro to save the sheet as a new excel workbook & a PDF simultaneously.
    If I only have the file name save as the Quote# + Customer Name, I have no issues.
    But, if I want to add on the location name onto the filename it saves as, I am getting a Runtime 91 error.

    We do several quotes for the same companies throughout the year, so I want to add the location name to the filename, to easily identify them.



    Error message:

    Run-time error '91':
    Object variable or With block variable not set



    VBA Code (example workbook attached):

    Sub SaveInvAsPDF()

    Dim invyr As String
    Dim invno As String
    Dim contname As String
    Dim custname As String
    Dim prepby As String
    Dim dt_issue As Date
    Dim dt_exp As Date
    Dim path As String
    Dim fname As String
    Dim nextrec As Range
    Dim location As Range


    invyr = Range("M6")
    invno = Range("O6")
    contname = Range("E11")
    custname = Range("E12")
    dt_issue = Range("Q6")
    dt_exp = Range("Q8")
    prepby = Range("P4")
    location = Range("B17")
    path = "S:\OILFIELD\00 QUOTES\001 SMALL QUOTES\2022 Quotes"
    fname = invyr & invno & "_" & custname & location
    savetodirectory = "S:\OILFIELD\00 QUOTES\001 SMALL QUOTES\2022 Quotes"

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, ignoreprintareas:=False, Filename:=path & fname

    Set nextrec = Sheet1.Range("A1048576").End(xlUp).Offset(1, 0)

    nextrec = invyr
    nextrec.Offset(0, 1) = invno
    nextrec.Offset(0, 3) = contname
    nextrec.Offset(0, 4) = custname
    nextrec.Offset(0, 5) = prepby
    nextrec.Offset(0, 6) = dt_issue
    nextrec.Offset(0, 7) = dt_exp

    Sheet1.Hyperlinks.Add anchor:=nextrec.Offset(0, 9), Address:=path & fname & ".pdf"


    End Sub



    Sub SaveInvAsExcel()

    Dim invyr As String
    Dim invno As String
    Dim contname As String
    Dim custname As String
    Dim prepby As String
    Dim dt_issue As Date
    Dim dt_exp As Date
    Dim path As String
    Dim fname As String
    Dim nextrec As Range
    Dim location As Range



    invyr = Range("M6")
    invno = Range("O6")
    contname = Range("E11")
    custname = Range("E12")
    location = Range("B17")
    dt_issue = Range("Q6")
    dt_exp = Range("Q8")
    prepby = Range("P4")
    path = "S:\OILFIELD\00 QUOTES\001 SMALL QUOTES\2022 Quotes\Quote Spreadsheets"
    fname = invyr & invno & "_" & custname & location
    savetodirectory = "S:\OILFIELD\00 QUOTES\001 SMALL QUOTES\2022 Quotes"



    'then clear color fill from grey cells
    Range("E11,M11,B17:Q17,B20:S34,B37").Interior.Color = xlNone

    'copy quote sheet to a new workbook
    Sheet3.Copy

    'then delete all buttons on worksheet
    Dim shp As Shape
    For Each shp In ActiveSheet.Shapes
    If shp.Type <> msoPicture Then shp.Delete
    Next shp

    'then clear color fill from grey cells
    Range("E11,M11,B17:Q17,B20:S34,B37").Interior.Color = xlNone


    'save new workbook to specified folder
    With ActiveWorkbook
    .Sheets(1).Name = "Quote"
    .SaveAs Filename:=path & fname, FileFormat:=51
    .Close
    End With
    'close workbook

    'save quote as PDF to specified folder
    With ActiveWorkSheet
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, ignoreprintareas:=False, Filename:=savetodirectory & fname
    End With


    'need to put quote details in the record of quotes sheet

    Set nextrec = Sheet1.Range("A1048576").End(xlUp).Offset(1, 0)

    nextrec = invyr
    nextrec.Offset(0, 1) = invno
    nextrec.Offset(0, 3) = contname
    nextrec.Offset(0, 4) = custname
    nextrec.Offset(0, 5) = prepby
    nextrec.Offset(0, 6) = dt_issue
    nextrec.Offset(0, 7) = dt_exp

    'add hyperlink to Excel & PDF files in the record of quotes sheet

    With Worksheets(1)
    Sheet1.Hyperlinks.Add anchor:=nextrec.Offset(0, 10), Address:=path & fname & ".xlsx", TextToDisplay:="Excel Copy"
    Sheet1.Hyperlinks.Add anchor:=nextrec.Offset(0, 9), Address:=savetodirectory & fname & ".pdf", TextToDisplay:="PDF Copy"
    End With



    End Sub



    Sub CreateNewInvoice()

    Dim invno As Long

    invno = Range("O6")

    Range("E11,P4,M11,B17,G17,Q17,B20:S34,B37").Value = ""

    MsgBox "Your next quote number is " & invno + 1

    Range("O6") = invno + 1

    Range("E11").Select

    Range("P4,E11,M11,B17,G17,Q17,B20:S34,B37").Interior.Color = RGB(242, 242, 242)

    ThisWorkbook.Save






    End Sub


    Sub RecordofInvoice()

    Dim invyr As String
    Dim invno As String
    Dim contname As String
    Dim custname As String
    Dim prepby As String
    Dim dt_issue As Date
    Dim dt_exp As Date
    Dim nextrec As Range

    invyr = Range("M6")
    invno = Range("O6")
    contname = Range("E11")
    custname = Range("E12")
    dt_issue = Range("Q6")
    dt_exp = Range("Q8")
    prepby = Range("P4")


    Set nextrec = Sheet1.Range("A1048576").End(xlUp).Offset(1, 0)

    nextrec = invyr
    nextrec.Offset(0, 1) = invno
    nextrec.Offset(0, 3) = contname
    nextrec.Offset(0, 4) = custname
    nextrec.Offset(0, 5) = prepby
    nextrec.Offset(0, 6) = dt_issue
    nextrec.Offset(0, 7) = dt_exp


    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,644

    Re: VBA Macro to Save as PDF - Getting Runtime Error 91

    Change declaration of location
    Please Login or Register  to view this content.
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    10-07-2022
    Location
    Alberta, Canada
    MS-Off Ver
    Microsoft 365 Apps for Business
    Posts
    2

    Re: VBA Macro to Save as PDF - Getting Runtime Error 91

    Oh my gosh - I thought about doing that! I knew it would be a simple error.

    Thanks so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. ActiveWorkbook.Save Runtime Error 1004 Help!
    By 7189456 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-13-2022, 12:50 PM
  2. No/Cancel save = Runtime Error 1004
    By sgad in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-17-2019, 03:43 AM
  3. Runtime error 1004 when trying to save file via VBA
    By avh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-09-2015, 03:20 AM
  4. [SOLVED] Save as PDF, choosing path, runtime error 1004
    By mikkelsl in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2014, 12:27 PM
  5. [SOLVED] runtime error 1004 method save of object _workbook failed
    By madhatter40 in forum Excel General
    Replies: 1
    Last Post: 06-22-2013, 07:30 AM
  6. [SOLVED] runtime error 1004 method save of object _workbook failed
    By madhatter40 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-20-2013, 03:37 AM
  7. [SOLVED] Runtime error 1004/ Save As method help
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-17-2006, 12:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1