kerrypnx
6/19/2018 - 4:10 AM

word2tex

8-17 revision

Sub word2tex(ByVal control As IRibbonControl)
With ActiveDocument.Content.Find
  .Text = "^p"
  .Font.Bold = 1
  .MatchWildcards = False
  .Replacement.Text = "^p"
  .Replacement.Font.Bold = 0
  .Replacement.Font.Italic = 0
  .Execute Replace:=wdReplaceAll
End With

Call formatSelect("italic", "\emph{")

Call formatSelect("bold", "\textbf{")

Call formatSelect("superscript", "\textsuperscript{")



Call formatSelect("subscript", "\textsubscript{")


 
End Sub
Sub formatSelect(str As String, str1 As String)
Dim seclen As Integer
    Selection.HomeKey wdStory

    With Selection.Find
    .ClearFormatting
    .Text = ""
    .Wrap = wdFindStop
    .MatchWildcards = False
    
    Select Case str
    
    Case "bold"
    .Font.Bold = -1
    
    Case "italic"
    .Font.Italic = -1
    
    Case "subscript"
    .Font.Subscript = -1
    
    Case "superscript"
    .Font.Superscript = -1
    
    End Select
    
    .Replacement.Text = ""
    
    Do
    .Execute
    If Not .Found Then
    Exit Do
    Else
    
      If Asc(Selection.Text) = 13 Then
        seclen = Len(Selection.Range.Text) + Len(str1) + 2
        Selection.Range.Text = str1 + "\textsubscript{" + Selection.Range.Text + "}"
        Selection.MoveRight wdCharacter, seclen
      Else
        seclen = Len(Selection.Range.Text) + Len(str1) + 1
        Selection.Range.Text = str1 + Selection.Range.Text + "}"
        Selection.MoveRight wdCharacter, seclen
      End If
   
    End If
    Loop
  End With
  
End Sub