使用动态文件名保存到SharePoint的VBA宏

时间:2015-09-25 16:34:25

标签: excel vba excel-vba sharepoint

我有一张excel表,我想通过宏保存到SharePoint。我对它进行了排序,但问题是每个月文件名都不同,我不想每次都更改宏(即filenameSeptember,filenameOctober等)。

我正在使用一个非常基本的宏,因为我不是非常擅长编写它们,它可以工作,但它的旧文件名是硬编码的:

Sub savetest()

ActiveWorkbook.SaveAs Filename:= _
    "http://SharePointdirectory/filenameSeptember.xlsm" _
    , FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

End Sub

上下文:每个月我都会使用新数据更新此文件并使用新文件名保存,并将其发送给用户进行验证 - 我希望此用户能够通过宏直接上传到SP目录自动使用我称之为文件的文件名。

我希望这是有道理的,并提前感谢。

3 个答案:

答案 0 :(得分:4)

这对你有用

Sub savetest()

ThisWorkbook.SaveAs Filename:= _
    "\\SharepointDirectory\" & ThisWorkbook.Name & MonthName(Month(Date), False) _
    , FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

End Sub

答案 1 :(得分:1)

我最终使用单元格来指定文件名并改为存储路径,这似乎有效:

Public Sub SavetoSP()
ThisFile = Range("D1").Value
ThisPath = Range("J1").Value

ActiveWorkbook.SaveAs Filename:=ThisPath & ThisFile & ".xlsm"
End Sub

答案 2 :(得分:0)

我相信问题和答案对于自动化办公室工作非常有用。提供通用功能以在SharePoint文档库上存储信息。

注意:用户必须将SharePoint库URL复制到特定的工作表中

A。我的程序模板

'========================================
'SAVE THIS FILE TO A SHAREPOINT
'========================================

Sub Push2SharePoint()

    ' define variables
    Dim SharePointPath As Variant
    Dim FileAsNamed As Variant
    ' retrieve SharePoint path indicated by the user inside Excel Sheet named "Select" on cell B33
    SharePointPath = ThisWorkbook.Sheets("Select").Range("B33").Text
    ' provide some error message if it's not populated
    On Error GoTo NoStorageSelected
    If Not SharePointPath <> False Then
        'Displaying a message if file not choosedn in the above step
        MsgBox "No storage space was selected.", vbExclamation, "Sorry!"
        'And existing from the procedure
        Exit Sub
    Else
        'Create the new file name, note we place data format in ISO 8601 format in front of the file name
        FileAsNamed = SharePointPath & Year(Date) & "-" & Month(Date) & "-" & Day(Date) & "_" & ThisWorkbook.Name

        ' save the copy
        ThisWorkbook.SaveAs FileName:=FileAsNamed, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False


    End If

Exit Sub
' Error Management
NoStorageSelected:
           MsgBox "Error: Excel can not reach SharePoint Folder Storage location" & vbCrLf & _
           "Possible reasons are: Storage location was not defined in the Worksheet 'Select' cell B33 or " & vbCrLf & _
           "Not having sufficient previledges to access SharePoint location " & vbCrLf & _
           "Make sure to add forward slash after SharePoint Document Library"
           Exit Sub

End Sub

B。参考文档

Microsoft Documentation ThisWorkbook.SaveAs Method