excel 在outlook中更新团队中的文件的VBA代码

gblwokeq  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(140)

我希望用户打开他们的Outlook,并有一个功能,将更新达特(主)文件,提示他们打开源文件。达特文件位于teams/sharepoint中。
用我的代码,它只制作了主文件的副本,这不是我想要的。

Sub UpdateTATFile()
    Dim tatFile As Workbook
    Dim tatSheet As Worksheet
    Dim sourceFile As Workbook
    Dim lastRow As Long
    Dim sourceFilePath As String
    
    ' Open the TAT file from Teams/SharePoint
    Dim sFileName As String
    sFileName = "link.xlxs" ' Update with the actual URL of your TAT file
    
    ' Create a new instance of Excel
    Dim xl As Object
    Set xl = CreateObject("Excel.Application")
    
    ' Open the TAT file
    Dim f As Object
    Set f = xl.Application.Workbooks.Open(fileName:=sFileName, ReadOnly:=False)
    
    ' Set the TAT file as the active workbook
    Set tatFile = xl.Application.ActiveWorkbook
    
    ' Specify the TAT sheet in the TAT file
    Set tatSheet = tatFile.Worksheets("IS PO bookings") ' Update with the appropriate TAT sheet name
    
    ' Prompt the user to select the source file
    Dim fileDialog As Object
    Set fileDialog = xl.Application.fileDialog(3) ' 3 corresponds to the FilePicker dialog type
    
    ' Display the file picker dialog
    If fileDialog.Show = -1 Then ' If the user clicked the "Open" button
        ' Get the selected file path
        sourceFilePath = fileDialog.SelectedItems(1)
        
        ' Check if the user selected a file
        If Len(sourceFilePath) > 0 And Dir(sourceFilePath) <> "" Then
            ' Open the source file
            Set sourceFile = xl.Application.Workbooks.Open(sourceFilePath, UpdateLinks:=False)
            
            ' Find the last row in the TAT sheet
            lastRow = tatSheet.Cells(tatSheet.Rows.Count, "A").End(xlUp).Row + 1
            
            ' Retrieve data from the source file and populate in the TAT sheet
            tatSheet.Cells(lastRow, "A").Value = sourceFile.Worksheets("Order Entry Form").Range("F13").Value
            tatSheet.Cells(lastRow, "B").Value = sourceFile.Worksheets("Order Entry Form").Range("D13").Value
            tatSheet.Cells(lastRow, "C").Value = sourceFile.Worksheets("Order Entry Form").Range("D21").Value
            tatSheet.Cells(lastRow, "D").Value = sourceFile.Worksheets("Order Entry Form").Range("D9").Value
            tatSheet.Cells(lastRow, "E").Value = Date
            tatSheet.Cells(lastRow, "F").Value = sourceFile.Worksheets("Order Entry Form").Range("D15").Value
            tatSheet.Cells(lastRow, "G").Value = sourceFile.Worksheets("Order Entry Form").Range("D7").Value
            tatSheet.Cells(lastRow, "H").Value = sourceFile.Worksheets("Order Entry Form").Range("F19").Value
            tatSheet.Cells(lastRow, "I").Value = sourceFile.Worksheets("Order Entry Form").Range("F9").Value
            tatSheet.Cells(lastRow, "M").Value = sourceFile.Worksheets("Order Entry Form").Range("O9").Value
            tatSheet.Cells(lastRow, "N").Value = sourceFile.Worksheets("ENQUIRY - ORDER REVIEW").Range("F27").Value
            tatSheet.Cells(lastRow, "O").Value = "PRV"
            
            ' Close the source file without saving changes
            sourceFile.Close SaveChanges:=False
        Else
            MsgBox "Source file not found.", vbExclamation
        End If
    End If
    
    ' Save and close the TAT file
    tatFile.Close SaveChanges:=True
    
    ' Close Excel
    xl.Quit
    Set xl = Nothing
End Sub
k75qkfdt

k75qkfdt1#

目前还不清楚代码中使用的实际文件路径是什么--它是一个文件名还是一个完整的URL。此外,确保手动完成相同的操作而不会出现问题也是有意义的。
用我的代码,它只会复制主文件,这不是我想要的
大多数Office应用程序被设计为处理本地文件。您需要在本地下载所需的文件,然后才能使用Outlook或Excel对象模型进行所需的更改。

vtwuwzda

vtwuwzda2#

您需要确保使用正确的URL从SharePoint打开工作簿。要获取URL,请从SP打开工作簿以在Excel中进行编辑,然后打开Excel VB编辑器,并在“即时”窗格中(Ctrl+G)键入:

? ActiveWorkbook.FullName

然后按回车键
你会得到类似这样的东西:

https://yourcompany.sharepoint.com/sites/Blah/TestLibrary/_test.xlsx

当传递给xl.Application.Workbooks.Open()时,这应该可以在编辑模式下打开工作簿
注意:如果您在SP中使用“复制链接”功能,则URL与(例如)从SP获得的URL不同:

https://yourcompany.sharepoint.com/:x:/s/Blah/Efz3ZR_-JvNEv7n8d9oYq58B7mhqh1GYvCxLdtrxsGu9Bg?e=Ebktf2

相关问题