Log what line error occurs: vba - vba

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.

Related

Formatting number to have 2 digits: "Object required"

Having a look in other threads on the issue, I've found how I can do it, but still i have some issue with my code (printing some values to a file. )
I'm trying to read some data from Excel sheet (it has 32 lines) and print it to a text file in some defined format. The code is for Excel Visual Basic
For y = 1 to 32
Print #1, y.ToString("D2")
Print #1, "some text" & y.ToString("D2")
Next y
I'm getting runtime error 424: object required on both of printing commands.
Am I missing something?
You seem to be mixing up Visual Basic with Visual Basic for Applications (VBA): they are quite different.
In VBA you can format a number with the Format function:
Format(y, "00")

VBA (Word) - can i see if application is already open

I've been having experiencing an issue with Word VBA where, if the Application is already open, it would just crash and doesn't do any further steps. I've been surfing the Net trying to figure this out, but no luck. The error appears at the o.Workbooks.Open line:
Dim o as Excel.Application
o.Workbooks.Open (ActiveDocument.Path + "\Data.xlsm")
Error message is
c:\fol\data.xlsm is already open or doesn't exist
It basically saves the data that was entered in Word text boxes. Note: All code afterwards works fine, this is the line where it breaks. I've tried using On Error that it would just stop, but it doesn't seem to work :/
Any ideas?

How do I fix Error 432 when loading a form in Word VBA?

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.

Runtime 424, Object Required on a Excel Macro, VBA

i'm getting the runtime 424 error when running this line of code:
dateOriginal = DateAdd(DateInterval.Day, dw, dateOriginal)
Does anyone has any idea of why is this error appearing?
Try the following
Substitute number for 1 as number of days
DateAdd("d",1, dateOriginal)

VB.NET Debug.Print not working, breakpoint disappears

I have a VB.NET application that I run in debug mode.
I have 3 lines
Dim sValue$
sValue = "test"
Debug.Print sValue
When I am not running, I set a breakpoint on the lines sValue = "test", and on the line Debug.Print sValue.
Now when I start debugging, the breakpoint on the line Debug.Print sValue disappears, and the Debug.Print is not performed.
However, the breakpoint on the line sValue = "test" stays there.
Does anybody know what might go wrong here?
Switching from x86 to AnyCPU helped.
Strange.
Community,
Here is another solution for those who are using Visual Studio 2012 (Express) for Desktop. Basically, you will need to import the system diagnostics and include a specific line of code. See below:
Imports System.Diagnostics
'Write Your Code Here
Trace.Listeners.Add(New TextWriterTraceListener(Console.Out))
Debug.Print(Today) 'This will print the date in "Short" form | Carriage Return
Debug.Write(Today) 'This will print the date in "Long" form
Trace.Write(Today) 'This will print the date in "Long" form
That's it. That very first line of code is necessary so that Visual Studio will recognize the Debug & Trace classes. The 2nd line of code is needed so that the window will actually display what you need. The final three lines of code do the exact same thing, essentially.
One more thing, make sure that you are in the Output Window's "Debug" page in order to see the printout!
One last thing, if you are a rookie [like me] please make sure that you hit F5 instead of CTRL+F5 if you want Visual Studio to display "Debug.Print" in your output window.
PS. There was a way to get the output to appear in the Immediate Window; but I changed my settings and now it won't appear. So, if you'd like it there you can tinker around with the options and you'll eventually get it.