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
Related
I am trying to check a various number of documents/paths if they are opened with the Open Statement in Excel. I need this to check if I can save a document or not.
The problem is that I receive a runtime error 70: Access denied. The error message only occure within the second Open Statement, the first one always works correctly. If only one of the documents is opened, it works as well. Therefore, I think that the first Open Statement is not closed properly.
However, I do not get how to solve the problem. I already tried various ways like Application.Wait, diffrendt FreeFiles, ... I also had a look in diffrent posts but as I am using the Open Statement for the first time I still do not get the right way.
Does maybe somebody has an idea what the problem could be?
There you have the code in Excel (I simplified the code to the core problem)
Sub Example()
Path1:
On Error GoTo errhandler
Open Path1 For Binary Access Read Lock Read As #1 --> Works fine
Close #1
Path2:
On Error GoTo errhandler_2
Open Path2 For Binary Access Read Lock Read As #2 --> Runtime error
Close #2
On Error GoTo 0
errhandler:
Error_Path1 = True
Close #1
GoTo Path2
errhandler_2:
Error_Path1 = True
Close #2
End Sub
I am trying to execute bin file using vba, code is working properly in one laptop, but in other laptop code is showing run time error 53 - file not found. Any suggestion please.
Sub SevenZip()
ChDir ("C:\Users\Desktop\LOGS\Test")
Zipping = Shell("7zipCTest.bat", vbNormalFocus)
End Sub
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.
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 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.