Hi All,
I have been trying to create a VBA that will detect if a level exists, if it does then set that level to active, if it doesn't then create that level and set it active. I wrote a code that I thought would work, but I am receiving a compile error saying that an object is required with the following part of the code highlighted:
Sub CreateOrActiveLevel()
Dim LevelName As StringDim LinksLevel As LevelDim oLevel As Level
Set LevelName = "Links"Set LinksLevel = ActiveDesignFile.Levels(Links).IsDisplayedIf LinksLevel = True Then ActiveSettings.Level = ActiveDesignFile.Levels(Links) Else Set oLevel = ActiveDesignFile.AddNewLevel(LevelName) Levels.Rewrite ActiveSettings.Level = ActiveDesignFile.Levels(Links)End If
End Sub
I have already found a similar post relating to this (http://communities.bentley.com/products/programming/microstation_programming/f/343173/t/88062) but despite Jon Summers always helpful and informative posts, I couldn't get my own to work. Can anyone see where I'm going wrong?
P.S. I am new to VBAs and coding, so it would be great if answers were kept in laymans terms.
Jeff
Unknown said:I have been trying to create a VBA that will detect if a level exists
We wrote this article on that topic.
Unknown said: Set LevelName = "Links"
Set LevelName = "Links"
The Set keyword is required in an assignment when the target is an object. A String is not an object (at least within the rules of VBA), and the Set keyword should not be used. The error message is telling you that it's seen the Set keyword, and expects an object to follow. Just do this:
LevelName = "Links"
This VBA syntax has befuddled many of us over the years. It may be the reason why Microsoft invented VB.NET, which is more straightforward.
Regards, Jon Summers LA Solutions
Charles (Chuck) Rheault CADD Manager
MDOT State Highway Administration
How do you test function CreateOrFindLevel? Have you written a test wrapper...
Sub TestCreateOrFindLevel () Const NewLevelName As String = "Be Communities" Debug.Print "Create or find level '" & NewLevelName & "'" CreateOrFindLevel NewLevelName End Sub
What happens in the Level Manager window? What do you find when you step through that code line-by-line (Function key F8)?
Hi Jeff,
I rewrite your original VBA code as below. It works for me.
Sub CreateOrActiveLevel() Dim LevelName As String Dim oLevel As Level LevelName = "Links" Set oLevel = ActiveDesignFile.Levels.Find(LevelName) If oLevel Is Nothing Then Set oLevel = ActiveDesignFile.AddNewLevel(LevelName) ActiveDesignFile.Levels.Rewrite End If ActiveSettings.Level = oLevel End Sub
HTH, YongAn
Unknown said:Test wrappers are a bit of a new concept to me.
A test wrapper, just like the example I provided, is a simple procedure that has but one goal. In the world of software engineering they are termed unit tests. It's purpose is to test some other procedure(s), with no other side-effects.
Unknown said: I would assume for a test wrapper to be the first sub in a macro
A curious assumption. A test wrapper can be anywhere you like. I prefer to place them next to the procedure they're intended to test, make them Private, and assign them a wacky name so their purpose is clear.