I'm facing a problem that I really don't understand.
I created a macro that opens a workbooks, reads some information in it, and closes it.
To open the workbook, I use these lines:
Path = Func_Use_File_DialogOpen()
If Path <> "" Then
Set wb_chosen = Application.Workbooks.Open(Path, UpdateLinks:=0)
ElseIf Path = "" Then
MsgBox ("Error, start again")
Exit Sub
End If
For your information, the variable Path contains the whole path of the file, with the extension.
Example: Path= C:\Users\alex\Desktop\file.xlsm
On my computer, the macro works perfectly, it opens the worbook, and I can use it with my variable called wb_chosen
But when I send my macro to a coworker, he gets the error
"RUNTIME ERROR 91: Object variable or With Block variable not set"
How is that possible? when I try to debug the macro on his computer, I see that the variable path is perfectly fine (like on my computer), but the variable wb_chosen is nothing
so the problem is on the line :
Set wb_chosen = Application.Workbooks.Open(Path, UpdateLinks:=0)
Could it be the Application.Workbooks that doesn't work?
I really need this macro to work on every computer, is there a parameter to change on Excel? should I just put this instead?
Workbooks.Open(Path)
FYI, here are some other details about the code
The workbook is declared like this
Public wb_chosen As Workbook
the function Func_Use_File_DialogOpen() is as follow:
Public Function Func_Use_File_DialogOpen() As String
On Error GoTo lblErr
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Show
Func_Use_File_DialogOpen = .SelectedItems(1)
End With
Exit Function
lblErr:
MsgBox Err.Description, vbCritical, "Error"
Func_Use_File_DialogOpen = vbNullString
End Function
The variable Path is a string that store the full path of the file. When I run the code on my computer, i have
Path=\\cm\TA\PNL\10 - octobre\pnl_Octobre.xlsm
I have a workbook that is password protected and I’d like to create a read only copy that other users can view on a different location on the network drive.
I know it’s a strange request as the other people could open the original as read only, but we don’t want them to know the location of the original or have anything to do with it, should they figure out my colleagues password.
The other issue we had was that people were opening as read only and it was still telling my colleague that it was locked by another user and he needs it for most of the day so that issue is annoying
Thanks in advance
What you could do is add the following event procedure to the ThisWorkbook module:
Const RemotePath As String = "D:\YourRemoteLocation\"
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
On Error GoTo CleanUp
If Success And InStr(ThisWorkbook.Name, "Copy of ") = 0 Then
Dim CopyFullName As String
CopyFullName = RemotePath & "Copy of " & ThisWorkbook.Name
Application.EnableEvents = False
Dim fso As FileSystemObject 'Requires the Microsoft Scripting Runtime Library
Set fso = New FileSystemObject
fso.CopyFile Source:=ThisWorkbook.FullName, Destination:=CopyFullName
Dim ReadOnlyWorkbook As Workbook
Set ReadOnlyWorkbook = Workbooks.Open(Filename:=CopyFullName)
Application.DisplayAlerts = False
ReadOnlyWorkbook.SaveAs Filename:=CopyFullName, Password:=""
Application.DisplayAlerts = True
ReadOnlyWorkbook.Close SaveChanges:=False
End If
CleanUp:
Application.EnableEvents = True
Application.DisplayAlerts = True
Set fso = Nothing
End Sub
This code will run every time the workbook is saved and export the current file to the remote location. Then it will open the copy and save it as a workbook without password.
Note that I've added InStr(ThisWorkbook.Name, "Copy of ") = 0 as a condition to the If-statement. Instr returns the position where a substring (arg2) appears in the main string (arg1) or zero if the substring is absent from the main string. In this context, we want it to be zero since we don't want to run the code in the workbook copy.
In this method, the owner of the original file will have to supply their password every time they save. You could automate this by passing the password as an argument to the Open method like this:
Set ReadOnlyWorkbook = Workbooks.Open( _
Filename:=CopyFullName, _
Password:="MyPassword")
However, the password would then be accessible by people looking into the VBA code.
Alternatively you could get the password from a local file that wouldn't be accessible from the Network, but then the file path would be visible.
And if the remote folder is not already set to be Read-only mode, you can make sure that people opening the remote version of the file do so in Read-Only mode by adding the following event procedure after the previous one.
Private Sub Workbook_Open()
If InStr(ThisWorkbook.Name, "Copy of ") = 0 Then
ThisWorkbook.ChangeFileAccess Mode:=xlReadOnly
End If
End Sub
Obviously this will only work if they enable macros.
we have an Importer for Jira that imports projects and configures them. I did not write the code myself and there is absolutely no technical documentation regarding it.
A few days ago its started showing an error message
I checked the directory and the file permissions and everything but it is still not working. The other thing is that it is not working solely for one person. Does anyone have any ideas?
Public Sub getValuesFromFile()
' if the property excelDefaultValues has already values in it break this sub
If excelDefaultValues.Count > 0 Then
Exit Sub
End If
' Definitions
Dim ExcelFolder As String
Dim selectedfile As String
Dim excel As Application
Dim workbook As Workbook
Dim sheet As Worksheet
Dim column As Integer = 0
Dim row As Integer = 0
' Define the location and name of the default values file
ExcelFolder = "R:\PM\01_Team\01_Allgemein\07_Jira_Importer\Masterfile"
selectedfile = ExcelFolder & "\" & "DefaultValues.xlsx"
' try to open excel and the file
Try
excel = New Application
excel.DisplayAlerts = False
excel.Workbooks.Open(selectedfile)
workbook = excel.ActiveWorkbook
sheet = workbook.Worksheets(1)
' fails if the file does not exist, open a message box.
Catch
MsgBox("file template not found! please make sure that the file is in this directory:" & vbCrLf & selectedfile, MsgBoxStyle.OkOnly, "warning!")
Exit Sub
End Try
I am new to VBA and Macros, this is my first attempt to code. So... I have a word document that functions as a template for report generation. The word document has tables/charts linked to an excel document. I am trying to create a macro that will take the existing source linked in the word document and change/Update them to the new desired location. The idea is to have the user run the macro and then be prompted to choose an excel file and the macro will automatically update all of the tables and charts.
I am receiving a
" Run-time error '91': Object variable or With block variable not set"
message for the following line:
ActiveDocument.Fields(x).LinkFormat.SourceFullName = newFile
After doing some research, I believe the problem is that I am assigning an object variable to a value. Which instead needs to be "set" due to it being an object? Therefore, I have tried changing the line to the following
Set ActiveDocument.Fields(x).LinkFormat.SourceFullName = newFile
which in return gets a
"Compile error: Invalid use of property"
message on the .SourceFullName part of the code.
As previously mentioned, I am a complete newbie and am effectively lost at this point. Any advice and pointers will be welcomed. I have palns in the future to automate other aspects of the report but this is the main time-consumer of the report process and is the reason as to why I am starting with this.
Thanks Guys!
Sub changeSource()
Dim dlgSelectFile As FileDialog 'FileDialog object '
Dim thisField As Field
Dim selectedFile As Variant
Dim newFile As Variant
Dim fieldCount As Integer '
Dim x As Integer
Set dlgSelectFile = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
With dlgSelectFile
.Filters.Clear 'clear filters
.Filters.Add "Microsoft Excel Files", "*.xls, *.xlsb, *.xlsm, *.xlsx" 'filter for only Excel files
'use Show method to display File Picker dialog box and return user's action
If .Show = -1 Then
For Each selectedFile In .SelectedItems
newFile = selectedFile 'gets new filepath
Next selectedFile
Else 'user clicked cancel
Exit Sub
End If
End With
Set dlgSelectFile = Nothing
'update fields
fieldCount = ActiveDocument.Fields.Count
For x = 1 To fieldCount
ActiveDocument.Fields(x).LinkFormat.SourceFullName = newFile
Next x
MsgBox "Source data has been successfully imported."
End Sub
I understand similar questions with these errors have been posted before, but I found nothing when it came to formatting tables so don't close this please. In my VBA code in MS Access 2013 it exports data from MS Access to Excel. 6 different queries get exported into 1 excel file, each on a different worksheet. This works fine. I then format each sheet to have all the data in a table. I have a form which lets the user choose the path to save the file. If it is the first time creating the file, it works properly. If it is the second time creating the file in that same directory, it doesn't work and it gives me the error:
Run-time error 1004: Method Range of object _Global failed
I figured this was because I was overwriting my file instead of deleting it and recreating it. So I added in some code to check if the file exists, and if it does, delete it. I added breakpoints and while running through this part of the code, I was watching my documents folder. The file successfully got deleted and then recreated which is what I wanted. It still gave me that error. I manually went to delete the file and then reran my code again. It worked properly.
How come I need to manually delete this file in order to rerun my code? Or is it something else that is causing the problem? Here is the important parts of my code as the whole thing is too long to post:
'Checks if a file exists, then checks if it is open
Private Sub checkFile(path As String)
Dim openCheck As Boolean
'If file exists, make sure it isn't open. If it doesn't, create it
If Dir(path) <> "" Then
openCheck = IsFileLocked(path)
If openCheck = True Then
MsgBox "Please close the file in " & path & " first and try again."
End
Else
deleteFile (path)
End If
Else
End If
End Sub
Sub deleteFile(ByVal FileToDelete As String)
SetAttr FileToDelete, vbNormal
Kill FileToDelete
End Sub
Private Sub dumpButton_Click()
On Error GoTo PROC_ERR
Dim path As String
Dim testBool As Boolean
path = pathLabel4.Caption
path = path & Format(Date, "yyyy-mm-dd") & ".xlsx"
checkFile (path)
dumpQueries (path)
formatFile (path)
'Error Handling
PROC_ERR:
If Err.Number = 2001 Then
MsgBox "A file may have been sent to " & path
Exit Sub
ElseIf Err.Number = 2501 Then
MsgBox "A file may have been sent to " & path
Exit Sub
ElseIf Err.Number = 3021 Then
MsgBox "A file may have been sent to " & path
Exit Sub
ElseIf Err.Number = 2302 Then
MsgBox "A file may have been sent to " & path
Exit Sub
ElseIf Err.Number = 0 Then
MsgBox "Your file has been stored in " & pathLabel4.Caption
Exit Sub
Else
MsgBox Err.Number & ": " & Err.Description & vbCrLf & vbCrLf & "New Error. Please contact the IT department."
End If
Private Sub dumpQueries(path As String)
Dim obj As AccessObject, dB As Object
Set dB = Application.CurrentData
For Each obj In dB.AllQueries
testBool = InStr(obj.name, "Sys")
If testBool <> True Then
If obj.name = "example1" Or obj.name = "example2" Then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, obj.name, path, True, editWorksheetName(obj.name)
End If
End If
Next obj
End Sub
'Autofits the cells in every worksheet
Private Sub formatFile(path As String)
Dim Date1 As Date, strReportAddress As String
Dim objActiveWkb As Object, appExcel As Object
Set appExcel = CreateObject("Excel.Application")
appExcel.Visible = False
appExcel.Application.Workbooks.Open (path)
Set objActiveWkb = appExcel.Application.ActiveWorkbook
With objActiveWkb
Dim i As Integer
For i = 1 To .Worksheets.count
.Worksheets(i).Select
.Worksheets(i).Cells.EntireColumn.AutoFit
.Worksheets(i).ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).name = "myTable1"
Next
End With
appExcel.ActiveWindow.TabRatio = 0.7
objActiveWkb.Close savechanges:=True
appExcel.Application.Quit
Set objActiveWkb = Nothing: Set appExcel = Nothing
End Sub
The error occurs near the bottom of the code. It's the line:
.Worksheets(i).ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).name = "myTable1"
There may be a couple functions I left out but they work fine and shouldn't be needed for answering the question.
This is the only relevant code:
Set objActiveWkb = appExcel.Application.ActiveWorkbook
With objActiveWkb
Dim i As Integer
For i = 1 To .Worksheets.count
.Worksheets(i).Select
.Worksheets(i).Cells.EntireColumn.AutoFit
.Worksheets(i).ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).name = "myTable1"
Next
End With
Things get easier to follow when you trim the fluff away and start naming things - there's no need to .Select anything, appExcel is already an Application object, and there's no need to make a copy reference to the active workbook just to use in a With block, especially if that copy is going to be an Object variable anyway - if the copy were a Workbook object then you would at least get IntelliSense for its members...
Your source range is ambiguous. Range("A1") in Excel-VBA is an implicit reference to the active worksheet.. but this is Access-VBA, so there's no such thing, xlSrcRange is an enum value defined in the Excel object model, so if you don't have a reference to the Excel object model (you're late-binding this, right?), and Option Explicit isn't specified, then xlSrcRange is treated by VBA like just another undeclared/uninitialized variable, and therefore you're passing a 0 there, and the xlSrcRange enum value stands for a 1 - and 0 happens to be the underlying value for xlSrcExternal. Same with xlYes.
Since we cannot possibly guess what the actual source range is supposed to be from the code you posted, I'm leaving you with this:
Dim target As Object
Dim srcRange As Object
Set srcRange = TODO
With appExcel.ActiveWorkbook
Dim i As Integer
For i = 1 To .Worksheets.Count
.Worksheets(i).Cells.EntireColumn.AutoFit
Set target = .Worksheets(i).ListObjects.Add(1, srcRange, , 1)
If target Is Not Nothing Then target.Name = "myTable1"
Next
End With
Side question... why name the table myTable1 when Excel will already have named it Table1 anyway? Also note, if .Add fails, your code blows up with a runtime error 91 because you'd be calling .Add off Nothing. Verifying that the target is not Nothing before setting its Name will avoid that.
To answer your question in the comments:
#Mat'sMug is this what you were talking about? because it gives me this error: "438: Object doesn't support this property or method" Here's the code: .Worksheets(i).ListObjects.Add(SourceType:=xlSrcRange, Source:=.Cells(1).CurrentRegion, _ XlListObjectHasHeaders:=xlYes, TableStylename:="TableStyleMedium1").name = "Table"
The reason this throws a 438 is because your With block variable is a Workbook object, and a Workbook object doesn't have a .Range member.
What I was talking about, is that in Excel VBA unqualified calls to Range, Row, Column, and Cells are implicitly referencing the ActiveSheet, and unqualified calls to Worksheets, Sheets and Names are implicitly referencing the ActiveWorkbook - that's a recurrent problem in a lot of VBA code and a very common mistake to make. The solution is basically to say what you mean, and mean what you say; in this case the failure is on "mean what you say" - the unqualified Range("A1") call is, according to the error message, calling [_Globals].Range("A1")... which is weird because it implies that you're referencing the Excel object model library, which means your late-binding and Object variables could just as well be early-bound: why deal with Object variables and lack of IntelliSense when you're already referencing the library you're late-binding to?