VBA project not working properly in excel 2007
Hi,
I have a vba project which works perfectly fine on my computer on excel 2010 and excel 2013. I made a project using excel 2010. Now I want to use in on excel 2007, so I am facing the following problems:-
1. I get an error "Error in hidden module." when I run any macor. This is overcome if I open the project in the vba editor and put my password and also go in tools>references and just open and close the dialogue, but why so?
2. I have function to create the pdf and another where the first is used, I am posting both functions.
The First Function:-
Function CreatePDFNamedRange(NamedRange As String, FixedFilePathName As String, _
OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
'This function will create a PDF with every sheet with
'a sheet level name variable
Dim FileFormatstr As String
Dim Fname As Variant
Dim Ash As Worksheet
Dim sh As Worksheet
Dim ShArr() As String
Dim s As Long
Dim SheetLevelName As Name
'Test If the Microsoft Add-in is installed
'We fill the Array with sheets with the sheet level name variable
For Each sh In ActiveWorkbook.Worksheets
If sh.Visible = -1 Then
Set SheetLevelName = Nothing
On Error Resume Next
Set SheetLevelName = sh.Names(NamedRange)
On Error GoTo 0
If Not SheetLevelName Is Nothing Then
s = s + 1
ReDim Preserve ShArr(1 To s)
ShArr(s) = sh.Name
End If
End If
Next sh
'We exit the function If there are no sheets with
'a sheet level name variable named
If s = 0 Then Exit Function
If FixedFilePathName = "" Then
'Open the GetSaveAsFilename dialog to enter a file name for the pdf
FileFormatstr = "PDF Files (*.pdf), *.pdf"
Fname = Application.GetSaveAsFilename("", filefilter:=FileFormatstr, _
Title:="Create PDF")
'If you cancel this dialog Exit the function
If Fname = False Then Exit Function
Else
Fname = FixedFilePathName
End If
'If OverwriteIfFileExist = False we test if the PDF
'already exist in the folder and Exit the function if that is True
If OverwriteIfFileExist = False Then
If Dir(Fname) <> "" Then Exit Function
End If
Application.ScreenUpdating = False
Application.EnableEvents = False
'Remember the ActiveSheet
Set Ash = ActiveSheet
'Select the sheets with the sheet level name in it
Sheets(ShArr).Select
'Now the file name is correct we Publish to PDF
On Error Resume Next
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=Fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=OpenPDFAfterPublish
On Error GoTo 0
'If Publish is Ok the function will return the file name
If Dir(Fname) <> "" Then
CreatePDFNamedRange = Fname
End If
Ash.Select
Application.ScreenUpdating = True
Application.EnableEvents = True
End Function
The Second function using the first:-
Function WorksheetsToPDFmail() As Boolean
Dim strFileName As String
Dim strmailbody As String
'Set the function to false
WorksheetsToPDFmail = False
If ActiveWindow.SelectedSheets.Count > 1 Then
MsgBox "There is more then one sheet selected," & vbNewLine & _
"be aware that every selected sheet will be published"
End If
HideRange
'Call the function with the correct arguments
'Tip: You can also use Sheets("Sheet3") instead of ActiveSheet in the code(sheet not have to be active then)
strFileName = CreatePDFNamedRange("Estimate", "D:\Estimate.pdf", True, False)
'For a fixed file name and overwrite it each time you run the macro use
'RDB_Create_PDF(ActiveSheet, "C:\Users\Ron\Test\YourPdfFile.pdf", True, True)
If strFileName <> "" Then
'Set the function to true
WorksheetsToPDFmail = True
strmailbody = "Dear Sir/Madam, " & vbCrLf & vbCrLf
strmailbody = strmailbody & "Please find the attached estimate as per your requirement, please do let us know if it can be of any further assistance to you." & vbCrLf & vbCrLf
strmailbody = strmailbody & "For any further assistance please feel free to contact the under signed."
EmailPDF strFileName, Range("F11"), "Estimate for " & Range("B7") & " dated " & Range("N2"), strmailbody, False
Else
MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
"Microsoft Add-in is not installed" & vbNewLine & _
"You Canceled the GetSaveAsFilename dialog" & vbNewLine & _
"The path to Save the file in arg 2 is not correct" & vbNewLine & _
"You didn't want to overwrite the existing PDF if it exist"
End If
UnhideRange
'Check whetehr the pdf was created and if so delete the file.
If Not strFileName = "" Then
Kill strFileName
End If
End Function
In Excel 2010 and 2013 the pdf is successfully created, but unfortunately in excel 2007 the pdf does not get created.
3. Also in excel 2007,i am prompted to enable macros each time, is there a way to enable all macros always in the workbook, this I know how to do in excel 2010 but how to do it in excel 2007.
Any help would be extremely appreciated as I am stuck.
Mikdad.
Recent comments
5 years 41 weeks ago
6 years 27 weeks ago
6 years 39 weeks ago
6 years 42 weeks ago
6 years 43 weeks ago
6 years 48 weeks ago
7 years 4 weeks ago
7 years 5 weeks ago
7 years 5 weeks ago
7 years 5 weeks ago