The tech resource your business needs. Sign up today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14372
  • Last Modified:

Uploading to Google Drive using VBA or VB

Does anyone have an example of VBA or VB code for uploading files to Google Drive?   I've found examples in C# in the Google Drive SDK documentation, but I don't know how to translate them into VBA.
0
jbren_94517
Asked:
jbren_94517
2 Solutions
 
MacroShadowCommented:
I'm not aware of a way to upload to Google Drive w/o additional software. See this link for several applications that either can setup Google Drive as a mapped drive or a local folder. Using any of the suggested applications all you need in vba is to copy your file to the local folder and from there it gets uploaded automatically.
0
 
GrahamSkanRetiredCommented:
Why not post the C# example here? It shouldn't be too difficult to convert.
0
 
jbren_94517Author Commented:
GrahamSkan,

Here is the C# code provided by Google for uploading documents to Drive.  

This came from:  https://developers.google.com/drive/quickstart-cs

Any help you or anyone else can provide in converting this to vba would be very much appreciated.

using System;
using System.Diagnostics;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Util;
using Google.Apis.Services;

namespace GoogleDriveSamples
{
    Class DriveCommandLineSample
    {
        static void Main(string[] args)
        {
            String CLIENT_ID = "YOUR_CLIENT_ID";
            String CLIENT_SECRET = "YOUR_CLIENT_SECRET";

            // Register the authenticator and create the service
            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
            var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
            var service = new DriveService(new BaseClientService.Initializer()
            {
                Authenticator = auth
            });

            File body = new File();
            body.Title = "My document";
            body.Description = "A test document";
            body.MimeType = "text/plain";

            byte[] byteArray = System.IO.File.ReadAllBytes("document.txt");
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

            FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
            request.Upload();

            File file = request.ResponseBody;
            Console.WriteLine("File id: " + file.Id);
            Console.WriteLine("Press Enter to end this process.");
            Console.ReadLine();
        }

        private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
        {
            // Get the auth URL:
            IAuthorizationState state = new AuthorizationState(new[] { DriveService.Scopes.Drive.GetStringValue() });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
            Uri authUri = arg.RequestUserAuthorization(state);

            // Request authorization from the user (by opening a browser window):
            Process.Start(authUri.ToString());
            Console.Write("  Authorization Code: ");
            string authCode = Console.ReadLine();
            Console.WriteLine();

            // Retrieve the access token by using the authorization code:
            return arg.ProcessUserAuthorization(authCode, state);
        }
    }
}
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
MacroShadowCommented:
Imports System
Imports System.Diagnostics
Imports DotNetOpenAuth.OAuth2
Imports Google.Apis.Authentication.OAuth2
Imports Google.Apis.Authentication.OAuth2.DotNetOpenAuth
Imports Google.Apis.Drive.v2
Imports Google.Apis.Drive.v2.Data
Imports Google.Apis.Util
Imports Google.Apis.Services

Namespace GoogleDriveSamples
    
    Class DriveCommandLineSample
        
        Private Shared Sub Main(ByVal args() As String)
            Dim CLIENT_ID As String = "YOUR_CLIENT_ID"
            Dim CLIENT_SECRET As String = "YOUR_CLIENT_SECRET"
            ' Register the authenticator and create the service
            Dim provider As var = New NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET)
            Dim auth As var = New OAuth2Authenticator(Of NativeApplicationClient)(provider, GetAuthorization)
            Dim service As var = New DriveService(New BaseClientService.Initializer)
            Dim body As File = New File
            body.Title = "My document"
            body.Description = "A test document"
            body.MimeType = "text/plain"
            Dim byteArray() As Byte = System.IO.File.ReadAllBytes("document.txt")
            Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream(byteArray)
            Dim request As FilesResource.InsertMediaUpload = service.Files.Insert(body, stream, "text/plain")
            request.Upload
            Dim file As File = request.ResponseBody
            Console.WriteLine(("File id: " + file.Id))
            Console.WriteLine("Press Enter to end this process.")
            Console.ReadLine
        End Sub
        
        Private Shared Function GetAuthorization(ByVal arg As NativeApplicationClient) As IAuthorizationState
            ' Get the auth URL:
            Dim state As IAuthorizationState = New AuthorizationState(new, [)
            DriveService.Scopes.Drive.GetStringValue
            state.Callback = New Uri(NativeApplicationClient.OutOfBandCallbackUrl)
            Dim authUri As Uri = arg.RequestUserAuthorization(state)
            ' Request authorization from the user (by opening a browser window):
            Process.Start(authUri.ToString)
            Console.Write("  Authorization Code: ")
            Dim authCode As String = Console.ReadLine
            Console.WriteLine
            ' Retrieve the access token by using the authorization code:
            Return arg.ProcessUserAuthorization(authCode, state)
        End Function
    End Class
End Namespace

Open in new window

0
 
jbren_94517Author Commented:
Thanks MacroShadow, but much of this isn't compiling in vba.  I was able to fix some of it, but many lines I'm not sure how to translate.
0
 
MacroShadowCommented:
It's not VBA it's VB.Net.
0
 
jbren_94517Author Commented:
Sorry MacroShadow,

I'm not as familiar with VB.net as VBA, and will attempt to convert this code to VBA.  

Thanks for your help with this.
0
 
MacroShadowCommented:
Impossible.
0
 
MilosssssCommented:
Is possible with Internet Explorer library.
Create a macro in VBA to Open IE, open link with gmail... log in
Then wait for loading and using IE library find Google disk button. Enter and still the same using IE API commands add attachments and press upload.

You can contact me via milos.krizan@gmail.com if you are interested in.
0
 
jbren_94517Author Commented:
I'm going to give the Internet Explorer library solution a try.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now