如何使用VBA将存储在SharePoint中的文件内容导入到MS Word文件中?

huangapple go评论60阅读模式
英文:

How can one import the contents of a file that is stored in SharePoint into an MS Word file using VBA?

问题

我有以下的使用情况。

我有一个文档,其中有一个“条款和条件” H1 部分。该部分没有内容。

这个部分的内容存储在不同的文件中(取决于需要合并的 T&Cs 的地区),这些文件都存储在 SharePoint 上。例如,美国的 T&C 存储在不同的文件中,而加拿大的 T&C 存储在不同的文件中,都在 SharePoint 上。

我想要一个功能,用户可以选择地区(使用用户表单中的复选框字段),以便他们可以插入相应地区的 T&C。然后,SharePoint 上文件的内容将被粘贴到“条款和条件”部分。

请问如何使用 VBA 完成这个任务。您能为我分解一下吗?

英文:

I have the following use case.

I have a document that has a "Terms and Conditions" H1 section. There is no content in that section.

The content of this section is in different files (depending on the region for which the T&Cs need to be incorporated) on SharePoint. For example, the T&C for America is in a different file, and the T&C for Canada is in a different file--all on SharePoint.

I want to have a functionality where the user can select the region (using a checkbox field in user form) for which they want to plug in the T&C. And then the content of the file, which is on SharePoint is pasted in the Terms and Conditions section.

How can i please accomplish this using VBA. Can you please break it down for me.

答案1

得分: 1

如果您在文档中创建一个书签(例如“TandC”),则可以执行以下操作:

Dim url as String 

'根据用户的选择分配一个网址...
url = "https://blah.sharepoint.com/sites/yoursite/TestLibrary/TandC_USA.docx" '例如

ActiveDocument.Bookmarks("TandC").Range.InsertFile url

与其每个文档都有一个单独的T&C,您可以将所有T&C合并到同一文档中,并为每个T&C分配一个不同的书签,然后在调用InsertFile时提供该书签名称:

ActiveDocument.Bookmarks("TandC").Range.InsertFile url, "TandC_USA"

参见:
https://learn.microsoft.com/en-us/office/vba/api/word.range.insertfile

英文:

If you create a bookmark (eg "TandC") in your document then you can do this:

Dim url as String 

'Assign a url based on the user's selection...
url = "https://blah.sharepoint.com/sites/yoursite/TestLibrary/TandC_USA.docx" 'for example

ActiveDocument.Bookmarks("TandC").Range.InsertFile url

Instead of having one document each with a separate T&C, you could combine all of the T&C in the same document, and give each one a distinct bookmark, then provide that bookmark name when calling InsertFile:

ActiveDocument.Bookmarks("TandC").Range.InsertFile url, "TandC_USA"

See:
https://learn.microsoft.com/en-us/office/vba/api/word.range.insertfile

答案2

得分: 0

我想要实现一个功能,让用户可以选择他们想要插入T&C的区域。然后,将位于SharePoint上的文件内容粘贴到《条款和条件》部分。

你想如何实现这个功能?使用 内容控件,Active X 控件,用户表单 等等?

然后,你必须拥有完整的权限来读取和下载完整文件。

你的功能主体可能是这样的:

Rem 通过 YouChat 进行信用:
Sub ImportSharePointFile()

    ' 定义变量
    Dim SPFileUrl As String  ' SharePoint文件的URL '
    Dim SPFileName As String ' SharePoint文件的文件名
    
    Dim HttpReq As Object       ' Http请求对象
    Dim Stream As Object        ' ADODB Stream对象
    Dim TempFilePath As String  ' 临时文件路径
    
    Dim Doc As Document         ' Word文档对象
    
    Dim rngToInsert As Range
        
    ' 设置SharePoint文件的URL和文件名
    SPFileUrl = "https://...."
    SPFileName = ".... yourfile.docx"
    
    ' 发送Http请求以下载SharePoint文件到本地
    Set HttpReq = CreateObject("MSXML2.XMLHTTP")
    HttpReq.Open "GET", SPFileUrl, False
    HttpReq.send
    
    If HttpReq.Status = 200 Then ' Http请求成功
    
        ' 创建临时文件
        TempFilePath = VBA.Environ$("temp") & "\" & SPFileName
        Set Stream = CreateObject("ADODB.Stream")
        Stream.Type = 1 ' 二进制类型
        Stream.Open
        Stream.Write HttpReq.responseBody ' 将Http请求的内容写入Stream
        Stream.SaveToFile TempFilePath, 2 ' 将Stream的内容写入文件
        Stream.Close
        Set Stream = Nothing
        
        ' 将临时文件的内容插入Word文档
        Set Doc = ActiveDocument ' 假设当前文档是需要导入的Word文档
         '插入到指定位置
        Set rngToInsert = Doc.Sections("Terms and Conditions").Range
        rngToInsert.InsertFile TempFilePath
        
        ' 删除临时文件
        Kill TempFilePath
    
    End If
    
    ' 释放对象
    Set HttpReq = Nothing
    
End Sub
英文:

> I want to have a functionality where the user can select the region for which they want to plug in the T&C. And then the content of the file, which is on SharePoint is pasted in the Terms and Conditions section.

And how do you want to implement the facility? Content Controls, Active X Controls, User Form, etc.?

Then you must have full permission to read and download the complete file.

The body of the functionality of yours may be like this :

Rem creedit with YouChat:
Sub ImportSharePointFile()

    ' Defined variables
    Dim SPFileUrl As String  ' SharePoint file's URL '
    Dim SPFileName As String ' File name of the SharePoint file
    
    Dim HttpReq As Object       ' Http request object
    Dim Stream As Object        ' ADODB Stream object
    Dim TempFilePath As String  ' Temporary file path
    
    Dim Doc As Document         ' Word Document object
    
    Dim rngToInsert As Range
        
    ' set the SharePoint file's URL and it's name
    SPFileUrl = "https://...."
    SPFileName = ".... yourfile.docx"
    
   ' Send Http request to download SharePoint files to local
    Set HttpReq = CreateObject("MSXML2.XMLHTTP")
    HttpReq.Open "GET", SPFileUrl, False
    HttpReq.send
    
    If HttpReq.Status = 200 Then ' Http request success
    
        ' Create a temporary file
        TempFilePath = VBA.Environ$("temp") & "\" & SPFileName
        Set Stream = CreateObject("ADODB.Stream")
        Stream.Type = 1 ' Binary Types
        Stream.Open
        Stream.Write HttpReq.responseBody ' Write the contents of the Http request to Stream
        Stream.SaveToFile TempFilePath, 2 ' Write the contents of the Stream to a file
        Stream.Close
        Set Stream = Nothing
        
        ' Insert the contents of the temporary file into a Word document
        Set Doc = ActiveDocument ' Assuming that the current document is a Word document that needs to be imported
         'insert into the specific position
        Set rngToInsert = Doc.Sections("Terms and Conditions").Range
        rngToInsert.InsertFile TempFilePath
        
        ' Delete the temporary file
        Kill TempFilePath
    
    End If
    
    ' Released Objects
    Set HttpReq = Nothing
    
End Sub

huangapple
  • 本文由 发表于 2023年6月8日 12:50:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定