Remote Printing with Dropbox - Visual Basic Script

Published in: Print - Dropbox

With the help of Dropbox, you can easily print files from mobile phones. This is the VBS code that makes it happen - it watches your Print Queue folder in Dropbox and sends it to the default printer as soon as it discovers a new file in that watched folder.

'Remote Printing through Dropbox
'Written by Amit Agarwal

Option Explicit
On Error Resume Next

Const WAIT_TIME  = 5000 '5 seconds
Const PRINT_TIME = 5000 '5 seconds

Dim WshShell, fso, configFile, objReadFile, str64, strPath, ApplicationData
Dim dbWatchDir, attFolder, objShell, objFolder, colItems, objItem, dbLogDir, logFolder, doneFolder

Set WshShell = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
ApplicationData = WshShell.ExpandEnvironmentStrings("%APPDATA%")

'Find the Dropbox Folder Location
configFile = ApplicationData & "\\Dropbox\\host.db"

If fso.FileExists( configFile ) Then
 Set objReadFile = fso.OpenTextFile( configFile, 1)
 Do Until objReadFile.AtEndOfStream
  str64 = objReadFile.ReadLine
strPath = Base64Decode(str64)
'WScript.Echo "Your Dropbox folder is located at " & strPath
WScript.Echo "Looks like Dropbox is not installed on this computer." &  VbCrLf & "Please install Dropbox and run this script again."
End If

dbWatchDir = strPath & "\\Attachments"

If Not fso.FolderExists (dbWatchDir) Then
 Set attFolder = fso.CreateFolder (dbWatchDir)
 WScript.Echo "Created a folder to hold your new print jobs - " & dbWatchDir
End If

dbLogDir = dbWatchDir & "\\Print_Log"

If Not fso.FolderExists (dbLogDir) Then
 Set logFolder = fso.CreateFolder (dbLogDir)
 WScript.Echo "Created a folder to hold processed jobs - " & dbLogDir
End If

Do While True

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(dbWatchDir)
Set colItems = objFolder.Items
doneFolder = dbLogDir & "\" & DateDiff("s", "1/1/2010", Now)

For Each objItem in colItems
 If Not objItem.IsFolder Then
  If Not fso.FolderExists (doneFolder) Then
   Set logFolder = fso.CreateFolder (doneFolder)
   WScript.Echo "Created a folder to save processed jobs - " & doneFolder
  End If
  WScript.Echo "Now printing: " & objItem.Name
  fso.MoveFile dbWatchDir & "\" & objItem.Name & "*", doneFolder
 end if
Set objShell = nothing
Set objFolder = nothing
Set colItems = nothing

' Decodes a base-64 encoded string (BSTR type).
' 1999 - 2004 Antonin Foller,
Function Base64Decode(ByVal base64String)
  Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  Dim dataLength, sOut, groupBegin

  base64String = Replace(base64String, vbCrLf, "")
  base64String = Replace(base64String, vbTab, "")
  base64String = Replace(base64String, " ", "")

  dataLength = Len(base64String)
  If dataLength Mod 4 <> 0 Then
    Err.Raise 1, "Base64Decode", "Bad Base64 string."
    Exit Function
  End If

  For groupBegin = 1 To dataLength Step 4
    Dim numDataBytes, CharCounter, thisChar, thisData, nGroup, pOut
    numDataBytes = 3
    nGroup = 0

    For CharCounter = 0 To 3
      thisChar = Mid(base64String, groupBegin + CharCounter, 1)

      If thisChar = "=" Then
        numDataBytes = numDataBytes - 1
        thisData = 0
        thisData = InStr(1, Base64, thisChar, vbBinaryCompare) - 1
      End If
      If thisData = -1 Then
        Err.Raise 2, "Base64Decode", "Bad character In Base64 string."
        Exit Function
      End If

      nGroup = 64 \* nGroup + thisData

    nGroup = Hex(nGroup)
    nGroup = String(6 - Len(nGroup), "0") & nGroup

    pOut = Chr(CByte("&H" & Mid(nGroup, 1, 2))) + _
      Chr(CByte("&H" & Mid(nGroup, 3, 2))) + _
      Chr(CByte("&H" & Mid(nGroup, 5, 2)))

    sOut = sOut & Left(pOut, numDataBytes)

  Base64Decode = sOut
End Function
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Print - Dropbox

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube