提问人:Math869 提问时间:11/14/2023 最后编辑:Math869 更新时间:11/14/2023 访问量:112
将信息从 Excel 转换为特定于 ppt 的字段
Convert info from Excel to ppt- specific fields
问:
我想在我的 vba 代码中包含一些功能,以从 Excel 文件中提取信息并将信息粘贴到 PowerPoint 中。
您可以在下面看到更新的代码,我在这里找到了帮助我解决此问题的人。
我现在想要什么:
更改特定的文件位置,用户可以在其中选择我的宏要编辑的文件的位置。
包括一个带有底部选择选项的选项,用户可以 (a) 仅保存文件;(b) 将文件保存在他们选择的目录中。(c) 通过 Microsoft Outlook 发送。
以下是没有选择文件功能的新代码:
Sub PasteExcelDataIntoPowerPointTextbox()
Dim ppApp As Object
Dim ppSlide As Object
Dim ppTextBox As Object
Dim xlApp As Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet
Dim excelRange As Excel.Range
' Initialize PowerPoint and Excel
Set ppApp = CreateObject("PowerPoint.Application")
ppApp.Visible = True ' Make PowerPoint visible
' Open the PowerPoint presentation
Set ppPresentation = ppApp.Presentations.Open("C:\Users\Public\HiringResultsNew.pptx")
' Assuming the Excel file is already open, else you can open it too
Set xlApp = GetObject(, "Excel.Application")
Set xlWorkbook = xlApp.ActiveWorkbook
Set xlWorksheet = xlWorkbook.Worksheets("HiringResults") ' Change to your sheet name
' Get the range of Excel data you want to copy
Dim arrCell, arrShp, i as Long
arrCell=Array("C1","C2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13","D14","D15","D16","D17","D18","D19","D20","D21")
arrShp=Array("REFTYPE","REFBUSINESS","REFNUMBERFILLS","REFVARIATION","REFTTF","REFCNPS","REFHMNPS","REFACTREQ","REFDIVMALE","REFDIVFAME","REFHIREINT","REFHIREEXT","REFTSTASO","REFTSEMRE","REFTSAGENC","REFLEVEX","REFLEVDI","REFLEVMA","REFLEVIN","REFDATAREF","REFKEYINSIGHTS")
Set ppSlide = ppPresentation.Slides(1)
For i = LBound(arrCell) To UBound(arrCell)
Set excelRange = xlWorksheet.Range(arrCell(i))
Set ppTextBox = ppSlide.Shapes(arrShp(i)).TextFrame.TextRange
ppTextBox.Text = excelRange.Text
Next
' Clean up
Set ppApp = Nothing
Set xlApp = Nothing
Set xlWorkbook = Nothing
Set xlWorksheet = Nothing
Set ppPresentation = Nothing
MsgBox "Report completed. Please edit and save it."
End Sub
'I want to include the option where the user can select the file by their own, using the code mentioned below: Where exactly do I have to include the code?
Sub Demo()
Dim obiFileDialog As FileDialog, strFilePath As String
Set obiFileDialog = Application.FileDialog(msoFileDialogFilePicker)
obiFileDialog.Title = "Select PowerPoint File"
obiFileDialog.Filters.Clear
obiFileDialog.Filters.Add "PowerPoint Files", "*.ppt*"
obiFileDialog.AllowMultiSelect = False
If obiFileDialog.Show Then
strFilePath = obiFileDialog.SelectedItems(1)
MsgBox "Your file is " & strFilePath
' Your code to open pptx
End If
End Sub
一旦这是我的基本代码。我自己创造了它。由于没有 VBA 知识,我恳请您的支持人员在准备好新代码的情况下提供您的答案,或者向我提供有关选择选项文件的内容的有力详细信息。
我还有另一个问题,我保证这将是这篇文章的最后一个问题:
我担心的是,由于信息特权,不同的用户将访问它,因此理想的情况是没有人能够访问它的地方,就像它现在在 C:\Users\Public\HiringResultsNew.pptx 中一样。
请问我怎样才能更新它?
答:
0赞
taller
11/14/2023
#1
- 使用循环来简化代码
- 使用 TextBox 的属性更新其内容
Text
Sub PasteExcelDataIntoPowerPointTextbox()
Dim ppApp As Object
Dim ppSlide As Object
Dim ppTextBox As Object
Dim xlApp As Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet
Dim excelRange As Excel.Range
' Initialize PowerPoint and Excel
Set ppApp = CreateObject("PowerPoint.Application")
ppApp.Visible = True ' Make PowerPoint visible
' Open the PowerPoint presentation
' Set ppPresentation = ppApp.Presentations.Open("C:\Users\Public\HiringResultsNew.pptx")
' If both Excel and PPT are in the same folder
Set ppPresentation = ppApp.Presentations.Open(ThisWorkbook.Path & "\HiringResultsNew.pptx") '**change**
' Assuming the Excel file is already open, else you can open it too
Set xlApp = GetObject(, "Excel.Application")
Set xlWorkbook = xlApp.ActiveWorkbook
Set xlWorksheet = xlWorkbook.Worksheets("HiringResults") ' Change to your sheet name
' Get the range of Excel data you want to copy
Dim arrCell, arrShp, i as Long
arrCell=Array("C1","C2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13","D14","D15","D16","D17","D18","D19","D20","D21")
arrShp=Array("REFTYPE","REFBUSINESS","REFNUMBERFILLS","REFVARIATION","REFTTF","REFCNPS","REFHMNPS","REFACTREQ","REFDIVMALE","REFDIVFAME","REFHIREINT","REFHIREEXT","REFTSTASO","REFTSEMRE","REFTSAGENC","REFLEVEX","REFLEVDI","REFLEVMA","REFLEVIN","REFDATAREF","REFKEYINSIGHTS")
Set ppSlide = ppPresentation.Slides(1)
For i = LBound(arrCell) To UBound(arrCell)
Set excelRange = xlWorksheet.Range(arrCell(i))
Set ppTextBox = ppSlide.Shapes(arrShp(i)).TextFrame.TextRange
ppTextBox.Text = excelRange.Text
Next
' Clean up
Set ppApp = Nothing
Set xlApp = Nothing
Set xlWorkbook = Nothing
Set xlWorksheet = Nothing
Set ppPresentation = Nothing
MsgBox "Report completed. Please edit and save it."
End Sub
Microsoft 文档:
更新:
问:是否可以更改文件位置?我的意思是:这个宏不是从“C:\users...”打开文件,而是让用户选择文件位置的选项?
Sub Demo()
Dim obiFileDialog As FileDialog, strFilePath As String
Set obiFileDialog = Application.FileDialog(msoFileDialogFilePicker)
obiFileDialog.Title = "Select PowerPoint File"
obiFileDialog.Filters.Clear
obiFileDialog.Filters.Add "PowerPoint Files", "*.ppt*"
obiFileDialog.AllowMultiSelect = False
If obiFileDialog.Show Then
strFilePath = obiFileDialog.SelectedItems(1)
MsgBox "Your file is " & strFilePath
' Your code to open pptx
End If
End Sub
Microsoft 文档:
评论
0赞
Math869
11/14/2023
我是否必须在代码的开头包含任何其他内容,或者只是为此进行修改?
0赞
taller
11/14/2023
我已经用完整的代码更新了它。
0赞
Math869
11/14/2023
我会更新它并让你知道!非常感谢。
0赞
Math869
11/14/2023
设置 xlWorksheet = xlWorkbook.Worksheets(“HiringResults”) ' 在此行中出现错误运行时错误 -9 下标超出范围。如何解决?
0赞
Math869
11/14/2023
它起作用了,但我想知道为什么我在第一次执行宏时发现错误 -9。
评论
no one would be able to access it
**change**