给方程式特定的数字 [英] Giving equations specific numbers

查看:73
本文介绍了给方程式特定的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VBA的新手,我非常感谢您的帮助.首先,我应该提到我使用OFFICE 2016. 我使用宏录制"创建了一个宏,该宏可以复制所选方程式,然后插入一张表(一行,两列)并对其进行调整以删除边框.之后,宏将复制的方程式粘贴到第一个单元格中,然后移至下一个单元格,并插入一个空方程式(用户然后在其中插入所需数量的方程式). 它可以正常工作,除非到达粘贴粘贴的方程式的步骤.每次我运行宏都会得到

I'm new to VBA and I really would appreciate the help. First I should mention that I use OFFICE 2016. I used Macro recording to create a macro that copies the selected equation then it inserts a table (one row,two columns)and adjust it to remove borders. After that the macro pastes the copied equation in the first cell and moves to the next cell and insert an empty equation (which the user then inserts the desired number of the equation in it). It works fine except until it reaches the step where it should paste the copied equation. Every time I run the macro I get

运行时错误:6335

Run-time error: 6335

宏中断,并且当我调试它时,这是中断该过程的行:

and the macro breaks and when I debug it, this is the line that breaks the procedure:

Selection.PasteAndFormat (wdFormatOriginalFormatting)

在调试后,当我按下继续/运行"按钮时,它将按要求完成作业. 下面是我使用的宏.预先感谢.

after debugging when I hit the continue/run button, it completes the job as required. Below is the macro I use. Thanks in advance.

Sub MacroEQNUMBER()
'
' MacroEQNUMBER Macro
'
'
Dim rng As Range
If Selection.Range = "" Then
        Selection.HomeKey Unit:=wdLine
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    End If
Set rng = Selection.Range
rng.Cut
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
        2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
    With Selection.Tables(1)
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
        .Borders.Enable = False
    End With
    Selection.Tables(1).Cell(1, 1).Range.Select
    Selection.TypeText Text:="["
    Selection.OMaths.Add Range:=Selection.Range
    Selection.MoveRight Unit:=wdCharacter, Count:=2
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="]"
    Selection.MoveLeft Unit:=wdCharacter, Count:=2
    Selection.Tables(1).Columns(1).Cells.Width = 80
    Selection.Tables(1).Columns(2).Cells.Width = 350
    Selection.Tables(1).Cell(1, 2).Range.Select
    Selection.PasteAndFormat (wdFormatOriginalFormatting)
    Selection.TypeBackspace
    Selection.HomeKey Unit:=wdLine
    Selection.MoveLeft Unit:=wdCharacter, Count:=4

End Sub

推荐答案

我觉得问题出在粘贴方程式上.由于使用的是Selection,因此很有可能会将Selection放在表格单元格 structure 中. Word曾经允许这样做,这会破坏"文档.在最新版本中,完全不允许这样做,这将导致错误...

I get the feeling the problem comes from pasting the equation. Since you're using Selection it's quite possible you're putting the Selection in a table cell structure. Word used to allow this, which would "break" the document. In more recent versions it's simply not allowed, which will result in an error...

我没有尽力在您的记录的代码上放置创可贴,而是尽力解释了含义(感谢您的出色描述)并将其转换为目标代码". SELECTION对象不是理想的对象,因为您永远无法确定它的位置或含义.更好的方法是使用基础对象模型,通常是RANGE对象.

Rather than try to put bandaids on your recorded code, I've done my best to interpret what is meant (thank you for the excellent description) and convert it to "object code". The SELECTION object isn't ideal because you can never be sure where it is, nor what is meant. Better is to use the underlying object model, usually RANGE objects.

首先,将选择项分配给一个范围-现在,无论文档中可见选择项在何处,范围都将永远不会改变.

To begin, the Selection is assigned to a Range - now, no matter where the visible selection is in the document, the Range will never change.

接下来,我将新表分配给Table对象.由此,我可以获得第一个单元格的范围并使用它(插入文本).

Next, I assign the new table to a Table object. From that, I can get the Range of the first cell and work with it (insert text).

我显然确实需要一个用于插入OMath的Selection,所以我将其放置在方括号之间,选择并插入该对象.最后,对列进行格式化,粘贴cut公式,并将选择内容放置在第一个单元格的开头(我想这就是您想要的,但是我不确定这是最后一个).

I apparently do need a Selection for inserting the OMath, so I position between the square brackets, select and insert that. Finally, the columns are formatted, the cut equation pasted and the selection positioned at the beginning of the first cell (I think that's what you wanted, but I'm not sure about this last).

Sub MacroEQNUMBER()
'
' MacroEQNUMBER Macro
'
'
Dim rng As Range
Dim tbl As word.Table
Dim cel As word.Cell
Dim rngCell As word.Range

If Selection.Range = "" Then
        Selection.HomeKey Unit:=wdLine
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    End If
Set rng = Selection.Range
rng.Cut
    Set tbl = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=1, NumColumns:= _
        2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed)
    With tbl
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
        .Borders.Enable = False
    End With

    Set cel = tbl.Cell(1, 1)
    Set rngCell = cel.Range
    rngCell.Text = "[]"
    rngCell.Collapse wdCollapseStart
    rngCell.MoveStart wdCharacter, 1

    rngCell.Select
    rngCell.OMaths.Add Range:=rngCell

    tbl.Columns(1).Cells.width = 80
    tbl.Columns(2).Cells.width = 350

    Set rngCell = tbl.Cell(1, 2).Range
    rngCell.PasteAndFormat wdFormatOriginalFormatting
    rngCell.Characters(rngCell.Characters.Count - 1).Delete
    tbl.Cell(1, 1).Select
    Selection.Collapse wdCollapseStart
End Sub

这篇关于给方程式特定的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆