PDA

View Full Version : Solved: Using a users InputBox data to set Document Properties (Page Setup)



AreDeeKay
11-01-2012, 02:30 AM
Hello All,
Very new here and to VBA Itself so i apologize in advance for my naivety.

I need to write a macro that uses an InputBox to collect data that will set the documents left and right margins. After much arduous researching through tutorials and the like, i have started with just the left margin, but am receiving this error "Runtime error '13' type mismatch.

i have tried many other ways to get this code to work but all have failed, here is the exact code that i have written.

Option Explicit

Sub SetPageMargins()
Dim strSetLeft As String

strSetLeft = InputBox("Please enter the size of left margin in cm", "Left Margin")

If strSetLeft <> "" Then
With ActiveDocument.PageSetup
.LeftMargin = CentimetersToPoints("")

End With
End If



End Sub



If anyone could point me in the right direction, if i am even heading there at all i would be extremely grateful. or if anyone knows of any tutorials that may help that would help allot also.

Thanks a Bunch!

-AreDeeKay

gmaxey
11-01-2012, 03:32 AM
Your immediate problem is:

.LeftMargin = CentimetersToPoints("") should be:
.LeftMargin = CentimetersToPoints(strSetLeft)

Sub SetPageMargins()
Dim strSetLeft As String
Do Until IsNumeric(strSetLeft) And strSetLeft > "0"
strSetLeft = InputBox("Please enter the size of left margin in cm", "Left Margin")
'If user cancels, get out.
If StrPtr(strSetLeft) = 0 Then GoTo lbl_Exit
Loop
With ActiveDocument.PageSetup
.LeftMargin = CentimetersToPoints(strSetLeft)
End With
lbl_Exit:
Exit Sub
End Sub

AreDeeKay
11-01-2012, 04:20 AM
Many, many thanks to you Greg!

I was way off, i am poking around your website as we speak. and i will be sure to check out the knowledge base here on VBAX also.

Again, thank you so much!

-AreDeeKay

fumei
11-01-2012, 04:03 PM
And be thankful that Word VBA is somewhat forgiving. CentimetersToPoints is a numeric data type (Single), not a string. Word is being quite polite in accepting a string value for CentimetersToPoints - it internally converts it.

Note that if the user typed "ten" this would be acceptable to the InputBox, as it accepts strings, but it would fail as a value for CentimetersToPoints.

Note also that while "" is technically a valid string, Word could NOT convert that to Single.

gmaxey
11-01-2012, 04:51 PM
Gerry,

The code as modified, won't pass "ten" or "" (or I don't think it will.).

fumei
11-01-2012, 05:56 PM
Crap. That is what i get for not fully reading. Didn't notice the IsNumeric.