TrippyTom
05-16-2009, 11:42 AM
I'm trying to convert a Public String variable into a Double so I can use it to change my margins on a selection, but I keep getting "Type Mismatch". I'm not sure what I'm doing wrong.
Sub MarginAll()
Dim myMargin As Double
myMargin = CDbl(myVal) 'change the String to a Double
With ActiveWindow.Selection.ShapeRange
If ActiveWindow.Selection.ShapeRange.HasTextFrame Then
.TextFrame.MarginLeft = myMargin
.TextFrame.MarginRight = myMargin
.TextFrame.MarginTop = myMargin
.TextFrame.MarginBottom = myMargin
End If
End With
End Sub
John Wilson
05-16-2009, 12:26 PM
Hi Tom ***margin is a Single BTW but your code should work. What does myVal equal?
TrippyTom
05-16-2009, 12:43 PM
It's a text string from a control in a toolbar
Public myVal As String
If MyButton1 Is Nothing Then
Set MyButton1 = Application.CommandBars(YOUR_TOOLBAR_NAME).Controls.Add(Type:=msoControlCom boBox)
End If
With MyButton1
.Width = 50
.Visible = True
End With
Sub SaveSettingIni(iToolBar As CommandBar)
Dim DirLoc As String
DirLoc = "C:\macros\"
With iToolBar
WriteIniValue DirLoc & "margins.ini", "Settings", "Margin", myVal
WriteIniValue DirLoc & "margins.ini", "Settings", "Units", myUnits
WriteIniValue DirLoc & "margins.ini", "Settings", "Position", CStr(.Position)
WriteIniValue DirLoc & "margins.ini", "Settings", "RowIndex", CStr(.RowIndex)
WriteIniValue DirLoc & "margins.ini", "Settings", "Left", CStr(.Left)
WriteIniValue DirLoc & "margins.ini", "Settings", "Top", CStr(.Top)
End With
End Sub
Sub ReadSettingIni(iToolBar As CommandBar)
Dim DirLoc As String
DirLoc = "C:\macros\"
With iToolBar
myVal = ReadIniValue(DirLoc & "margins.ini", "Settings", "Margin")
myUnits = ReadIniValue(DirLoc & "margins.ini", "Settings", "Units")
MyButton2.State = IIf(myUnits = "in", msoButtonDown, msoButtonUp)
MyButton3.State = IIf(myUnits = "cm", msoButtonDown, msoButtonUp)
MyButton1.Text = myVal '<---
.Position = Val(ReadIniValue(DirLoc & "margins.ini", "Settings", "Position"))
.RowIndex = Val(ReadIniValue(DirLoc & "margins.ini", "Settings", "RowIndex"))
.Left = Val(ReadIniValue(DirLoc & "margins.ini", "Settings", "Left"))
.Top = Val(ReadIniValue(DirLoc & "margins.ini", "Settings", "Top"))
End With
End Sub
TrippyTom
05-16-2009, 01:30 PM
I think the reason it's happening is because it thinks myVal is null. I have to figure out a way to read the setting I typed into the control box into myVal like I do when in the ReadSettingIni procedure.
TrippyTom
05-16-2009, 01:59 PM
Ok, I change it to this (not even using the public variable).
It works if I step through the code, but not if I run it normally. If I run it normally I still get the Type Mismatch error. :banghead:
Sub MarginAll()
Dim oldctrl As CommandBarComboBox
Dim myMargin As Single
Set oldctrl = CommandBars("Margins").Controls(2)
myMargin = CSng(oldctrl.Text) 'change the String to a Single
With ActiveWindow.Selection.ShapeRange
If ActiveWindow.Selection.ShapeRange.HasTextFrame Then
.TextFrame.MarginLeft = myMargin * 72
.TextFrame.MarginRight = myMargin * 72
.TextFrame.MarginTop = myMargin * 72
.TextFrame.MarginBottom = myMargin * 72
End If
End With
End Sub
mdmackillop
05-16-2009, 04:00 PM
How about
myMargin = CommandBars("Margins").Controls(2)*1
TrippyTom
05-18-2009, 05:16 PM
Hmm... I'm not sure exactly what I did, but when I tried it at work it worked. Marking the thread solved.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.