Check if a property of an object has value or not - vba

I couldn't find a solution for this on SO or other source. I apologize in advance if this is a tired question:
I have a macro that iterates through placeholders in shapes. Some of these placeholders have text, some don't. I'm trying to figure out if
.Shapes.Placeholder.TextFrame.TextRange has any value or not (In the 'watches' section of the VBA editor, this property has <The specified value is out of range.>). If it does, do something. Otherwise, continue the For Each statement. However, everything I tried has thrown out of bounds errors or something of that type.
I have tried:
If Not (placeholder.TextFrame.TextRange.Length = 0) Then: Immediately throws out of bounds error
Try/catch, which doesn't seem to exist in VBA (Gives me Sub or Function not registered)
If Not CStr(placeholder.TextFrame.TextRange) = "0" then
I'm quite out of my depth as I don't have experience with any Visual Basic iteration. I just need to, if that property is empty or null, skip over the placeholder.
EDIT: Following suggestions, I tried this:
For Each placeholder In sld.NotesPage.Shapes.Placeholders
If Not (placeholder.TextFrame Is Nothing) Then
If Not (placeholder.TextFrame.TextRange Is Nothing) Then
If Not (placeholder.TextFrame.TextRange.LanguageID Is Nothing) Then
placeholder.TextFrame.TextRange.LanguageID = lng
End If
End If
End If
Next
I still get an out of bounds error on placeholder.TextFrame.TextRange Is Nothing. Did I make a syntax error?
FINAL EDIT: Turns out there was an answer in SO after all. For this specific problem, it was enough to add On Error Resume Next right after theFor Each.

Related

Error HRESULT E_FAIL when iterating through for loop only

I'm seeing the good old "System.Runtime.InteropServices.COMException HResult=0x80004005 Message=Error HRESULT E_FAIL has been returned from a call to a COM component" error when attempting to find an item via a for loop as shown below:
For i = 1 to itemList.Count
oObject = itemList.Item(i)
Next
But not if I hardcode the index, this finds item 1 without issue:
oObject = itemList.Item(1)
Obviously I don't want to do that and need to search through all the objects in my "itemList" to find the one I'm looking for.
I'm being intentionally vague because the software I'm working in is Dassault 3D Experience but am writing macros for it through Visual Studio 2017. I'm not sure where to even start debugging this sort of issue so any suggestions would be appreciated. Thanks.
Edit: adding full code of what I'm trying to do here (find an object, display its name, also select it on screen to double check. I will later add a check to make sure the object found in each loop is really what I'm looking for). All variables have been declared before this section.
selactive = CATIA.ActiveEditor.Selection
selactive.Clear()
product1Service = CATIA.ActiveEditor.GetService("PLMProductService")
oRootOcc = product1Service.RootOccurrence
cVPMOccurrences = oRootOcc.Occurrences
For i = 1 to cVPMOccurrences.Count
oVPMOccurrence = cVPMOccurrences.Item(i)
selactive.Add(oVPMOccurrence)
MsgBox(oVPMOccurrence.Name)
Next
The line that fails is oVPMOccurrence = cVPMOccurrences.Item(i)
Can you do something like this with a For Each loop?
For each oVPMOccurrence as oRootOcc.Occurrence in cVPMOccurrences.Items
selactive.Add(oVPMOccurrence)
MsgBox(oVPMOccurrence.Name)
Next
Using a For Each means you don't have to worry at all about the index
Not sure what the type of oVPMOccurrence is as you haven't specified
Most indexes in .net are zero base. I don't know what itemList is but I suspect the index of the first item is 0.
For i = 0 to itemList.Count - 1
oObject = itemList.Item(i)
Next
Not sure why you want to overwrite the value of oObject on every iteration.

VBA: Run-time error '5825' Object has been deleted

I was putting this code ActiveDocument.Variables("time1").Delete before End Sub and I get that error "Object has been deleted" so that if a variable "time1" exist, it will be deleted at the end of the procedure. I understand why I get that code because "time1" has already deleted on the first run but I want to skip and end the sub if it encounter errors. I tried to do this
On Error Goto here
ActiveDocument.Variables("time1").Delete
here:
End sub
but I still get that Error. Why is it that the error handler did not work?
You should be able to avoid this if you are not interested in whether the Variable existed or not.
ActiveDocument.Variables("time1") = ""
should remove a Variable called "time" if there is one, and execute without errors if there is not.
In a similar way,
ActiveDocument.Variables("time1") = "something"
will create the Variable if it does not exist.
This is one of the things that makes Variables slightly easier to work with than Custom Document Properties, although it does mean that empty variables are not allowed.

Replacing nothing with a string

This may be a simple question, but I have searched quite a few sites and have tried a few of my own ideas and I still cannot seem to find a simple way to get Visual Studio to replace all of the listbox items with the string of nothing with some other text.
Using things such as :
For Each S In ListBox1.Items
S.Replace("", "Not Blank")
Next
Shows:
Error
String cannot be of zero length
Which is quite annoying because the actual listbox item contains no text.
This seems to be one of the easiest things I have ever encountered while using vb.net. But it now seems very hard for what should be a simple command.
A couple of problems. The Replace function returns a new value, and you promptly ignore it. Second, you can't really modify the collection as you For-Each over it, so a For-Loop would be more appropriate.
I think you want something like this instead:
For i As Integer = 0 To ListBox1.Items.Count - 1
If String.IsNullOrEmpty(ListBox1.Items(i).ToString) Then
ListBox1.Items(i) = "Not Blank"
End If
Next

How to check if a Paragraph is in a Table or not in MS-Word macro?

The paragraph object in the Word has a property called Range. Within this Range object has a property called Cells.
For paragraph that are not in a table, this property Paragraph.Range.Cells is set to "". This can be seen in the Watches window in debug mode.
For paragraph that are in a table, the property Paragraph.Range.Cells has other properties in it, for example it has a property called Count.
I am using this property of Paragraph.Range.Cells to determine if the paragraph is in a table or not. However, I cant seem to figure out how to test this.
For example, I cannot simply test like this...
If paragraph.Range.Cells <> Null Then.... or even
If IsNull(paragraph.Range.Cells) Then ...
It throws a Run-time error '5907' There is no table at this location
So, how would I test for this? thanks
You can use the Information property:
If Selection.Information(wdWithInTable) Then
'What ever you'd like to do
End If
Hence you don't need any manual error catching mechanisms.
You can't call the Cells method unless the paragraph is in a table. You need to use a different method to determine whether the range is in a table.
You can use either...
paragraph.Range.Tables.Count > 0
...or...
paragraph.Range.Information(wdWithinTable)
Note that the second one looks more obvious, but is actually slower (only a problem if you're doing this inside a loop).
*Edited (if Err=) changed to (If Err<>)
You can simply allow the error to happen and catch it using OnError statement
Dim ParagraphIsTable As Object
OnError Resume Next 'allows errors to happen but execute next instruction
ParagraphIsTable = paragraph.Range.Cells
If Err <> 5907 Then '(this is to check for a specific error that might have happened)
'No Error occured, this means that ParagraphIsTable variable must contain a value
' Do the rest of your code here
Else
' an Error occured, this means that this is not a table
' do whatever
End If
OnError Goto 0 ' This cancels the effect of OnError Resume Next
' Which means if new errors happen, you will be prompt about them

Object reference not set to an instance of an object (Completely broken?) in vb.net

I know, I know, I could have used a for loop, dont tell me anything about that. Please, help!
Private Function LoadSaved() ''//Loads saved clippings if the user wants us to
Dim ZomgSavedClips As StringCollection
If IsDBNull(My.Settings.SavedClips) = False Then ''//If it is null this would return a rather ugly error. Dont want that do we?
ZomgSavedClips = My.Settings.SavedClips ''//ZomgSavedClips name was a joke, I just felt like it.
ZomgSavedClips.Add(" ") ''//This line ought to fix the error, but doesnt
i = 0
While i < ZomgSavedClips.Count ''//This is where the error occurs
ClipListings.Rows.Add(ZomgSavedClips(i))
i = i + 1 ''//First time I wrote this function I forgot this line. Crashed mah comp. Fail.
End While
End If
End Function
The line While i < ZomgSavedClips.Count is bugging, I know that the .count should return null but I even added a blank piece of text just to stop that. Whats up with this? Should I add actual text?
SavedClips is null no? If it is null it could pass the test IsDBNull beacuse the both are not the same
Obviously, My.Settings.SavedClips is still set to Nothing.
SavedClips is regular 'ole null (nothing in VB). Include a check for "My.Settings.SavedClips is nothing". If that evaluates to true then just leave the function.
I even added a blank piece of text just to stop that.
All you did was move where the error happens. You can't call .Add() on a null/Nothing object.
'''<summary>Loads saved clippings if the user wants us to</summary>'
Private Sub LoadSaved() ''//Loads saved clippings if the user wants us to
''//Load saved clips into memory
Dim ZomgSavedClips As StringCollection = My.Settings.SavedClips
If ZomgSavedClips Is Nothing Then ZomgSavedClips = New StringCollection()
''//Apply loaded clips to visible listings
Dim i As Integer
While i < ZomgSavedClips.Count ''
ClipListings.Rows.Add(ZomgSavedClips(i))
i += 1
End While
End Sub
Some notes on this code:
Don't use Function when you mean Sub
Since you'll be selling this code to others, you want to use xml comments at the top so that Visual Studio can give better intellisense helps.
IsDBNull() doesn't do what you think it does.
Yes, you should use a for loop, but since you already commented on that I left the while loop alone with the assumption that there's more code you didn't show us.