命令按钮正在重命名

时间:2015-01-16 14:18:42

标签: vba ms-word activex

我遇到问题,在Word 2007中重命名的活动x控制命令按钮没有用户实际重命名它们。我直接观察到用户使用嵌入式活动x控件保存文档,并且在打开文档时名称似乎没问题,但是当他们保存文档时,它们会被重命名。

例如,CommandButton11的name属性将重命名为CommandButton111。在某些情况下,似乎1被添加到命令按钮名称的末尾,因此10变为101,而在其他情况下,1被添加到命令按钮的实际值,所以说CommandButton10变为CommandButton11。命令按钮的代码不会改变,但因为我引用了代码中各个命令按钮的名称,它显然会中断。

代码的目的是在文档中嵌入一个OLE对象并将其正确放置在表中。

以下是命令按钮的具体代码:

Private Sub CommandButton10_Click()

wrdTbl = 1
wrdRow = 11
wrdCol = 2
Set obj = CommandButton10

Call buttontransformer

End Sub

按钮变换器如下:

Private Sub buttontransformer()
    If ActiveDocument.Tables(wrdTbl).Cell(wrdRow, wrdCol).Range.Text = Chr(13) & Chr(7) Then
    obj.Caption = "Remove File"
        Call OLEObjectAdd
Else
    ActiveDocument.Tables(wrdTbl).Cell(wrdRow, wrdCol).Select
    Selection.EndKey unit:=wdRow, Extend:=wdExtend
    Selection.Delete
obj.Caption = "Click to Add File"
ireply = MsgBox("Add another file?", buttons:=vbYesNo, Title:="UPLOAD NEW FILE?")
    If ireply = vbYes Then
        obj.Caption = "Remove File"
        Call OLEObjectAdd
    Else
        Exit Sub
    End If
End If
End Sub

OleObjectAdd如下:

Private Sub OLEObjectAdd()

Dim fd As FileDialog
Dim ofd As Variant
Dim FP As String
Dim FN As String
Dim Ext As String
Dim fType As String

'Selection.MoveRight Unit:=wdCharacter, Count:=1

Set fd = Application.FileDialog(msoFileDialogFilePicker)
ActiveDocument.Tables(wrdTbl).Cell(wrdRow, wrdCol + 1).Select

With fd
.ButtonName = "Select"
.AllowMultiSelect = False
.Filters.Clear
If .Show = -1 Then
    For Each ofd In .SelectedItems
    FP = ofd
    Debug.Print FP
    FN = Right(FP, Len(FP) - InStrRev(FP, "\"))
    Debug.Print FN
    Ext = Right(FP, Len(FP) - InStrRev(FP, "."))
    Debug.Print Ext
    Next ofd
    On Error GoTo 0
    Else
        Exit Sub
    End If
End With

If Ext = "pdf" Then
    fType = "adobe.exe"
ElseIf Ext = "doc" Or Ext = "docx" Or Ext = "docm" Then
    fType = "word.exe"
ElseIf Ext = "xls" Or Ext = "xlsx" Or Ext = "xlsm" Then
    fType = "Excel.exe"
End If

Selection.InlineShapes.AddOLEObject ClassType:=fType, _
    fileName:=FP, LinkToFile:=False, _
    DisplayAsIcon:=True, IconFileName:= _
    fType, IconIndex:=0, IconLabel:= _
    FN

Selection.Move unit:=wdCell, Count:=-2
Selection = FN


End Sub

我已经完成了Microsoft Fixit来解决Active-X损坏的控件,它在我测试过的其他几台计算机上运行良好。

我搜索了高低的答案,似乎无法找到答案。任何帮助将不胜感激。

0 个答案:

没有答案