問題描述
使用 Word VBA 中的宏選擇大小寫將英寸轉換為毫米 (Convert inch to mm using macro select Case in Word VBA)
試圖讓這個宏在 Word VBA 中工作。任何解決此問題的幫助將不勝感激。
Sub ConvertToMM()
Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document
Dim inch_in As Integer
Dim mm_out As Variant
Set wrdDoc = Application.ActiveDocument
Set wrdRng = wrdDoc.Content
Set wrdFind = wrdRng.Find
inch_in = CVar(mm_out)
mm_out = “”
With wrdFind
Select Case inch_in
Case Is = 0.039
mm_out = “1MM”
Case Is = 0.059
mm_out = “1.5MM”
Case Is = 0.079
mm_out = “2MM”
Case Is = 0.118
mm_out = “3MM”
Case Is = 0.157
mm_out = “4MM”
Case Is = 0.236
mm_out = “6MM”
Case Is = 0.315
mm_out = “8MM”
Case Is = 0.394
mm_out = “10MM”
Case Is = 0.472
mm_out = “12MM”
End Select
End With
wrdRng.Text = mm_out
End Sub
參考解法
方法 1:
Try using the next function, please:
Function InchToMM(i As Double) As String
InchToMM = Round(i * 25.4, 0) & "MM"
End Function
It can be called/tested with such a code:
Sub testConvertInchToMM()
Dim inc As String
inc = "0.039" 'use Find or whatever you want to determine it...
MsgBox InchToMM(CDbl(inc))
End Sub
方法 2:
First, do replace the smart quotes from Word with real double‑quotes:
Set wrdFind = wrdRng.Find
inch_in = CVar(mm_out)
mm_out = ""
With wrdFind
Select Case inch_in
Case Is = 0.039
mm_out = "1MM"
Case Is = 0.059
mm_out = "1.5MM"
Case Is = 0.079
mm_out = "2MM"
Case Is = 0.118
mm_out = "3MM"
Case Is = 0.157
mm_out = "4MM"
Case Is = 0.236
mm_out = "6MM"
Case Is = 0.315
mm_out = "8MM"
Case Is = 0.394
mm_out = "10MM"
Case Is = 0.472
mm_out = "12MM"
End Select
End With
wrdRng.Text = mm_out
If that "doesn't work", consider a generic method for your task as found here:
Converting between meters and inches
(by Mapleleaf、FaneDuru、Gustav)