English 中文(简体)
在Word VBA中,InlineShape物体的删除方法为什么不可靠地发挥作用?
原标题:In Word VBA, why does the InlineShape object s Delete method not work reliably?
  • 时间:2023-06-08 02:52:23
  •  标签:
  • vba
  • ms-word

我多年来一直存在的一个问题是,在拟定法律时,删除因诺言文件采集的InlineShape物体,同时测试代谢法,即每一英明沙伯物体的删除方法,这种方法并不可靠地删除每一英明沙佩物体。 如果我通过该守则单行删除其中任何一个;如果我操作整个删除(从日内瓦考试委员会窗口内部),有时会删除某些物体,但通常在收集中留下至少一个物体。 当该文件是活跃的窗口(而不是VBE)并且该代码通过用户互动(例如由Macros menu操作)来操作时,它有时会正确地执行,删除所有这类InlineShape物体,但有时却留下其中的 in。

例如,这里有一些显示问题的法典。

设置:

  1. 开放一份新文件。

  2. 在该文件中添加3个主动X突击队物体。

  3. 撤销该文件,并提供一份原件(加上“积极X”控制法)。

  4. 在标准模块中添加以下代码:

    Sub Demo()
        Dim i As Integer
    
        With ThisDocument
            Debug.Print "Before:"
            For i = .InlineShapes.Count To 1 Step -1
                Debug.Print "    " & .InlineShapes(i).OLEFormat.Object.Name  To the Immediate window
            Next i
    
            For i = .InlineShapes.Count To 1 Step -1
                Debug.Print "Deleting " & .InlineShapes(i).OLEFormat.Object.Name
                .InlineShapes(i).Delete
            Next i
    
            Debug.Print "After:"
            For i = .InlineShapes.Count To 1 Step -1
                Debug.Print "    " & .InlineShapes(i).OLEFormat.Object.Name
            Next i
        End With
    End Sub
    

测试-1:在职业考试和测验科窗口中,单步穿透镜。 我的结果是,没有InlineShape物体(活性X控制)被实际删除。

测试-2:在职业考试和测验中心窗口中,广播例行直截了当,没有单步。 我的结果是,第一批2件InlineShape物体被删除,但第三个物体仍被删除。

测试-3:

A. 导 言 接近经过更新的试验文件。

B. 出席情况 将贵试卷(所有三个主动X控制都未变)的节省版重回原版。

C. 出席情况 开放原版本,并在文字<>上开放 tab, 选择Macros=>View Macros。 每天从那里开播<<>Demo。 我由此得出的结果是,所有3个InlineShape物体均被删除。 但是,在《生产法典》中,我所看到的情况是,没有删除,也没有删除除一个之外的一切。 我没有把什么情况产生不同的结果。

是否有任何人知道这实际上是一个ug子,还是我刚刚很密集,以致有某种明显的技术,能够在所有情况下可靠地执行InlineShapes删除方法?

问题回答

不管怎样,从历史上看,我先去做以下工作:正常运转,同时通过守则进行单步工作,如果其他人认为这样做是有用的。

有三个工作领域(我知道):

  1. 第一版 Shape对Shape物体提出反对(称其“Convert ToShape”方法),然后通过Shapes收集予以删除。 由于第二批收款周期过于复杂和间接费用,这项工作并不可取。 (说明沙皮环必须检查沙皮。) 混合体(msoOLEControlObject)的类型财产,而不是重整的InlineShapeOLEControlObject。

  2. 选择网上 Shape 反对的范围,并通过甄选标的删除方法予以删除。 由于节省目前的甄选费用、选择每个因提纲沙皮删除、然后在作出选择时恢复原选,因此这项工作并不可取。

  3. 简单地取代英属地 Shape物体的尺寸,其体状无效。 由于这项工作很简单,因此更为可取。

     Sub DeleteAllActiveXControls(wdDoc As Word.Document)
          
          Deletes all ActiveX controls in the specified document.
          
          *************************************************************************************************************
    
         Dim i As Integer
    
          *************************************************************************************************************
          Word InLineShape Delete method BUG WORKAROUND --- WORKAROUND --- WORKAROUND --- WORKAROUND --- WORKAROUND
          
          Word has a bizarre bug in which a call to an InLineShape object s Delete method doesn t reliably delete the
          shape.  Testing has demonstrated that the problem always manifests while single-stepping through the deletion
          code.  When the code is executed normally by way of some user interaction with the document, some of the
          InLineShape objects do get deleted, but not all of them get reliably deleted (under circumstances that I have
          yet to determine).
          
          I think this is clearly a bug because the Delete method does not *reliably* function as advertised by
          Microsoft s reference documentation: https://learn.microsoft.com/en-us/office/vba/api/word.inlineshape.delete
          
          The simplest workaround that I ve found is to just replace the InLineShape object s range content with a null
          string.
          
         With wdDoc
         For i = .InlineShapes.Count To 1 Step -1
             If .InlineShapes(i).Type = wdInlineShapeOLEControlObject Then  (ActiveX control)
                 .InlineShapes(i).Range = ""  Delete the InlineShape object s current range content by replacement,
             End If                           which automatically deletes the object from the InlineShapes collection.
         Next i
         End With
          
          END WORKAROUND --- END WORKAROUND --- END WORKAROUND --- END WORKAROUND --- END WORKAROUND --- END WORKAROUND
          *************************************************************************************************************
     End Sub
    




相关问题
Handling no results for docmd.applyfilter

I have an Access app where I use search functionality. I have a TextBox and a Search Button on the form, and it does a wildcard search of whatever the user enters in the TextBox, and displays the ...

Outlook 2007 CommandBarControl.Execute won t work

I recently switched to Outlook 2007 and noticed that my VBA-macros won t work. I use the following code to open a new appointment-item (and fill it automatically). It worked perfect in Outlook 2003, ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

MS Access: list macro from VBA

I have to deal with a few macros (not VBA) in an inherited Access application. In order to document them, I would like to print or list the actions in those macros, but I am very dissatisfied by ...

热门标签