提取嵌入对象以本地保存,无法获取OLEObject类的对象属性错误。

huangapple go评论60阅读模式
英文:

Extracting Embedded Objects to save locally, Unable to get Object property of OLEObject class Error

问题

我理解你需要对Excel 365中的VBA代码进行翻译,但不需要翻译代码本身。以下是代码中的注释和字符串部分的翻译:

'puts file list entries into array
'将文件列表中的条目放入数组中

'adds icons for attached files to dc worksheet
'将附加文件的图标添加到工作表

'this is a generic icon
'这是一个通用的图标

'Attached File & i + 1
'附加文件 & i + 1

'Unable to get the Object property of the OLEObject class
'无法获取OLEObject类的Object属性

'finds file objects and sets insert statement
'查找文件对象并设置插入语句

希望这些翻译对你有所帮助。如果需要更多协助,请随时提问。

英文:

I have a userform in excel 365 that can take file attachments. It embeds the selected files into a worksheet with the following code:

'puts file list entries into array
If DE_Form.AttachedFiles_List.ListCount > 0 Then
    For i = 0 To DE_Form.AttachedFiles_List.ListCount - 1
        ReDim Preserve FileNameArray(0 To i)
        FileNameArray(i) = DE_Form.AttachedFiles_List.List(i)
    Next i
End If

'adds icons for attached files to dc worksheet
For i = 0 To UBound(FileNameArray)
    'choose an icon based on filename extension
    'get all after last "." in filename
    FNExtension = Right(FileNameArray(i), Len(FileNameArray(i)) - _
                InStrRev(FileNameArray(i), "."))
 
    'select icon based on filename extension
    Select Case UCase(FNExtension)
    Case Is = "TXT"
      iconToUse = "C:\Windows\system32\packager.dll"

    Case Is = "XLS", "XLSM", "XLSX"
      iconToUse = "C:\Windows\Installer\{91140000-0011-0000-0000-0000000FF1CE}\xlicons.exe"

    Case Is = "PDF"
      iconToUse = "C:\Windows\Installer\{AC76BA86-1033-F400-7761-000000000004}\_PDFFile.ico"

    Case Else
      'this is a generic icon
      iconToUse = "C:\Windows\system32\packager.dll"
    End Select
    
    dc.Cells(GetLastRow(dc), 32 + i).ColumnWidth = 10
    dc.Cells(5, 32 + i).value = "Attached File " & i + 1
    dc.Cells(5, 32 + i).WrapText = True
    
    dc.Cells(GetLastRow(dc), 32 + i).value = FileNameArray(i)
    
    Set j = dc.OLEObjects.Add(fileName:=FileNameArray(i), _
                                link:=False, _
                                DisplayAsIcon:=True, _
                                IconFileName:=iconToUse, _
                                IconIndex:=0, _
                                IconLabel:=FileNameArray(i))
    On Error Resume Next
    
    With j
        '.ShapeRange.LockAspectRatio = msoFalse
        .Top = dc.Cells(GetLastRow(dc), 32 + i).Top
        .Left = dc.Cells(GetLastRow(dc), 32 + i).Left
        .Width = dc.Cells(GetLastRow(dc), 32 + i).Width
        .Height = dc.Cells(GetLastRow(dc), 32 + i).Height
    End With
    
Next i

I am trying to pull out the embedded files and save them to disk. I am also making an entry in an access database for each file. I can't save the files. I can open them (activate and I have also used the .verb xlprimary to open) but not save. I'd like to not use a bunch of SendKeys statements. I have tried using the indexes as references (this code does not), I'm activating the object first. I'm doing or have tried everything I can find on google. I keep getting an error. 'Unable to get the Object property of the OLEObject class'. I appreciate any help/advise. Thank you!

Dim NewCon As DAO.Database
Dim rs As DAO.Recordset2, rsattach As DAO.Recordset2
Dim fn() As String, fpath As String
Dim nfle As Object

'finds file objects and sets insert statement
For i = 6 To GetLastRow(ldc)
    For j = 32 To ldc.Cells(i, Columns.Count).End(xlToLeft).Column
        For Each k In ldc.OLEObjects
            k.Activate
                
            Select Case k.name
            Case Right(k.name, 6) = "Button", Right(k.name, 4) = "Icon":
            
            Case Else:
            
            If k.Left = ldc.Cells(i, j).Left Then
            
            Set NewCon = OpenDatabase("\\MyAccessDB.accdb")
            Set rs = NewCon.OpenRecordset("AttachedFiles", dbOpenTable)
            
            rs.AddNew
            
            rs.Fields("ProgramID") = ldc.Cells(i, "a").value
            fn = Split(ldc.Cells(i, j).Text, "\")
            rs.Fields("filename") = fn(UBound(fn))
            k.name = fn(UBound(fn))
            
            fpath = "\\MyFolderLocation\" & _
                k.name
            
            k.Object.SaveAs fpath                'Error is here
            
            rs.Fields("FileLocation") = fpath
            rs.Fields("Modified On") = ldc.Cells(i, "ae").value
            
            End If
            End Select
        Next k
    Next j
Next i

答案1

得分: 1

这当然不是对问题的完整回答,但对于评论来说太长了,我认为可能会有所帮助... 请用以下代码替换您的这一行 k.Object.SaveAs fpath

Dim shp As Shape
Set shp = k.ShapeRange(1)
If shp.Type = msoEmbeddedOLEObject Then
    Debug.Print shp.OLEFormat.progID ' 有关信息
    If shp.OLEFormat.progID Like "Excel*" Then
        shp.OLEFormat.Verb XlOLEVerb.xlVerbOpen
        Dim wkb As Workbook
        Set wkb = ActiveWorkbook
        wkb.SaveAs "C:\test\" & shp.Name & wkb.FileFormat ' 更改为您的文件夹
        wkb.Close
    ElseIf shp.OLEFormat.progID Like "Acrobat*" Then
        ' 处理Acrobat的操作... 我没有Acrobat库
    ElseIf shp.OLEFormat.progID Like "Package*" Then
        shp.OLEFormat.Verb XlOLEVerb.xlVerbPrimary
        ' 使用SendKeys或Windows API进行Notepad操作?
    Else
        Debug.Print "未知的ProgID '" & shp.OLEFormat.progID & "'"
    End If
Else
    Debug.Print shp.Name & " 不是嵌入的OLE对象"
End If

需要 Acrobat 和 Notepad 的代码,如评论所述,但可以正确保存Excel文件。绝对需要错误处理!

英文:

This is certainly not a full answer to the question, but far too long for a comment and I thought may help ... replace your line k.Object.SaveAs fpath with

Dim shp As Shape
Set shp = k.ShapeRange(1)
If shp.Type = msoEmbeddedOLEObject Then
    Debug.Print shp.OLEFormat.progID ' for info
    If shp.OLEFormat.progID Like "Excel*" Then
        shp.OLEFormat.Verb XlOLEVerb.xlVerbOpen
        Dim wkb As Workbook
        Set wkb = ActiveWorkbook
        wkb.SaveAs "C:\test\" & shp.Name & wkb.FileFormat ' change to your folder
        wkb.Close
    ElseIf shp.OLEFormat.progID Like "Acrobat*" Then
        ' do Acrobat stuff ... I don't have the Acrobat library
    ElseIf shp.OLEFormat.progID Like "Package*" Then
        shp.OLEFormat.Verb XlOLEVerb.xlVerbPrimary
        ' do Notepad stuff with SendKeys or Windows API?
    Else
        Debug.Print "Unknown ProgID '" & shp.OLEFormat.progID & "'"
    End If
Else
    Debug.Print shp.Name & " is not an embedded OLEObject"
End If

Needs code for Acrobat and Notepad, as per comments, but saves Excel files correctly. Definitely needs error handling!

答案2

得分: 0

这是我最终得到的代码。尚未完全完成,但大家可以理解思路。再次感谢 @JohnM。

'查找文件对象并设置插入语句
For Each k In ldc.OLEObjects
    '保存文件到共享驱动器
    k.Activate
    Set shp = k.ShapeRange(1)
    
    If shp.Type = msoEmbeddedOLEObject Then
    
        '获取对象地址的行和列
        adrs = Split(shp.TopLeftCell.Address, "$")
        j = ldc.Range(adrs(1) & "1").Column
        i = adrs(2)
        
        Set NewCon = OpenDatabase("\\Server\Access.accdb")
        Set rs = NewCon.OpenRecordset("AttachedFiles", dbOpenTable)
        
        rs.AddNew
        
        rs.Fields("ID") = ldc.Cells(i, "a").Value
        fn = Split(ldc.Cells(i, j).Text, "\")
        rs.Fields("filename") = fn(UBound(fn))
        
        fpath = "\\Server\MyFolder\"
        
        rs.Fields("FileLocation") = fpath
        rs.Fields("Modified On") = ldc.Cells(i, "ae").Value
        
        '对象的新路径
        ldc.Cells(i, j).Value = fpath & k.Name
        
        '保存文件到共享驱动器
        
        Debug.Print shp.OLEFormat.progID '用于信息
                
        Select Case True
                
        Case shp.OLEFormat.progID Like "Excel*":
            shp.OLEFormat.Verb XlOLEVerb.xlVerbOpen
                        
            Set wkb = GetObject("Worksheet in " & ThisWorkbook.Name, "Excel.Application")
            wkb.SaveAs FileName:=fpath & shp.Name
            wkb.Close
                    
        Case shp.OLEFormat.progID Like "Acrobat*":
            ChDir ("\\Server\Myfolder\")
                        
            shp.OLEFormat.Verb XlOLEVerb.xlVerbPrimary
            
            Set wrd = GetObject(, "AcroExch.App")
            
            'SendKeys方法:
            wrd.Application.SendKeys "+^(s)", True '保存
            'Application.Wait Now + TimeValue("0:00:2")
                    
            wrd.Application.SendKeys shp.Name, True '尝试发送新文件名但不成功
            'Application.Wait Now + TimeValue("0:00:2")
                    
            wrd.Application.SendKeys "~", True
            'Application.Wait Now + TimeValue("0:00:2")
                    
            wrd.Application.SendKeys "^(q)", True '关闭阅读器
            'Application.Wait Now + TimeValue("0:00:2")
                
        Case shp.OLEFormat.progID Like "Word*", shp.OLEFormat.progID Like "textfile*":
            shp.OLEFormat.Verb XlOLEVerb.xlVerbOpen
                        
            Set wrd = GetObject(, "Word.Application")
                        
            Set wrdfle = wrd.Documents("Document in " & ThisWorkbook.Name)
                        
            wrdfle.SaveAs fpath & shp.Name
            wrd.Application.Quit
                    
        Case shp.OLEFormat.progID Like "PowerPoint*":
            shp.OLEFormat.Verb XlOLEVerb.xlVerbOpen
                        
            Set wrd = GetObject(, "PowerPoint.Application")
            Set wrdfle = wrd.ActivePresentation
                        
            wrdfle.SaveAs fpath & shp.Name
            wrdfle.Close
                
        Case shp.OLEFormat.progID Like "Package*":
            shp.OLEFormat.Verb XlOLEVerb.xlVerbPrimary
            ' 使用SendKeys或Windows API执行Notepad操作?
        Case Else:
            Debug.Print "未知的ProgID '" & shp.OLEFormat.progID & "'"
        End Select
    NewCon.Close
    End If
Next k
英文:

Here's the final code I wound up with. Not totally finished but y'all get the idea. Thanks again @JohnM

'finds file objects and sets insert statement
For Each k In ldc.OLEObjects
     'saves file to sharedrive
    k.Activate
    Set shp = k.ShapeRange(1)
            
    If shp.Type = msoEmbeddedOLEObject Then
    
        'gets row and column of the object's address
        adrs = Split(shp.TopLeftCell.Address, "$")
        j = ldc.Range(adrs(1) & "1").Column
        i = adrs(2)
            
        Set NewCon = OpenDatabase("\\Server\Access.accdb")
        Set rs = NewCon.OpenRecordset("AttachedFiles", dbOpenTable)
            
        rs.AddNew
            
        rs.Fields("ID") = ldc.Cells(i, "a").value
        fn = Split(ldc.Cells(i, j).Text, "\")
        rs.Fields("filename") = fn(UBound(fn))
            
        fpath = "\\Server\MyFolder\"
            
        rs.Fields("FileLocation") = fpath
        rs.Fields("Modified On") = ldc.Cells(i, "ae").value
        
        'new path for object
        ldc.Cells(i, j).value = fpath & k.name
            
        'saves file to sharedrive
        
        Debug.Print shp.OLEFormat.progID ' for info
                
        Select Case True
                
        Case shp.OLEFormat.progID Like "Excel*":
            shp.OLEFormat.Verb XlOLEVerb.xlVerbOpen
                        
            Set wkb = GetObject("Worksheet in " & ThisWorkbook.name, "Excel.Application")
            wkb.SaveAs fileName:=fpath & shp.name
            wkb.Close
                    
        Case shp.OLEFormat.progID Like "Acrobat*":
            ChDir ("\\Server\Myfolder\")
                        
            shp.OLEFormat.Verb XlOLEVerb.xlVerbPrimary
            
            Set wrd = GetObject(, "AcroExch.App")
            
            'Send Keys method:
            wrd.Application.SendKeys "+^(s)", True 'Saves
            'Application.Wait Now + TimeValue("0:00:2")
                    
            wrd.Application.SendKeys shp.name, True 'Attempts to send new file name but doesn't.
            'Application.Wait Now + TimeValue("0:00:2")
                    
            wrd.Application.SendKeys "~", True
            'Application.Wait Now + TimeValue("0:00:2")
                    
            wrd.Application.SendKeys "^(q)", True 'closes the reader.
            'Application.Wait Now + TimeValue("0:00:2")
                
        Case shp.OLEFormat.progID Like "Word*", shp.OLEFormat.progID Like "textfile*":
            shp.OLEFormat.Verb XlOLEVerb.xlVerbOpen
                        
            Set wrd = GetObject(, "Word.Application")
                        
            Set wrdfle = wrd.Documents("Document in " & ThisWorkbook.name)
                        
            wrdfle.SaveAs fpath & shp.name
            wrd.Application.Quit
                    
        Case shp.OLEFormat.progID Like "PowerPoint*":
            shp.OLEFormat.Verb XlOLEVerb.xlVerbOpen
                        
            Set wrd = GetObject(, "PowerPoint.Application")
                    Set wrdfle = wrd.activepresentation
                        
            wrdfle.SaveAs fpath & shp.name
            wrdfle.Close
                
        Case shp.OLEFormat.progID Like "Package*":
            shp.OLEFormat.Verb XlOLEVerb.xlVerbPrimary
            ' do Notepad stuff with SendKeys or Windows API?
        Case Else:
            Debug.Print "Unknown ProgID '" & shp.OLEFormat.progID & "'"
        End Select
    NewCon.Close
    End If
Next k

huangapple
  • 本文由 发表于 2023年6月27日 21:35:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定