【问题标题】:Create an object in VB.Net to use in VBA在 VB.Net 中创建一个对象以在 VBA 中使用
【发布时间】:2019-06-10 11:05:15
【问题描述】:

首先,我对此很陌生,所以要温柔!

我正在尝试在 VB.net 中创建一个用于 vba 的类/对象。我在这篇文章的底部使用了 Gary Whitcher 的代码:

Sample vb.net code to upload file to Amazon S3 storage

我在 Visual Studio 中创建了一个类,并设法让它输出一个 TLB 文件,我可以将它导入 Excel 中的 VBA。

然后我可以使用 VBA 中的对象在 S3 存储系统中创建一个新文件夹,但是在使用“AddFileToFolder”方法时遇到了问题。

我不得不稍微编辑一下 Gary 的代码才能让它在 VS 中编译,编辑后的版本如下。

Imports Amazon.S3
Imports Amazon.S3.Model
Imports Amazon
Imports Amazon.S3.Util
Imports System.Collections.ObjectModel
Imports System.IO

Public Class aws_s3

Const AWS_ACCESS_KEY As String = "AccessKey" 'is set to MY actual key
Const AWS_SECRET_KEY As String = "SecretKey" 'is set to MY actual key


Private Property s3Client As IAmazonS3

Sub New()
    Try
        s3Client = New AmazonS3Client(AWS_ACCESS_KEY, AWS_SECRET_KEY, RegionEndpoint.USEast1)
    Catch ex As Exception

    End Try

End Sub


Public Function CreateFolder(bucketName As String, folderName() As String) As String
    Dim returnval As String = ""
    Try
        Try
            Dim folderKey As String = ""
            If Not AmazonS3Util.DoesS3BucketExist(s3Client, bucketName) Then
                returnval = "Bucket does not exist"
            Else
                For i = 0 To folderName.Length - 1
                    folderKey += folderName(i) & "/"

                Next
                ' folderKey = folderKey & "/"    'end the folder name with "/"
                Dim request As PutObjectRequest = New PutObjectRequest()
                request.BucketName = bucketName
                request.StorageClass = S3StorageClass.Standard
                request.ServerSideEncryptionMethod = ServerSideEncryptionMethod.None
                ' request.CannedACL = S3CannedACL.BucketOwnerFullControl
                request.Key = folderKey
                request.ContentBody = String.Empty
                s3Client.PutObject(request)
            End If
        Catch ex As Exception
            returnval = ex.Message
        End Try
    Catch ex As AmazonS3Exception
        returnval = ex.Message
    End Try
    Return returnval
End Function
Public Function AddFileToFolder(FileName As String, bucketName As String, folderName As String) As String
    Dim returnval As String = ""
    Try
        Try
            If Not AmazonS3Util.DoesS3BucketExist(s3Client, bucketName) Then
                Dim fname() As String = folderName.Split("/")
                CreateFolder(bucketName, fname)
            Else
                Dim path As String = FileName
                Dim file As FileInfo = New FileInfo(path)

                Dim key As String = String.Format("{0}/{1}", folderName, file.Name)
                Dim por As PutObjectRequest = New PutObjectRequest()
                por.BucketName = bucketName
                por.StorageClass = S3StorageClass.Standard
                por.ServerSideEncryptionMethod = ServerSideEncryptionMethod.None
                por.CannedACL = S3CannedACL.PublicRead
                por.Key = key
                por.InputStream = file.OpenRead()
                s3Client.PutObject(por)
            End If
        Catch ex As Exception
            returnval = ex.Message
        End Try
    Catch ex As AmazonS3Exception
        returnval = ex.Message
    End Try
    Return returnval & " dll"
End Function

结束类

使用 VBA,我创建了上述对象并可以成功执行 CreateFolder,但是在执行 addfiletofolder 时出现错误“类不支持自动化或不支持预期的接口”

VBA 代码如下所示:

Dim aws As AWS_S3
Dim Result As String
Dim UploadFile As String

UploadFile = "C:\Zipped Builds\Hinchley Legion.zip"

Set aws = New AWS_S3

Dim fld(1) As String
fld(0) = "folder"
fld(1) = "subfolder"

Result = aws.CreateFolder("nsmcustomercontent", fld)

If Result <> "" Then GoTo errHandle

Result = aws.AddFileToFolder(UploadFile, "nsmcustomercontent", fld)

If Result <> "" Then GoTo errHandle

Exit Sub

errHandle:
    MsgBox Result


End Sub

我猜测 CreateFolder 工作正常,但 AddFileToFolder 没有,在 VS 中创建的类中存在问题,缺少依赖关系或其他什么?

【问题讨论】:

  • 所以我设法缩小了问题的范围。仅当我传递作为数组的 fld 变量时才会引发错误。如果我将数组中的单个元素传递给它,则不会遇到问题,例如fld(0)
  • AddFileToFolder 函数不需要字符串数组。它需要一个可以包含/ 作为分隔符的字符串。因此,不要传递数组["folder", "subfolder"] ...尝试传递字符串"folder/subfolder"

标签: vba vb.net amazon-s3


【解决方案1】:

感谢 Anu6is,这确实是问题所在。该课程的作者写了以下让我失望的用法:

将文件添加到文件夹

            Dim fld(1) As String                 
            fld(0) = <foldername>
            fld(1) = <subfoldername>
            'List each sub folder as an element in array
            Dim rtrn As String = aws.AddFileToFolder(<local file name>,<bucketname>, fld)

我认为我需要更好地阅读 VB.Net!非常感谢您的快速回复,非常感谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多