I am trying to put a graphic file into a UserForm Image in VBA. Using the code below
UserForm1.Image1.Picture = "d:\Missionary\Mexico\Daisy Marlene Olivares.bmp"
I get the error
"Compile Error: Type mismatch".
I know that the file path is correct.
Modifying the code to the following
UserForm1.Image1.Picture = LoadPicture("d:\Missionary\Mexico\Daisy Marlene Olivares.bmp")
I get the error
"Compile Error: Sub or Function not defined"
I suspect that part of the reason for this error is because the image control is not ActiveX.
Can anyone help?
You need to reference OLE Automation for the LoadPicture function.
Related
I get the following error:
Run-time error '-2147467259 (80004005)':
Method 'ExportAsFixedFormat2' of object '_Document' failed
I cannot figure out why the error occurs. See below for the entirety of my code.
Sub ExportAsPDF()
ActiveDocument.ExportAsFixedFormat2 OutputFileName:="test.pdf", ExportFormat:=wdExportFormatPDF, CreateBookmarks:=wdExportCreateHeadingBookmarks
End Sub
The file name is just a test. I've tried using the full path where I want to save it to: /Users/tom/Downloads/test.pdf (no such file already exists at this path).
I've got exactly the same problem and i've found the solution.
the path is too long, which makes the function crash.
Try to copy your file on a smaller path aand retry
I am writing some code in VBA for Word to redirect users to a link if they would like to or to close the window if not using a yes/no box. The problem is that if the user does not want to continue to the link, the code gives an error 424: Object required.
Set objShell = CreateObject("Wscript.Shell")
intMessage = MsgBox("Would you like to read about alternatives to these words?", vbYesNo, "Access Denied")
If intMessage = vbYes Then
objShell.Run ("LINK")
Else
Wscript.Quit
End If
The Error 424 highlights 'Wscript.Quit' when I debug the error.
How to I resolve this?
Thanks!
WScript is a VBScript object. You're writing VBA code. Remove the instruction altogether, WScript is undefined and means nothing.
You're getting that error because, in your code, WScript is a Variant/Empty, only because Option Explicit is missing from the top of your module, which allows VBA code to compile & run without validating that every variable is properly declared (this means typos happily compile & run as well) - in other words it's an undeclared variable, and since you're making a member call on it, per VBA syntax this could only be an object... except it isn't, hence "Object required".
Remote the instruction.
Add Option Explicit to the top of your module
Declare all variables. Look into how to use the Dim statement.
I have a VBA code that I used to use in an Excel 2016 (64-bit), and it works fine.
But when I tried to use in a Excel 2010 (32-bit) version, I've got an error "Runtime Error (1004) Application Defined or Object Defined Error" in the following line:
CndFrmtType = Sh.Cells.FormatConditions(CndFrmtItem).Type
I've already tried to re-build the file in a 2010 Excel (from the ground, to avoid reference's issues), but it still not working.
I really tried to google it, but didn't find anything about an eventual incompatibility in the FormatConditions object.
The returned FormatConditions object is as follows:
Can anyone help me on this?
be sure you have:
set Sh to a valid Worksheet object
initialized CndFrmtItem to a valid name or index number to reference a format condition of that worksheet
declared CndFrmtType as of Long type
and it'll work
As far as I can tell, there is nothing in VBA that tells you what line you are on or at what line an error has occurred. Is there some way to get the line number like there is in Visual Basic, ie
Dim CurrentStack As System.Diagnostics.StackTrace
lineNo = CurrentStack.GetFrame(0).GetFileLineNumber
How can one adapt this code for VBA?
erl will give you the error line number but only if you have added line numbers to your code, either manually or using an add-in like MZ Tools.
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.