Converting VB.Net to VBA

N

Neil

Anyone familiar with VB.Net? I have some code I need to convert from
VB.net to VB or VBA (I will be using it in Access 2010). The code is to
upload a document to a SharePoint library (using the SharePoint
Foundation 2010 Managed COM). The original code was in C#. I used a C#
to VB.Net converter to get it into VB.Net. Now I need to get it into VB
or VBA. Is it possible?

Here is the original C# code (from
http://msdn.microsoft.com/en-us/library/ee956524.aspx):

using System;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.SharePoint.Client;
// The following directive avoids ambiguity between the
// System.IO.File class and Microsoft.SharePoint.Client.File class.
using ClientOM = Microsoft.SharePoint.Client;
class Program
{
static void Main(string[] args)
{
ClientContext clientContext =
new ClientContext("http://intranet.contoso.com");
using (FileStream fileStream =
new FileStream("NewDocument.docx", FileMode.Open))
ClientOM.File.SaveBinaryDirect(clientContext,
"/Shared Documents/NewDocument.docx", fileStream, true);
}
}

And here is the code after it was converted to VB.Net:

Imports System.IO
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing
Imports Microsoft.SharePoint.Client
' The following directive avoids ambiguity between the
' System.IO.File class and Microsoft.SharePoint.Client.File class.
Imports ClientOM = Microsoft.SharePoint.Client
Class Program
Private Shared Sub Main(args As String())
Dim clientContext As New ClientContext("http://intranet.contoso.com")
Using fileStream As New FileStream("NewDocument.docx", FileMode.Open)
ClientOM.File.SaveBinaryDirect(clientContext, "/Shared
Documents/NewDocument.docx", fileStream, True)
End Using
End Sub
End Class

Any idea how this would look in VB or VBA?

Thanks!

Neil
 
N

Neil

Never mind re. this. Found another way to do it without having to use
this code. Thanks!
 
G

Gene Wirchenko

Never mind re. this. Found another way to do it without having to use
this code. Thanks!

It is a good idea to post your solution.

[snip]

Sincerely,

Gene Wirchenko
 
N

Neil

Never mind re. this. Found another way to do it without having to use
this code. Thanks!

It is a good idea to post your solution.

[snip]

Sincerely,

Gene Wirchenko

The solution I found was a completely different approach that didn't
require conversion from .Net, was a pure VBA approach for uploading a
file to a SharePoint library. Since this isn't a SharePoint forum, I
didn't think it would be of interest here (though I realize SharePoint
is discussed here a little in conjunction with Access, so maybe it is).
So, sure, I'd be happy to post.

The solution I found is actually quite simple. SharePoint libraries,
apparently, allow themselves to be accessed through Windows Explorer and
accessed like a regular directory without the need for FTP.

Thus, my solution was to simply map a drive letter to the SharePoint
library URL. And, yes, believe it or not, it worked!

Now, a caveat. There are certain settings that have to be in place in
order for Explorer to access a SharePoint library. Specifically,
WebClient has to be a running service, and https://*.sharepoint.com has
to be listed as a local intranet site. The steps to do all that are very
well explained in this article:

http://blogs.technet.com/b/asiasupp...oint-document-library-in-office-365-site.aspx

If those items are in place, then the code should work.

When mapping a SharePoint drive, Windows doesn't let you map the drive
if the site is already in use as a network location. Thus, I added a
line of code to remove any network use of the site before mapping it.

My code is below. Once the library is mapped to a drive, it can be used
just like any other Windows location, with FileCopy, FileSystemObject,
Dir(), etc. It's a very simple and powerful solution for us Access
programmers.

The below code uses the format (e-mail address removed) because I'm
using it in Office 365. But, of course, any username format would be
fine, if a non-Office 365 account is used.

And the library URL is in the format

http://DOMAIN.sharepoint.com/SUBSITE/LIBRARY

But, of course, a subsite isn't required (or there could be multiple
subsite levels). Basically, whatever the URL to the library is, that
should be used.

All words in ALL CAPS should be replaced with actual terms, of course.

Neil


Sub MapSharePointLibrary()

Dim strLibURL As String
Dim strUser As String
Dim strPW As String
Dim strDrive As String
Dim strDisplay As String

strLibURL = "http://DOMAIN.sharepoint.com/SUBSITE/LIBRARY"
strUser = "(e-mail address removed)"
strPW = "PASSWORD"
strDrive = "L" 'drive letter to map to
strDisplay = "c" 'use c to not display results, k to display
results

Shell "cmd.exe /" & strDisplay & " net use /delete " & strLibURL
Shell "cmd.exe /" & strDisplay & " net use " & strDrive & ": " &
strLibURL & " " & strPW & " /user:" & strUser

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top