使用 Word VBA 中的宏選擇大小寫將英寸轉換為毫米 (Convert inch to mm using macro select Case in Word VBA)


問題描述

使用 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 MapleleafFaneDuruGustav)

參考文件

  1. Convert inch to mm using macro select Case in Word VBA (CC BY‑SA 2.5/3.0/4.0)

#select-case #ms-word #vba






相關問題

SQL 加入 2 個共享列的查詢 (SQL joining 2 queries that share a column)

sql中的條件總和,根據字段中的條件從另一個表中獲取正確的總和 (conditional sum in sql, get correct sum from another table based on a condition in a field)

如何對此查詢進行選擇案例? (How can I do select case to this query?)

聲明一個案例列表在多個地方使用 (Declaring a case list to be used in more than one place)

選擇案例語句中的數組是否可能? (Is an array inside a select case statement possible?)

將 VBA 選擇案例轉換為 C# (Convert VBA select case to C#)

使用 Word VBA 中的宏選擇大小寫將英寸轉換為毫米 (Convert inch to mm using macro select Case in Word VBA)

訪問,選擇案例 ActiveControl.Name? (Access, Select Case ActiveControl.Name?)

選擇不運行每個案例的案例 (Select Case Not Running Every Case)

JustBasic,在代碼中出現“錯誤的嵌套錯誤選擇/案例” (JustBasic, getting "bad nesting error select/case" in code)







留言討論