Hi I'm actually coding to delete all the userforms of a project and recreate them by using a XML file.
The problem is that when I delete a userform and I want to recreate it with the same name, I get the following error "Run-time error '75' - Path/File error".
I use this to delete userforms :
Application.VBE.VBProjects(iMyName).VBComponents.Remove VBComponent:=.Item(x)
Where iMyName is the project name and x the variable in my loop.
And just after I use:
Set ihm_f = .Add(vbext_ct_MSForm)
ihm_f.Properties("Name") = "CA"
Where ihm_f is my new userform and CA the name of the previous Userform I deleted for example.
Do someone know how to do ?
Try Saving the Workbook between Removing and re-Creating.
Works for me, but I would be interested to knows a better way.
Related
I am trying to assign a button to my macro but it just won't budge , i've saved my macro as a .dvb file (Project.dvb) the code is on "Module2" and my Subname is "FCI", so here is my string "^C^C_-vbarun;Project.dvb!Module2.FCI". But it keeps on returning as "Macro not found".
Did you not load the DVB? Type VBAMAN to verify the file is loaded. You may also want to use this instead:
^C^C-vbarun;FCI;
I have a Word .dot file which works in older versions of Word but fails with error 432 when run in Word 2013.
When I debug the code I have the line:
Load customerForm
And VBA shows the error:
Run-time error '432': File name or class name not found during Automation operation
The project "pennyscode" includes "Module1" which contains the function being debugged, "ThisDocument" and a form called "customerForm".
I have tried changing the name to "pennyscode.customerForm" but this doesn't make any difference.
This code is being called from a Sub function which is called from Document_New().
Updates
I can place a breakpoint on the Load customerForm line and demonstrate that it is the line that is causing the problem. If at this point I mouse over the word "customerForm" VBA comes up with
customerForm = <Object variable or With block variable not set>
If I delete/skip the Load line then the next line is customerForm.Show and that produces the same error.
If I just open the .dotm file and then use Alt-F11 to open VBA, I can look at the code for selectCustomer, list properties/methods and customerForm appears in the list.
Additional Note
I believe that within the Load function it must be calling GetObject and it is this that is failing. It is as if VBA can't find the customerForm object even though it appears in the project.
I've posted the full code of the function being called from Document_New below.
Sub selectCustomer()
Dim Doc As Document
Set Doc = Application.ActiveDocument
If Doc.CustomDocumentProperties.Item("Customer") = "Nothing" Then
Load customerForm
customerForm.Show
Unload customerForm
Doc.Fields.Update
a$ = Doc.CustomDocumentProperties.Item("InvoiceNumber")
a$ = customerForm.pathBox.Value + "\" + a$
Doc.SaveAs (a$)
End If
End Sub
I've also posted the full .dotm (Excel 2013) and .dot (previous excel) and some sample data (.xls) here:
Dropbox/Public/Invoice 2015-16.dotm
Dropbox/Public/Invoice 2015-16.dot
Dropbox/Public/data.xls
Update
I've not had much luck making progress on this question. Can anyone suggest an approach to investigating this? Or how I might improve the information on the question?
I finally managed to fix this, and I have a few learnings.
Firstly the debugger shows the error as occurring on the Load customerForm line, but this is actually not the case.
The customerForm has an _Initialize function which loads data into it before it is displayed. This function was failing with but the debugger stops on the wrong place.
I was able to debug this more effectively by putting a breakpoint on the start of the _Initialize sub and then stepping through the code.
Once I had discovered this I realized that the code was failing to find the XLSX file due to a wrong path, thus causing the run-time error.
Once I'd fixed up all the paths, I then hit a second error: runtime error '9' which is a subscript problem. This also reported on the Load customerForm line and was also due to a problem with the _Initialize function.
This was the true source of the problem, and demonstrated a functional change between Office 2013 and previous versions of Office.
My code was opening an XLSX file and attempting to read data from it:
Dim myXL As Object
Dim myWS As Object
Set myXL = GetObject("C:\Test\data.xlsx")
myXL.Application.Visible = True
myXL.Parent.Windows(1).Visible = True
Set myWS = myXL.Application.Worksheets("Customers")
The run-time error 9 was due to the index of the Windows property, as their were no windows. In previous versions of Office, there was a single window, with 2013 the array is empty.
After much messing about I tried adding this line:
myXL.Activate
before accessing the Windows() array. Once that was executed Windows(1) existed and the code worked as before.
Hope this can help someone else struggling with similar problems.
I have one of the stranges problems I have encountered for many years. I have a Workbook with a lot of code that validates the users data and has been used for many years now. A user reported that the latest version of the file was crashing excel and giving him an "automation error". To my knowledge the changes made to the most recent file were minor and should not have caused this. On analysis the cause of the problem was straight forward, but how it happened, why it happened and how to fix it, I do not know.
The issue occurred due to the below line of code, which is expecting a numeric, but the user supplied a string:
Ltrim(Str(Usersdata(UsersDataRow,UUID_Col)))
Note the upper case S on str. The previous version of the workbook has the same line but str is in all lower case, and does NOT crash excel.
Both files syntax check perfectly. But the strangest thing is when I have both old and new version of the file open in the VBA editor.
If I edit the line in the old file and change str to Str, the editor autocorrects it back to all lower case str.
If I edit the new workbook and change Str to str, the editor autocorrects it back to init cap Str.
So as it stands, I cannot correct the new file.
This behaviour is very strange and am hoping someone can tell me how it has happened and possibly how to solve it?
Regarding fixing the main problem I suggest replacing the line
Ltrim(Str(Usersdata(UsersDataRow,UUID_Col)))
with a more thorough input validation that can handle alphanumeric values
.
As Siddharth Rout suggested in the comments:
The symptoms you describe indicate that the VBA name space has been corrupted
The most common source are variable names like "str", "val", "name", "file", "count", "cell", "row" etc
A quick way to check name conflicts is to click inside the variable name and press F1;
the Help should show "Keyword Not Found"
.
The experiment bellow can demonstrate the problem: open a new Excel, and Alt + F11 for VBA
paste this code in a standard module:
Sub test1()
Dim txt As String 'valid variable name
txt = Str("123") 'Str() remains with a capital S
End Sub
.
Now replace the code with this (obvious problem):
Sub test2()
Dim str As String 'invalid variable name
str = str("123") 'Str() is converted to lower case "s"
End Sub
.
VBA is now corrupted and here is one way to fix it:
close the file
reopen it, and do not allow macros to run
open VBA editor (Alt + F11)
perform a Search and Replace in all VBA modules for "str" (replace "str" with "Str")
Match Case
Find Whole Word Only
Current Project <-- most important setting
The Replace operation will be performed only once, because VBA will automatically convert all other instances of "str" to "Str" before any other replacements
Name space is now restored the next time you open the file
(the procedure forces a recompilation of the P-code generated for all modules)
Another way to re-generate clean P-code is to export each individual standard module as *.bas files, *.cls for Class modules, and *.frm for user forms code, and import all into a new Excel file
I'm sure this is something simple, but I have looked all over and cannot find an explanation. Surely, plenty of other people would like to move modules and a userform to another workbook. I exported 3 modules and 1 userform and then imported them into a new workbook. The userform name that was exported was "NUI" and when I look at the properties in the new workbook after importing the name is still "NUI". However, now when I try to run the sub to show the userform, I get a run-time data mismatch error and NUI is highlighted in the line NUI.StartUpPosition. Any ideas why this no longer works? Is there some sort of link that needs to be broken? Does the module still try to find some userform NUI in other files? If I delete the userform in the new workbook and add a new userform, when I try to name it NUI, it tells me the name is in use. Any ideas?
Sub CallUserForm()
' Calls UserFrom NUI and centers it to excel window whether it is full screen or not.
PO.Unprotect Password:="1234"
PO.Activate
NUI.StartUpPosition = 0
NUI.Top = Application.Top + 125
NUI.Left = Application.Left + Application.Width / 2 - NUI.Width / 2
NUI.Show
PO.Protect Password:="1234"
End Sub
I just tried what you describe (export form and module, import into a new spreadsheet). It worked fine for me. However: I wonder if you may have accidentally imported the copy of the form into the original file (instead of the new one).
If I delete the form from the new file, I get the same error you did, so check that you actually did import to the new location.
I am using some code in my excel document to open images, rotate them as appropriate and then save them to a temp folder (see How do I rotate a saved image with VBA?).
I am now trying to rename these generated images within the same folder using Name... as..., however this throws up an error, saying that the image is still in use by Excel ("File/Path access error" in VBA - it tells me its in use by excel when renaming it in an explorer window though)
Is there any way to "throw this out" of Excel using VBA before this stage? I did try Set p = Nothing (with p coming from Set p = .Pictures.Insert(Filepath)) but this still gives an error. The sheet that the new image is created from is deleted after it is saved to the temp folder so it does not exist on the sheet any more either. Once I fully close excel, I can rename the file again.
The "solution" I ended up with was to copy folder C:\Temp\a\ to C:\Temp\b\ and make the changes from there before copying to \\my\actual\location
This works OK as a decent workaround for this instance, but by no means a 100% complete answer to the question
YOUR CODE HERE.....
CLOSE
'by add "close" at the ending of your line before conducting the next action