PDA

View Full Version : fly an executable VBA expressions



jspattavina
10-18-2011, 10:00 AM
I want to be able to build an execute a VBA expression base on dropdownlist and/or inputbox. How can I convert the string returned by these objects to create a VBA expreesion at runtime?
Example: I have a subroutine that goes to a specific line.

Public Sub GoToLinePrevious(x)
Selection.GoTo What:=wdGoToLine, Which:=wdGoToPrevious, Count:=x
End Sub

I would like the user to be able to change the What:=wdGoToLine to say What:=wdGoToParagraph, based on the user selecting "paragraph" instead of selecting "Line"

Thus I can "build" on the fly an executable VBA expression or subroutine call.
An alternative is to be able to pass arguments to a subroutine, based on User input.

Thanks

ptvn

Frosty
10-21-2011, 10:06 AM
Without being snarky... this is really already built into Word (and many other programs). It is...

Arrow Up (or down) key
And CTRL + Arrow Up (or down) key.

And pressing those multiple times are going to get you where you want to go probably faster than requiring the user to interact with a dialog box where the user chooses a value for X.

I'm not sure how you're getting your "x" value (unless you have a different function determining it for you)... So if you're grabbing that value from user input, then you're requiring some kind of user input, at which point you might as well build your function to be completely robust, so that it is...


Public Sub GoToSomething (bParagraph as Boolean, bUp as boolean, iCount as Integer)
If bParagraph Then
If bUp Then
Selection.MoveUp Unit:=wdParagraph, Count:=iCount
Else
Selection.MoveDown Unit:=wdParagraph, Count:=iCount
End If
Else
If bUp Then
Selection.MoveUp Unit:=wdLine, Count:=iCount
Else
Selection.MoveDown Unit:=wdLine, Count:=iCount
End If
End If
End Sub
There are many ways to approach this (and many logic structures... you could use AND and ElseIf structures too, or variables to determine values to pass to a single Selection.GoTo line, change the arguments of the function actually be the arguments used in the GoTo line... but the above works as well as any).

The Selection object probably isn't the way to go, but you'd need to give more info to give you an ultimately "better" function than what you're asking for. I suspect you probably want your "X" to be something greater than 5 or 10 (which, to me, is the area where I might think this would be more efficient than simply pressing the arrow key X amount of times, with or without the CTRL key also pressed)... at which point I think you should probably describe what you really want to accomplish, because this may not be the best approach.

Frosty
10-21-2011, 10:13 AM
Hmm, re-reading your question... it actually seems like a concept question. The short answer is... things like wdGotoParagraph and wdGoToPrevious are actually built in enumerations. Those are "long" variable types.

Converting string values to long is either easy or impossible.

CLng is the conversion function, but obviously "" and "abc" will fail to convert. You can use the Object Browser to find the specific values of particular enumerations, but there is no built-in process to allow you to convert "wdGotoParagraph" into the actual enumeration wdGotoParagraph. Easier to know that the value is... and when I just looked it up, wdGotoParagraph doesn't even exist...

However, you can use Selection.MoveDown Unit:=wdParagraph, Count:=x
.MoveUp also works... will edit the above code to reflect this.

Basically-- check out the object browser.

gmaxey
10-23-2011, 06:14 AM
Private Sub ListBox1_Click()
Select Case ListBox1.ListIndex
Case 0
ActiveDocument.Variables("GoToWhere").Value = 11 'Heading. There is no enumeration for paragraph
Case 1
ActiveDocument.Variables("GoToWhere").Value = 3
End Select
End Sub
Public Sub GoToLinePrevious(x)
Selection.GoTo What:=ActiveDocument.Variables("GoToWhere"), Which:=wdGoToPrevious, Count:=x
End Sub
Sub Test()
GoToLinePrevious 4
End Sub


Note: There is no such thing as wdGoToParagraph.



I want to be able to build an execute a VBA expression base on dropdownlist and/or inputbox. How can I convert the string returned by these objects to create a VBA expreesion at runtime?
Example: I have a subroutine that goes to a specific line.

Public Sub GoToLinePrevious(x)
Selection.GoTo What:=wdGoToLine, Which:=wdGoToPrevious, Count:=x
End Sub

I would like the user to be able to change the What:=wdGoToLine to say What:=wdGoToParagraph, based on the user selecting "paragraph" instead of selecting "Line"

Thus I can "build" on the fly an executable VBA expression or subroutine call.
An alternative is to be able to pass arguments to a subroutine, based on User input.

Thanks

ptvn

jspattavina
11-01-2011, 09:00 AM
Private Sub ListBox1_Click()
Select Case ListBox1.ListIndex
Case 0
ActiveDocument.Variables("GoToWhere").Value = 11 'Heading. There is no enumeration for paragraph
Case 1
ActiveDocument.Variables("GoToWhere").Value = 3
End Select
End Sub
Public Sub GoToLinePrevious(x)
Selection.GoTo What:=ActiveDocument.Variables("GoToWhere"), Which:=wdGoToPrevious, Count:=x
End Sub
Sub Test()
GoToLinePrevious 4
End Sub


Note: There is no such thing as wdGoToParagraph.