2017-12-12 6 views
0

docxファイルには、数学オブジェクト(mml)の特定の表記を含むテキストがたくさんあります。これらの表記法は、特定の区切り文字(MATHSTARTとMATHEND)で囲まれています。書式なしのテキストを置換するVBA

例:

MATHSTART<math xmlns="http://www.w3.org/1998/Math/MathML" display="block" alttext="x equals StartFraction negative b plus-or-minus StartRoot b squared minus 4 a c EndRoot Over 2 a EndFraction"> 
    <semantics> 
    <mrow> 
     <mi>x</mi> 
     <mo>=</mo> 
     <mrow class="MJX-TeXAtom-ORD"> 
     <mfrac> 
      <mrow> 
      <mo>−<!-- − --></mo> 
      <mi>b</mi> 
      <mo>±<!-- ± --></mo> 
      <mrow class="MJX-TeXAtom-ORD"> 
       <msqrt> 
       <msup> 
        <mi>b</mi> 
        <mrow class="MJX-TeXAtom-ORD"> 
        <mn>2</mn> 
        </mrow> 
       </msup> 
       <mo>−<!-- − --></mo> 
       <mn>4</mn> 
       <mi>a</mi> 
       <mi>c</mi> 
       </msqrt> 
      </mrow> 
      </mrow> 
      <mrow> 
      <mn>2</mn> 
      <mi>a</mi> 
      </mrow> 
     </mfrac> 
     </mrow> 
    </mrow> 
    <annotation encoding="application/x-tex">x={-b\pm {\sqrt {b^{2}-4ac}} \over 2a}</annotation> 
    </semantics> 
</math>MATHEND 
私は今、すべてのこれらの部品をフェッチして、区切り文字(演算スタートとMATHEND)せずにもう一度戻ってそれらを入れて、文書からそれらをカットするためにマルコスを使用したい

とフォーマットなし(wdFormatPlainTextのようなもの)。望ましい結果は、docxの数式です。私はこれまで持って何

docx math equation object

Dim regex As Object, wholeDocText As String 
Set regex = CreateObject("VBScript.RegExp") 
Selection.WholeStory 
Selection.Copy 
wholeDocText = Selection.Text 

With regex 
    .Pattern = "MATHSTART[.\s\S]*?MATHEND" 
    .Global = True 
End With 

Set matches = regex.Execute(wholeDocText) 

For Each match In matches 
    s1 = Replace(match.Value, "MATHSTART", "") 
    s1 = Replace(s1, "MATHEND", "") 

    'select match.Value in the document 
    'overwrite the selected string with the new one 
    'sth like: Selection.Text s1(wdFormatPlainText) 
Next match 

問題は、私が実装する方法がわからないコメントの三行、です。

+0

単にコメントコードの代わりに 'match = s1'を書くだけですか? – Vityata

+0

@Vityataは単に変数の値を変更するだけですが、書式設定なしで上書きされるようにドキュメントのテキスト部分をマークするスクリプトが必要です。 – kPeach

答えて

0

注:(

s1 = Replace(CopyRange.Text, "MATHSTART", "") 
s1 = Replace(s1, "MATHEND", "") 

Selection.Text = s1 
Selection.Copy 
Selection.PasteAndFormat (wdFormatPlainText) 

copyRangeのがMATHENDする演算スタートから文字列全体である:私は、実際の作業は、この部分で起こるこのanswer

Sub convertMmlToWordField() 

Dim StartWord As String, EndWord As String 
Dim FindStartRange As Range, FindEndRange As Range 
Dim CopyRange As Range, CopyStartRange As Range, CopyEndRange As Range 

Set FindStartRange = ActiveDocument.Range 
Set FindEndRange = ActiveDocument.Range 
Set CopyRange = ActiveDocument.Range 

StartWord = "MATHSTART" 
EndWord = "MATHEND" 

'Starting the Lookup for the starting word 
With FindStartRange.find 
    .Text = StartWord 
    .Replacement.Text = "" 
    .Forward = True 
    .Wrap = wdFindAsk 
    .Format = False 
    .MatchCase = False 
    .MatchWholeWord = False 
    .MatchWildcards = False 
    .MatchSoundsLike = False 
    .MatchAllWordForms = False 

    'Execute the Lookup 
    Do While .Execute 
     If .Found = True Then 
      Set CopyStartRange = FindStartRange 
      CopyStartRange.Select 

      'Setting the FindEndRange up for the remainder of the document beginning from the end of the StartWord 
      FindEndRange.Start = CopyStartRange.End 
      FindEndRange.End = ActiveDocument.Content.End 
      FindEndRange.Select 

      'Setting the Find to look for the End Word 
      With FindEndRange.find 
       .Text = EndWord 
       .Execute 

       If .Found = True Then 
        Set CopyEndRange = FindEndRange 
        CopyEndRange.Select 
       End If 
      End With 

      'Selecting the copy range 
      CopyRange.Start = CopyStartRange.Start 
      CopyRange.End = CopyEndRange.End 
      CopyRange.Select 

      s1 = Replace(CopyRange.Text, "MATHSTART", "") 
      s1 = Replace(s1, "MATHEND", "") 

      Selection.Text = s1 
      Selection.Copy 
      Selection.PasteAndFormat (wdFormatPlainText) 
     End If  'Ending the If Find1stRange .Found = True 
    Loop  'Ending the Do While .Execute Loop 
End With 'Ending the Find1stRange.Find With Statement 

End Sub 

から検索及び選択機能を利用しました包括的)。 その部分をカットするだけです。 実際のMMLは残っています。これで、Selectionオブジェクトにフィードして(文書内の文字列を置き換えて)、書式設定なしでコピーして貼り付けることができます。

0

あなただけRegEx.Matchごとに2つの単語を削除したい場合は、次のように動作するはずです:

Sub TestMe() 

    Dim regex As Object, wholeDocText As String 
    Set regex = CreateObject("VBScript.RegExp") 
    Selection.WholeStory 
    Selection.Copy 
    wholeDocText = Selection.Text 

    With regex 
     .Pattern = "MATHSTART[.\s\S]*?MATHEND" 
     .Global = True 
    End With 

    Set matches = regex.Execute(wholeDocText) 

    For Each match In matches 
     s1 = Replace(match, "MATHSTART", "") 
     s1 = Replace(s1, "MATHEND", "") 
     match = s1 
    Next match 

End Sub 

は、私が最初にReplace()から.Valueを削除していると私はmatch = s1を追加しました。

関連する問題