0

I wrote a C# class which connects to Dropbox and lets you upload, download, delete and generate link files.

It's working with a Windows Forms but I have to access it from VBA (Microsoft Access). The problem comes when it goes to task.Wait(). I've ""debugged" this throwing Exceptions and after that, doesn't go through.

public DropBox()
{
    //Empty constructor because VBA doesn't support constructors with args
}

public void Connect(string tokenUser)
{
    try
    {
        dropbox = new DropboxClient(tokenUser);
        var taskInicio = Task.Run(async () => await dropbox.Users.GetCurrentAccountAsync());
        //throw new Exception("Arriving?");   //ARRIVES
        taskInicio.Wait();
        throw new Exception("Arriving?");    //Throws "one or more errors"
    }
    catch (AggregateException ex)
    when (ex.InnerException is BadInputException
          || ex.InnerException is AuthException)
    {
        throw new Exception("Incorrect Token or without access", ex.InnerException);
    }
}

On VBA

Option Compare Database

Private Sub btActivar_Click()
    Call test
End Sub

Public Function test()

    Dim objDrop As CloudFiles.DropBox
    Set objDrop = New CloudFiles.DropBox

    MsgBox (objDrop.HolaMundo)
    objDrop.Connect("TokenLongChicken")
    'objDrop.DeleteFile("https://www.dropbox.com/s...?dl=0")


End Function

The "One or more errors produced" sounds like it comes from the "mscorlib" or so...

Any ideas? This is getting quite messy :/

Thanks.

Erik A
  • 31,639
  • 12
  • 42
  • 67
Gonzo345
  • 1,133
  • 3
  • 20
  • 42
  • 3
    I stand to be corrected, but if I am not wrong the last update to the core features of VBA happened in 2003. It is highly improbable that these kind of features will be ever added to the language. – Steve Feb 14 '17 at 13:04
  • @Steve - and this is what MS has built in 2003 - https://msdn.microsoft.com/en-us/library/aa213656(office.11).aspx – Vityata Feb 14 '17 at 13:36
  • Can you change the connect method to return a Boolean, then in the VBA do until = true, and if once task completed return true from the C#? – Nathan_Sav Feb 14 '17 at 13:42
  • "One or more errors produced" is an AggregateException. So this didn't go wrong as you expected, it was the task that threw. And the InnerException did not match the *when* clause, it never will since it uses the InnerExceptions property. Note the s. Consider Flatten() and rethrowing InnerExceptions[0]. – Hans Passant Feb 14 '17 at 14:51

2 Answers2

0

VBA does have Application.Wait, I think you can give it a try. I have a code to wait for an IE connection which you can use as example:

Do While IE.Busy ' Need to wait until the page has loaded
  Application.Wait (Now + TimeValue("00:00:01")) ' Wait one second
Loop

Tell me if it helps you.

PedroMVM
  • 336
  • 1
  • 5
  • 13
  • Pedro, you could check the document.readystate also in the IE example, and try doevents rather than wait. – Nathan_Sav Feb 14 '17 at 13:39
  • Yes, I know it. But in my case I have a loop which loads several pages to webscrap and sometimes the browser hangs, even doing `IE.Quit`and `Set IE = Nothing` every loop. That's why I have this check for `IE.Busy`. – PedroMVM Feb 14 '17 at 13:46
0

I was having headaches thinking that it could be something related with the Tasks, asyncs, awaits... and it was something related with the Newtonsoft json library.

I updated the library through the NuGet to the 9.0 version and everything worked fine on Windows Forms, but looks like something is wrong when I use it through the .TLB because when I escaped the exception I got at some point in my deletion method, it said the Newtonsoft Json 7.0.0.0 library was missing (it was at the same directory anyway).

I finally removed that Newtonsoft Json version I was using and the Dropbox API, downloaded box again but I declined applying any updates. I couldn't even try to apply a downgrade or so.

Good ending, but I don't really get why did it search for the 7.0.0.0 when I was using the 9.x on my Windows Form project, which works and exported the .DLL and the .TLB.

Thanks to everybody.

EDIT: And yes, I guess this answers the question: VBA supports Task.Wait (at least coming from a C# .dll import)

Gonzo345
  • 1,133
  • 3
  • 20
  • 42
  • Well, that wasn't the problem, stuff happens. Your exception handling is broken pretty badly, you'd better fix it because this will happen again. If you don't then you'll yet again have no idea what went wrong. – Hans Passant Feb 14 '17 at 22:42
  • Thanks for the comment. I made event custom Exceptions on my project, but I saw that "one or more errors happened" before doing that (at the beginning of the project). It's (not) funny how you could debug an external library on VBA :( – Gonzo345 Feb 14 '17 at 23:03