PDA

View Full Version : Dim a variable as a statement



nhmabv
10-12-2014, 09:47 PM
I am using rather long statements in my macros, EG

N1=Selection.Information(wdHorizontalPositionRelativeToTextBoundary)
I would like to shorten that, I tried the following:

Dim CurrPosition as Object
set CurrPosition =Selection.Information(wdHorizontalPositionRelativeToTextBoundary)

But that does not work. Is there a way to do this?

Thanks Mucho

gmayor
10-12-2014, 10:31 PM
Almost there - try


Dim CurrPosition As Single
If Documents.Count > 0 Then
CurrPosition = Selection.Information(wdHorizontalPositionRelativeToTextBoundary)
MsgBox CurrPosition
Else
MsgBox "Cursor is not in a document!"
End If

macropod
10-12-2014, 10:49 PM
If all you're after is shortening:
Selection.Information(wdHorizontalPositionRelativeToTextBoundary)
you could use:
Selection.Information(7)

This could translate to:
Dim CurrPosition as Single
CurrPosition =Selection.Information(7)

gmaxey
10-13-2014, 05:31 AM
If you are wondering how to better determine the appropriate data type (e.g., single instead of object), you can use the Locals window (View>Locals).

Start by declaring CurPosition as a variant in your codee.g.:
Dim CurPosition

Step through the procedure using F8 and watch the variable in the Locals window. You will see it initially as variant/empty. When it gets its value it changes to variant/single. Now you know you can use a single data type.

SamT
10-13-2014, 07:34 AM
Greg,

Nice tip. Gotta remember that.

fumei
10-13-2014, 03:22 PM
Also, if you think about whatever it is you can often come up with a possible data type.

wdHorizontalPositionRelativeToTextBoundary returns a NUMBER, a VALUE.

Therefore, one of the number data types (Single, Double, Long) is appropriate.

"Object" is used for "things". A table. A ContentControl. Although, even then it is better to use the exact object name as a the declaration.

But I agree with macropod. If it is simply a matter of trying to reduce long text, look up the actual value used by VBA for the term (in this case wdHorizontalPositionRelativeToTextBoundary) and use that. It is in fact what VBA does. VBA translates ALL textual terms into a numeric value.

gmaxey
10-13-2014, 03:38 PM
To look up just paste the constant into the object browser.

I don't really see the point though of purposefully or always replacing constants with there numerical equivalent just to reduce long text. If one is not careful ones code can become cryptic and nearly meaningless to anyone but themselves. There are a couple of exceptions though. For example if you want to reference a checkbox content control in code it can be to your advantage to use 8 instead of wdContentControlCheckBox because the latter would result in a compile error in Word 2007 which doesn't have a checkbox CC.

There are also times when the generic object is better than the actual object e.g., ContentControl. For example if you use:

Dim oCC as ContentControl
Set oCC = ActiveDocument.SelectContentControlsByTitles(RS1).Item(1)
oCC.oRSCC.AllowInsertDeleteSection

in Word 2010 you would get a compile error because Word 2010 doesn't have a repeating section CC and its methods wouldn't compile.

This however would:
Dim oCC as Object
Set oCC = ActiveDocument.SelectContentControlsByTitles(RS1).Item(1)
oCC.oRSCC.AllowInsertDeleteSection

fumei
10-13-2014, 08:05 PM
Oh I totally agree. It is not something I would use all the time. It is GOOD to be able to use text. It is handy sometimes though. Oddly one of the most common times I use a number is with Collapse. And certainly there are appropriate places to use Object.

Bob Phillips
10-14-2014, 01:59 AM
To look up just paste the constant into the object browser.

I don't really see the point though of purposefully or always replacing constants with there numerical equivalent just to reduce long text. If one is not careful ones code can become cryptic and nearly meaningless to anyone but themselves. There are a couple of exceptions though. For example if you want to reference a checkbox content control in code it can be to your advantage to use 8 instead of wdContentControlCheckBox because the latter would result in a compile error in Word 2007 which doesn't have a checkbox CC.

There are also times when the generic object is better than the actual object e.g., ContentControl. For example if you use:

Dim oCC as ContentControl
Set oCC = ActiveDocument.SelectContentControlsByTitles(RS1).Item(1)
oCC.oRSCC.AllowInsertDeleteSection

in Word 2010 you would get a compile error because Word 2010 doesn't have a repeating section CC and its methods wouldn't compile.

This however would:
Dim oCC as Object
Set oCC = ActiveDocument.SelectContentControlsByTitles(RS1).Item(1)
oCC.oRSCC.AllowInsertDeleteSection

Wouldn't you use conditional compilation to manage those differences?

gmaxey
10-14-2014, 06:15 AM
xld,

I certainly would not be adverse to that and have even tried. However, as what little I know about VBA has been gained through sometimes frustrating trial and error or monkey see monkey do, I was unsuccessful. There was no combination of conditional coding that I could imagine that worked. I would certainly be pleased if you could illustrate or explain how I could use conditional coding such that the following would run in Word 2010:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oCC As ContentControl
For Each oCC In ActiveDocument.ContentControls
If oCC.Type = 9 Then oCC.AllowInsertDeleteSection = True
Next oCC
End Sub