[英]VBA Excel Paste to PowerPoint in Bold
Hi Have built this string 嗨,已经建立了这个字符串
str = ""
str = "Release Date" & str & objWorksheet.Cells(i, 1).Value & Chr(13)
str = "Distributor" & str & objWorksheet.Cells(i, 2).Value & Chr(13)
str = "Genre" & str & objWorksheet.Cells(i, 10).Value & Chr(13)
str = "Starring" & str & objWorksheet.Cells(i, 7).Value & Chr(13) & Chr(13)
str = str & objWorksheet.Cells(i, 14).Value
In Excel and ready to copy it over to a text box in PowerPoint 2010. 在Excel中,准备将其复制到PowerPoint 2010中的文本框中。
The words in "" I would like to be Bold when pasting, with everything else regular. 我希望在粘贴时将“”中的单词加粗,其他所有规则的内容都应加粗。
How do I go about doing that (coding in Excel VBA). 我该怎么做(在Excel VBA中编码)。
The PowerPoint presentation is created afresh every time so can't have the code in PowerPoint. PowerPoint演示文稿每次都会重新创建,因此无法在PowerPoint中使用代码。
Thank you! 谢谢!
My Code looks like this: I keep getting MisMatch error 13 when it tries to run BoldSomeWords 我的代码如下所示:尝试运行BoldSomeWords时,我不断收到MisMatch错误13
Hi David. 嗨,大卫。 Just tried this and I'm getting Type Mismatch error.
刚刚尝试过此操作,但出现Type Mismatch错误。 My full code looks like this:
我的完整代码如下所示:
Sub CreateSlides()
'Dim the Excel objects
Dim objWorkbook As New Excel.Workbook
Dim objWorksheet As Excel.Worksheet
'Dim the File Path String
Dim strFilePath As String
'Dim the PowerPoint objects
Dim PPT As Object
Dim pptSlide As PowerPoint.Slide
Dim pptLayout As PowerPoint.CustomLayout
Dim pptNewSlide As PowerPoint.Slide
Dim str As String
Dim Title As String
Set PPT = GetObject(, "PowerPoint.Application")
PPT.Visible = True
'Get the layout of the first slide and set a CustomLayout object
Set pptLayout = PPT.ActivePresentation.Slides(1).CustomLayout
'Run the OpenFile function to get an Open File dialog box. It returns a String containing the file and path.
strFilePath = OpenFile()
'Open the Excel file
Set objWorkbook = Excel.Application.Workbooks.Open(strFilePath)
'Grab the first Worksheet in the Workbook
Set objWorksheet = objWorkbook.Worksheets(1)
'Loop through each used row in Column A
For i = 2 To objWorksheet.Range("A65536").End(xlUp).Row
Set PPT = GetObject(, "PowerPoint.Application")
Set pptNewSlide = PPT.ActivePresentation.Slides.AddSlide(PPT.ActivePresentation.Slides.Count + 1, pptLayout)
'Get the number of columns in use on the current row
Dim LastCol As Long
Dim boldWords As String
boldWords = "Release Date: ,Distributor: ,Genre: ,Starring: "
LastCol = objWorksheet.Rows(i).End(xlToRight).Column
If LastCol = 16384 Then LastCol = 1 'For some reason if only column 1 has data it returns 16384, so correct it
'Build a string of all the columns on the row
str = ""
str = "Release Date: " & str & objWorksheet.Cells(i, 1).Value & Chr(13) & _
"Distributor: " & objWorksheet.Cells(i, 2).Value & Chr(13) & _
"Genre: " & objWorksheet.Cells(i, 10).Value & Chr(13) & _
"Starring: " & objWorksheet.Cells(i, 7).Value & Chr(13) & Chr(13) & _
objWorksheet.Cells(i, 14).Value
sfile = Cells(i, 3) & ".jpg"
Set PPT = GetObject(, "PowerPoint.Application")
spath = "FILEPATHGOESHERE"
'Write the string to the slide
pptNewSlide.Shapes(2).TextFrame.TextRange.Text = objWorksheet.Cells(i, 3).Value 'This enters the film Title
PPT.ActivePresentation.Slides(PPT.ActivePresentation.Slides.Count).Shapes(1).TextFrame.TextRange.Text = str
BoldSomeWords PPT.ActivePresentation.Slides(PPT.ActivePresentation.Slides.Count).Shapes(1), str, boldWords
Applying formatting to substrings is not difficult, but it can be cumbersome given the depth of PowerPoint's object model. 将格式设置应用于子字符串并不难,但是鉴于PowerPoint对象模型的深度,这样做可能会很麻烦。 Here is a simple illustration.
这是一个简单的例子。
NOTE I modified the string concatenation because it looked incorrect to me (essentially you were duplicating the string within itself, which didn't seem correct). 注意我修改了字符串连接,因为它对我来说看起来是不正确的(本质上是您在内部复制字符串,这似乎不正确)。
Sub Main()
'This will be the calling procedure, so it can be the one which
' builds the concatenated string, but also will be where you define
' which parts of this string should be bold
Dim str$
Dim boldWords as String
'Establish a string of words/phrases to bold, to be used later
boldWords = "Release Date,Distributor,Genre,Starring"
'Create your concatenated string
' I modified this because I don't think it was correct
str = ""
str = "Release Date" & str & objWorksheet.Cells(i, 1).Value & Chr(13) & _
"Distributor" & objWorksheet.Cells(i, 2).Value & Chr(13) & _
"Genre" & objWorksheet.Cells(i, 10).Value & Chr(13) & _
"Starring" & objWorksheet.Cells(i, 7).Value & Chr(13) & Chr(13) & _
objWorksheet.Cells(i, 14).Value
'Put these words in a shape:
ActivePresentation.Slides(1).Placeholders(1).TextFrame.TextRange.Text = str
'Send your string, the list of bold words, and the shape in to a subroutine
' that will apply the bold font
BoldSomeWords ActivePresentation.Slides(1).Shapes(1), str, boldWords
That subroutine will then invoke the following sub, which applies formatting bold for particular substrings, given as part of the list assigned in myWords
variable, above. 然后,该子例程将调用以下子例程,该子例程将特定子字符串的格式设置为粗体,这是上面
myWords
变量中分配的列表的一部分。
Sub BoldSomeWords(shp As Object, str As String, myWords As String)
Dim word As Variant
Dim iStart As Integer, iEnd As Integer
'Convert the list of words in to an iterable array, and
' iterate it.
For Each word In Split(myWords, ",")
'Loop just in case there are duplicates, or omit the Do Loop if
' that is not a concern.
Do Until InStr(iEnd + 1, str, word) = 0
iStart = InStr(iStart + 1, str, word)
iEnd = iStart + Len(word)
shp.TextFrame.TextRange.Characters(iStart, Len(word)).Characters.Font.Bold = msoTrue
Loop
Next
End Sub
UPDATE FROM COMMENTS 评论更新
I changed the declaration for shp
in the BoldSomeWords
routine to generic object instead of Shape
. 我将
BoldSomeWords
例程中的shp
声明更改为通用对象,而不是Shape
。 I think there is a conflict between a PowerPoint.Shape
and an Excel.Shape
, and in the context of your VBE, it assumes to be an Excel.Shape
, hence the mismatch. 我认为
PowerPoint.Shape
和Excel.Shape
之间存在冲突,并且在您的VBE上下文中,它假定是Excel.Shape
,因此不匹配。
Either of the following should work, late-binding as generic Object
: 下列任何一种都应该起作用,将其后期绑定为通用
Object
:
BoldSomeWords(shp As Object, str As String, myWords As String)
Or early-binding as a strongly-typed shape: 或早期绑定为强型形状:
BoldSomeWords(shp As PowerPoint.Shape, str As String, myWords As String)
A few more things to consider... 还有几件事要考虑...
Get rid of New
keyword in your objWorkbook declaration, it's unnecessary because you're assigning the objWorkbook
using the Excel.Application.Workbooks.Open
method, so you don't need a New
workbook. 摆脱objWorkbook声明中的
New
关键字,这是不必要的,因为您是使用Excel.Application.Workbooks.Open
方法分配objWorkbook
的,因此不需要New
工作簿。
Change PPT from generic Object to strongly typed PowerPoint.Application
there is no real reason to mix early- and late-binding your objects. 将PPT从通用对象更改为强类型
PowerPoint.Application
程序没有真正的理由混合早期和晚期绑定您的对象。
You have three assignment statements Set PPT = GetObject(, "PowerPoint.Application")
. 您有三个赋值语句
Set PPT = GetObject(, "PowerPoint.Application")
。 Only the first one is needed. 只需要第一个。
Declare ALL of your variables, sPath, sFile, i... There may be more, if you use Option Explicit
that will identify the undeclared variables for you. 声明所有变量,sPath,sFile等。如果使用
Option Explicit
,它将为您标识未声明的变量,可能还有更多。
There were one or two places where you used the fully qualified PPT.ActivePresentation.Slides(PPT.ActivePresentation.Slides.Count)
when you already have a variable pptNewSlide
. 当已经有变量
pptNewSlide
时,在一两个地方使用了完全限定的PPT.ActivePresentation.Slides(PPT.ActivePresentation.Slides.Count)
。 Use this for brevity's sake, instead, for example in the last two lines, you could change to: 为了简洁起见,请使用此代码,例如,在最后两行中,您可以更改为:
pptNewSlide.Shapes(1).TextFrame.TextRange.Text = str
BoldSomeWords pptNewSlide.Shapes(1), str, boldWords
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.