0

Trying to get this macro to work in Word VBA. Any help to fix this would be greatly appreciated.

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
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Mapleleaf
  • 1
  • 2
  • Therefore you need to tell us what is wrong with your code? What errors do you get and where? • Note that VBA does not accept these smart quotes `“ ”` and you need to replace all of them with the normal quotes `" "` – Pᴇʜ May 19 '21 at 11:22
  • Could you also give us the purpose of the macro? – Charles Kenyon May 19 '21 at 12:11
  • It replaces all of my values with 8MM and I am trying specifically search out the numbers in the Select Case to be changed because there are Imperial numbers I want to leave in the document. – Mapleleaf May 19 '21 at 12:20

3 Answers3

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
FaneDuru
  • 38,298
  • 4
  • 19
  • 27
0

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

Gustav
  • 53,498
  • 7
  • 29
  • 55
-1
Sub AutoOpen()

ConvertTO1MM
ConvertTO1_5MM
ConvertTO2MM
ConvertTO3MM
ConvertTO4MM
ConvertTO5MM
ConvertTO6MM
ConvertTO8MM
ConvertTO10MM
ConvertTO12MM

End Sub

Sub ConvertTO1MM()

Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document
Dim mm As Variant

Set wrdDoc = Application.ActiveDocument

Set wrdRng = wrdDoc.Content

Set wrdFind = wrdRng.Find

With wrdFind

    .Text = "0.039"
                                           
    SearchResult = .Execute
                                                            
    Select Case .Text
        Case Is = 0.039
            mm = Round(.Text * 25.4, 0) & "MM"
    End Select
                                                            
End With
                                                                                                               
    If SearchResult = True Then
                                                                                                               
        wrdRng.Text = mm
                                                                                                                            
    End If
                                                                                                                            
End Sub

Sub ConvertTO1_5MM()

Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document
Dim mm As Variant

Set wrdDoc = Application.ActiveDocument

Set wrdRng = wrdDoc.Content

Set wrdFind = wrdRng.Find

With wrdFind

    .Text = "0.059"
                                           
    SearchResult = .Execute
                                                            
    Select Case .Text
        Case Is = 0.059
            mm = Round(.Text * 25.4, 1) & "MM"
    End Select
                                                            
End With
                                                                                                               
    If SearchResult = True Then
                                                                                                               
        wrdRng.Text = mm
                                                                                                                            
    End If
                                                                                                                            
End Sub

Sub ConvertTO2MM()

Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document
Dim mm As Variant

Set wrdDoc = Application.ActiveDocument

Set wrdRng = wrdDoc.Content

Set wrdFind = wrdRng.Find

With wrdFind

    .Text = "0.079"
                                           
    SearchResult = .Execute
                                                            
    Select Case .Text
        Case Is = 0.079
            mm = Round(.Text * 25.4, 0) & "MM"
    End Select
                                                            
End With
                                                                                                               
    If SearchResult = True Then
                                                                                                               
        wrdRng.Text = mm
                                                                                                                            
    End If
                                                                                                                            
End Sub

Sub ConvertTO3MM()

Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document
Dim mm As Variant

Set wrdDoc = Application.ActiveDocument

Set wrdRng = wrdDoc.Content

Set wrdFind = wrdRng.Find

With wrdFind

    .Text = "0.118"
                                           
    SearchResult = .Execute
                                                            
    Select Case .Text
        Case Is = 0.118
            mm = Round(.Text * 25.4, 0) & "MM"
    End Select
                                                            
End With
                                                                                                               
    If SearchResult = True Then
                                                                                                               
        wrdRng.Text = mm
                                                                                                                            
    End If
                                                                                                                            
End Sub

Sub ConvertTO4MM()

Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document
Dim mm As Variant

Set wrdDoc = Application.ActiveDocument

Set wrdRng = wrdDoc.Content

Set wrdFind = wrdRng.Find

With wrdFind

    .Text = "0.157"
                                           
    SearchResult = .Execute
                                                            
    Select Case .Text
        Case Is = 0.157
            mm = Round(.Text * 25.4, 0) & "MM"
    End Select
                                                            
End With
                                                                                                               
    If SearchResult = True Then
                                                                                                               
        wrdRng.Text = mm
                                                                                                                            
    End If
                                                                                                                            
End Sub

Sub ConvertTO5MM()

Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document
Dim mm As Variant

Set wrdDoc = Application.ActiveDocument

Set wrdRng = wrdDoc.Content

Set wrdFind = wrdRng.Find

With wrdFind

    .Text = "0.196"
                                           
    SearchResult = .Execute
                                                            
    Select Case .Text
        Case Is = 0.196
            mm = Round(.Text * 25.4, 0) & "MM"
    End Select
                                                            
End With
                                                                                                               
    If SearchResult = True Then
                                                                                                               
        wrdRng.Text = mm
                                                                                                                            
    End If
                                                                                                                            
End Sub

Sub ConvertTO6MM()

Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document
Dim mm As Variant

Set wrdDoc = Application.ActiveDocument

Set wrdRng = wrdDoc.Content

Set wrdFind = wrdRng.Find

With wrdFind

    .Text = "0.236"
                                           
    SearchResult = .Execute
                                                            
    Select Case .Text
        Case Is = 0.236
            mm = Round(.Text * 25.4, 0) & "MM"
    End Select
                                                            
End With
                                                                                                               
    If SearchResult = True Then
                                                                                                               
        wrdRng.Text = mm
                                                                                                                            
    End If
                                                                                                                            
End Sub

Sub ConvertTO8MM()

Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document
Dim mm As Variant

Set wrdDoc = Application.ActiveDocument

Set wrdRng = wrdDoc.Content

Set wrdFind = wrdRng.Find

With wrdFind

    .Text = "0.315"
                                           
    SearchResult = .Execute
                                                            
    Select Case .Text
        Case Is = 0.315
            mm = Round(.Text * 25.4, 0) & "MM"
    End Select
                                                            
End With
                                                                                                               
    If SearchResult = True Then
                                                                                                               
        wrdRng.Text = mm
                                                                                                                            
    End If
                                                                                                                            
End Sub

Sub ConvertTO10MM()

Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document
Dim mm As Variant

Set wrdDoc = Application.ActiveDocument

Set wrdRng = wrdDoc.Content

Set wrdFind = wrdRng.Find

With wrdFind

    .Text = "0.394"
                                           
    SearchResult = .Execute
                                                            
    Select Case .Text
        Case Is = 0.394
            mm = Round(.Text * 25.4, 0) & "MM"
    End Select
                                                            
End With
                                                                                                               
    If SearchResult = True Then
                                                                                                               
        wrdRng.Text = mm
                                                                                                                            
    End If
                                                                                                                            
End Sub

Sub ConvertTO12MM()

Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document
Dim mm As Variant

Set wrdDoc = Application.ActiveDocument

Set wrdRng = wrdDoc.Content

Set wrdFind = wrdRng.Find

With wrdFind

    .Text = "0.472"
                                           
    SearchResult = .Execute
                                                            
    Select Case .Text
        Case Is = 0.472
            mm = Round(.Text * 25.4, 0) & "MM"
    End Select
                                                            
End With
                                                                                                               
    If SearchResult = True Then
                                                                                                               
        wrdRng.Text = mm
                                                                                                                            
    End If
                                                                                                                            
End Sub
Timothy Rylatt
  • 7,221
  • 2
  • 10
  • 14
Mapleleaf
  • 1
  • 2