Wherein have I wrong?

A

avkokin

Hello.
I create one macro that should to show info about paper size. Then I
tryed make it in function but I get error. That code.Wherein have I
wrong?

Sub psize()
MsgBox IdentifyPaperSize(ps)
End Sub

Function IdentifyPaperSize(ps As String) As String
'Identify Paper Size
Dim iPaperSize As Integer
Dim ps(42) As String
iPaperSize = Selection.PageSetup.papersize
ps(0) = "wdPaper10x14"
ps(1) = "wdPaper11x17"
ps(2) = "wdPaperLetter"
ps(3) = "wdPaperLetterSmall"
ps(4) = "wdPaperLegal"
ps(5) = "wdPaperExecutive"
ps(6) = "wdPaperA3"
ps(7) = "wdPaperA4"
ps(8) = "wdPaperA4Small"
ps(9) = "wdPaperA5"
ps(10) = "wdPaperB4"
ps(11) = "wdPaperB5"
ps(12) = "wdPaperCSheet"
ps(13) = "wdPaperDSheet"
ps(14) = "wdPaperESheet"
ps(15) = "wdPaperFanfoldLegalGerman"
ps(16) = "wdPaperFanfoldStdGerman"
ps(17) = "wdPaperFanfoldUS"
ps(18) = "wdPaperFolio"
ps(19) = "wdPaperLedger"
ps(20) = "wdPaperNote"
ps(21) = "wdPaperQuarto"
ps(22) = "wdPaperStatement"
ps(23) = "wdPaperTabloid"
ps(24) = "wdPaperEnvelope9"
ps(25) = "wdPaperEnvelope10"
ps(26) = "wdPaperEnvelope11"
ps(27) = "wdPaperEnvelope12"
ps(28) = "wdPaperEnvelope14"
ps(29) = "wdPaperEnvelopeB4"
ps(30) = "wdPaperEnvelopeB5"
ps(31) = "wdPaperEnvelopeB6"
ps(32) = "wdPaperEnvelopeC3"
ps(33) = "wdPaperEnvelopeC4"
ps(34) = "wdPaperEnvelopeC5"
ps(35) = "wdPaperEnvelopeC6"
ps(36) = "wdPaperEnvelopeC65"
ps(37) = "wdPaperEnvelopeDL"
ps(38) = "wdPaperEnvelopeItaly"
ps(39) = "wdPaperEnvelopeMonarch"
ps(40) = "wdPaperEnvelopePersonal"
ps(41) = "wdPaperCustom"

IdentifyPaperSize = ps(iPaperSize)

End Function
 
J

Jean-Guy Marcil

avkokin said:
Hello.
I create one macro that should to show info about paper size. Then I
tryed make it in function but I get error. That code.Wherein have I
wrong?

A couple of comments on your code...

Do not use Integer anymore. The compiler actually converts the Integer type
into a Long type. So your effort to save memory are useless because the
smallest "allocatable" memory unit is a Long, and it slows down the execution
because the compiler needs to do a type conversion.

Do use Option Explicit at the top of each module. This will help you detect
all kinds of mistakes before actually executing code.
In your case, you would have been warned that "ps" in the "psize" sub was
not defined. It might have help you realize that you are passing a totally
empty variable to a function...
Then, once you would have fixed that, you would have been warned that "ps"
is defined twice in the "IdentifyPaperSize" Function: "ps As String" and "Dim
ps(42) As String".
To automatically activate "Option Explicit", in the VBA editor window, do
Tools > Options... > "Editor" tab > check "Require Variable Declaration" in
the "Code Settings" section of the tab. I always use Option Explicit and it
saves me from the many mistakes I make from typing too fast or not paying
attention to what I actually type... Also, it makes debugging easier.

Try this:
"IdentifyPaperSize(Selection.PageSetup.PaperSize)"
calls the "IdentifyPaperSize" Function and passes to it the
"Selection.PageSetup.PaperSize" parameter, which is a Long.


Option Explicit

Sub psize()

MsgBox IdentifyPaperSize(Selection.PageSetup.PaperSize)

End Sub

Function IdentifyPaperSize(lngPS As Long) As String

Dim ps(42) As String

ps(0) = "wdPaper10x14"
ps(1) = "wdPaper11x17"
ps(2) = "wdPaperLetter"
ps(3) = "wdPaperLetterSmall"
ps(4) = "wdPaperLegal"
ps(5) = "wdPaperExecutive"
ps(6) = "wdPaperA3"
ps(7) = "wdPaperA4"
ps(8) = "wdPaperA4Small"
ps(9) = "wdPaperA5"
ps(10) = "wdPaperB4"
ps(11) = "wdPaperB5"
ps(12) = "wdPaperCSheet"
ps(13) = "wdPaperDSheet"
ps(14) = "wdPaperESheet"
ps(15) = "wdPaperFanfoldLegalGerman"
ps(16) = "wdPaperFanfoldStdGerman"
ps(17) = "wdPaperFanfoldUS"
ps(18) = "wdPaperFolio"
ps(19) = "wdPaperLedger"
ps(20) = "wdPaperNote"
ps(21) = "wdPaperQuarto"
ps(22) = "wdPaperStatement"
ps(23) = "wdPaperTabloid"
ps(24) = "wdPaperEnvelope9"
ps(25) = "wdPaperEnvelope10"
ps(26) = "wdPaperEnvelope11"
ps(27) = "wdPaperEnvelope12"
ps(28) = "wdPaperEnvelope14"
ps(29) = "wdPaperEnvelopeB4"
ps(30) = "wdPaperEnvelopeB5"
ps(31) = "wdPaperEnvelopeB6"
ps(32) = "wdPaperEnvelopeC3"
ps(33) = "wdPaperEnvelopeC4"
ps(34) = "wdPaperEnvelopeC5"
ps(35) = "wdPaperEnvelopeC6"
ps(36) = "wdPaperEnvelopeC65"
ps(37) = "wdPaperEnvelopeDL"
ps(38) = "wdPaperEnvelopeItaly"
ps(39) = "wdPaperEnvelopeMonarch"
ps(40) = "wdPaperEnvelopePersonal"
ps(41) = "wdPaperCustom"

IdentifyPaperSize = ps(lngPS)

End Function
 
S

StevenM

To: Jean-Guy Marcil,

Re: << Do not use Integer anymore. The compiler actually converts the
Integer type into a Long type. So your effort to save memory are useless
because the smallest "allocatable" memory unit is a Long, and it slows down
the execution because the compiler needs to do a type conversion. >>

Is this in all situations, or in this particular situation?

For example, let's say one creates a simple loop like:

For i = 1 to 5

Should the variable 'i' always be a long?

Steven Craig Miller
 
J

Jean-Guy Marcil

StevenM said:
To: Jean-Guy Marcil,

Re: << Do not use Integer anymore. The compiler actually converts the
Integer type into a Long type. So your effort to save memory are useless
because the smallest "allocatable" memory unit is a Long, and it slows down
the execution because the compiler needs to do a type conversion. >>

Is this in all situations, or in this particular situation?

For example, let's say one creates a simple loop like:

For i = 1 to 5

Should the variable 'i' always be a long?

The compiler does not look at "context".
Integers are converted to Longs simply because the smallest unit of memory
that can be addressed is now a long.
At least, this is what I understood when it was taught to me... I may have
misunderstood!
 
A

avkokin

The compiler does not look at "context".
Integers are converted to Longs simply because the smallest unit of memory
that can be addressed is now a long.
At least, this is what I understood when it was taught to me... I may have
misunderstood!

Thank you very much! I did use Option Explicit but I unknow about
converting Integer into Long.
Sincerely, Anton Kokin
 
Top