save userform as pdf file to Microsoft Teams folder

Mr_Ragweed2

Board Regular
Joined
Nov 11, 2022
Messages
145
Office Version
  1. 365
Platform
  1. Windows
Howdy,
I have a userform that i currently can print. I would like to print and save from the "print button" on the form. I'm assuming it would save as a pdf but maybe an image is easier? Doesn't matter to me. I would like to save it to a specific folder in Microsoft Teams.
my code for printing is below. (It is not specific to my printer as it allows many users to print to their default printer.)

VBA Code:
If Application.Dialogs(xlDialogPrinterSetup).Show Then
    UserForm1_MyUserformName.PrintForm
    Else
    Exit Sub
End If

I have a cell to reference for giving a unique name to each file: Sheet("Special Sheet").Range("D1")
All users would be saving to the same file:
"https://blah blah.sharepoint.com/:f:?r/teams/blah blah/Shared%20Documents/General/Blah%20Apps/abc%20Orders?csf=1&web=1&e=xW6Z7s"

Any help is greatly appreciated. Thank you very much!
 
Does this work for saving on SharePoint OneDrive ?
VBA Code:
Private Sub CommandButton1_Click()
  
    Dim sPath As String, sFile As String
 
    sPath = "https://My_Company.sharepoint.com/teams/Company_File/Shared%20Documents/General/Sales%20Apps/Seed%20Orders/"
    sFile = "MyFormImage.png"
 
    If SaveUserFormToDisk(Me, sPath, sFile) Then
        MsgBox "UserForm Image saved as: " & sPath & sFile, vbInformation, "SaveUserFormToDisk."
    Else
        MsgBox "Failed to save UserForm Image to disk." & vbLf & vbLf & _
            "Check validity of file path & file extension.", vbCritical
    End If

End Sub
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try this for saving the image in the user's desktop folder:
VBA Code:
Private Sub CommandButton1_Click()
 
    Dim sPath As String, sFile As String
 
    sPath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
    sFile = "MyFormImage.png"
 
    If SaveUserFormToDisk(Me, sPath, sFile) Then
        MsgBox "UserForm Image saved as: " & sPath & sFile, vbInformation, "SaveUserFormToDisk."
    Else
        MsgBox "Failed to save UserForm Image to disk." & vbLf & vbLf & _
            "Check validity of file path & file extension.", vbCritical
    End If

End Sub

As for saving on the specific folder in Microsoft Teams, maybe someone else here can figure it out as I am not familiar with that. The problem is probably due to the url path format.

PS: Reason for not wanting to save to the C:\ drive is because the user may need elevated administrator access.
 
Last edited:
Upvote 0
Oh that works beautifully!! Thank you so very much! This at least gives me a workable release until i can figure out something with Teams (if even possible).

PS: I have no special access than the users. I am one of the users and i was just unsatisfied with our currently methodology for capturing these sales, so i made a better one. But i like your version and your reasoning :)

So i think the only thing left for me to tidy up at this point is to write a few lines to check for duplicate file names and then solve that with a msgbox requiring the user to pick a new name.

I also have to put this practice code into my actual userform. Unless i have problems there...i think we might be done??? Once that works i will mark your code as "Solved".

Dan_W - huge thanks to you as well!!!

I am truly grateful to both of you. I only hope to one day be able to help others in the forum the way you have helped me.
🍻
 
Upvote 0
So i think the only thing left for me to tidy up at this point is to write a few lines to check for duplicate file names and then solve that with a msgbox requiring the user to pick a new name.

How about time stamping the the file name as in your code in post #15 or my code in post #44 ?

Rich (BB code):
Private Sub CommandButton1_Click()

    Dim sPath As String, sFile As String

    sPath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
    sFile = Format(Now, "dd-mmm-yyyy - hh-mm-ss") & " - " & "MyFormImage.png"

    If SaveUserFormToDisk(Me, sPath, sFile) Then
        MsgBox "UserForm Image saved as: " & sPath & sFile, vbInformation, "SaveUserFormToDisk."
    Else
        MsgBox "Failed to save UserForm Image to disk." & vbLf & vbLf & _
            "Check validity of file path & file extension.", vbCritical
    End If

End Sub

That added date & time stamp will ensure the image file(s) is\are never overrriden.
 
Upvote 0
Also, another option that you may want to consider, is to give the users a choice to save the form image under whichever name and location they wish as well as in the image format they want... You can achieve this by prompting them to enter the file name, the file path (folder) and the file image type via the GetSaveAsFilename dialogbox.

Try this code variation :
VBA Code:
Private Sub CommandButton1_Click()
 
    'Image formats accepted :
    '=====================
    ' BMP,DIB,RLE,JPEG,JPG,JPE,JFIF,GIF,TIFF,TIF,PNG

    Dim sPath As String, sFile As String
    Dim sFileSaveName As Variant, sFileFilter As Variant

    sFileFilter = "File BMP (*.BMP),*.BMP, File PNG (*.PNG),*.PNG,File Gif (*.GIF),*.GIF,File JPEG (*.JPG),*.JPG,"
    sFileFilter = sFileFilter & "File JPE (*.JPE),*.JPE,File DIB (*.DIB),*.DIB,File RLE (*.RLE),*.RLE,"
    sFileFilter = sFileFilter & "File JFIF (*.JFIF),*.JFIF,File TIFF (*.TIFF),*.TIFF,"
    sFileFilter = sFileFilter & "File TIF (*.TIF),*.TIF"

    sFileSaveName = Application.GetSaveAsFilename("", sFileFilter)
 
    If sFileSaveName <> "False" Then
        sPath = Left(sFileSaveName, InStrRev(sFileSaveName, Application.PathSeparator))
        sFile = Right(sFileSaveName, Len(sFileSaveName) - InStrRev(sFileSaveName, Application.PathSeparator))
        If SaveUserFormToDisk(Me, sPath, sFile) Then
            MsgBox "UserForm Image saved as: " & sPath & sFile, vbInformation, "SaveUserFormToDisk."
        Else
            MsgBox "Failed to save UserForm Image to disk." & vbLf & vbLf & _
                "Check validity of file path & file extension.", vbCritical
        End If
    End If

End Sub

This will offer more flexibility to the users of your code.
 
Last edited:
Upvote 0
Hello. My apologies, but my Sunday was uncharacteristically hectic. I see that you've resolved the image capture part of the task (Thank you, Jaafar).

As for the Teams/Sharepoint element, I heard back from my friend about who to go about connecting to your account with VBA. It appears that Office 365 allows you to save to a Sharepoint URL with VBA, but that this is limited to MS Office documents. This would explain why you are able to save the workbook, but not the image files. This is what I suspected might be the case, and so it is for that reason I thought it might make sense (for now, at least) to use Jaafar's solution above to save the image capture, and then reimport them back into the workbook - at least that way you will have the image and it's easy enough to save the workbook to your Teams.

For non-Office documents, the method is a trickier; it requires connecting to an API and upload the file to the Sharepoint server with your account details. The good news is that my friend has written a set of routines and put them on the internet somewhere (Github, I think); the (possibly) bad news is that the code is designed for a SOAP API, and it may be the case that MS have changed technology since he wrote the library of routines. I will check when I get home today.
 
Upvote 0
Also, another option that you may want to consider, is to give the users a choice to save the form image under whichever name and location they wish as well as in the image format they want... You can achieve this by prompting them to enter the file name, the file path (folder) and the file image type via the GetSaveAsFilename dialogbox.
Wow. I've not even heard of some of these image file type!
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,969
Members
452,539
Latest member
delvey

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top