Long story short, I need to maintain a reference table that maps criteria to the slides where these criteria are addressed. Obviously, this is painful since people will be updating PowerPoints until the very last minute.
My goal is to have a macro that can do the following:
I put all PowerPoint files in a given folder (we'll it .../ImportMe)
Run a script that opens each PowerPoint file.
Makes a list of the Slide#'s in column B
Find the Text Box I'm looking for (These all start with "CT:"). Copy the text and paste it into column C for that respective row.
Put the PowerPoint file name in column A for all applicable rows.
I.e. the below...
I have created code that can get me the slide numbers (and I could get the file name, although I have not done it yet). I'm struggling with copying and pasting the data from a specific textbox (or at all, really) -- this is the portion I want to focus on at the moment...
Set xlSheet = Excel.Application.ActiveWorkbook.Sheets("Reference Table")
pptpath = "C:\Users\Username\ImportMe"
Set PP = CreateObject("PowerPoint.Application")
Set pptPres = PP.Presentations.Open(pptpath)
PP.Visible = msoCTrue
For Each pptSlide In pptPres.Slides
'Find new last row
LastRow = xlSheet.Range("B" & xlSheet.Rows.Count).End(xlUp).Row
NewRow = LastRow + 1
xlSheet.Range("B" & NewRow).Value = pptSlide.SlideNumber
For Each pptShape In pptSlide.Shapes
If pptShape.TextFrame2.HasText Then
pptText = pptShape.TextFrame2.TextRange.Text
If InStr(1, pptText, "CT:") > 0 Then
'pptShape.TextFrame2.TextRange.Copy
xlSheet.Range("C" & NewRow).Value = pptText
Else
'Do Nothing
End If
Else
'Do Nothing
End If
Next pptShape
Next pptSlide
pptPres.Close
This just gives me a blank in column C. My impression is that I'm not looping through the PowerPoint "shapes" correctly. I say that because this because it will correctly put the slide #'s in column B.
Please let me know if you have any questions!
Edit to address comment:
Added additional code
The code is hosted in excel
With ".Value" I get that same exact result
***Important Note: I had an error bypass on (not sure why -- this is dumb when testing code). I turned it off and I am getting error...
Run-time error '-2147024893 (80070003)': Method 'Open' of objection 'Presentations' failed
Which doesn't make any sense because the code is able to open the PP and pull the slide #'s.
I'm writing an Excel macro to read MS Word comments and put it in a sheet. To do this I'm using this VBA Functions:
Set myWord = New Word.Application
Set myDoc = myWord.Documents.Open(varFile)
For Each thisComment In myDoc.Comments
With thisComment
destSheet.Cells(rowToUse, colToUse + 2).Value = .Range.Text
destSheet.Cells(rowToUse, colToUse + 5).Value = .Scope.Text
End With
rowToUse = rowToUse + 1
Next thisComment
Is there a VBA function that could give me the internal number of the comment? I cannot use INDEX because it gave me the location of the comment inside the collection, and it change if the user insert another comments.
What I need is to be able to identify the comments inside the sheet, so I can Append/Update/Delete comments (specially Update) on the sheet.
I'm using MS Word comments to insert "qualitative coding" info on my documents, controling with added specific info (for each comment) in a Excel sheet.
Warm regards
Despite my lack of coding experience and using code stolen from various other posts, I have managed to cobble together an Excel VBA that opens up an existing Word document and then customizes it using values in specific cells in my excel workbook. So for example, the VBA searches the Word document for specified phrases like "[Client Name]" and "[Contract Date]" (and so on), and then replaces them with the specified Excel cell values such as "Bob's Burgers, Inc." and "Jan. 9, 2011" (as the case may be). It works great except the "[Client Name]" in each of the Word document's four Headers are completely ignored by the find/replace process.
In short, I am looking for the snippet of code that would modify the existing macro so that it finds/replaces in the headers/footers at the same time as the main body of the document. If this isn't possible, what would the vba code to add an additional find/replace macro to find each instance of "[Client Name]" in each header and replace with the needed custom phrase from Excel?
Here is the find/replace part of my macro as it currently reads:
Set WA = CreateObject("Word.Application")
WA.Documents.Open (pathh)
WA.Visible = True
For oCell = 1 To 44
from_text = Sheets("ReplaceLIST").Range("A" & oCell).Value
to_text = Sheets("ReplaceLIST").Range("B" & oCell).Value
With WA.ActiveDocument
Set myRange = .Content
With myRange.Find
.Execute FindText:=from_text, ReplaceWith:=to_text, Replace:=2
End With
End With
Next oCell
The following code is intended to update Word bookmarks with formatted data from Excel, however the formatting doesn't come across and unsure why, would appreciate any suggestions. The formatted data is text with certain works underlined.
Set wb = ActiveWorkbook
TodayDate = Format(Date, "mmmm d, yyyy")
Path = wb.Path & "\update_file.docx"
'Create a new Word Session
Set pappWord = CreateObject("Word.Application")
'Open document in word
Set docWord = pappWord.Documents.Add(Path)
'Loop through names in the activeworkbook
For Each xlName In wb.Names
'if xlName's name is existing in document then put the value in place of the bookmark
If docWord.Bookmarks.Exists(xlName.Name) Then
docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName).Text
End If
Next xlName
Try this instead, using Copy and the (poorly-documented) ExecuteMso method. You need to use Copy against the range (in order to capture formatting) and then you can effectively do the same as the right-click Paste + Keep Source Formatting option:
If docWord.Bookmarks.Exists(xlName.Name) Then
xlName.RefersToRange.Copy
docWord.Bookmarks(xlName.Name).Select
docWord.Application.CommandBars.ExecuteMso "PasteSourceFormatting"
End If
Alternatively, and this might be better because ExecuteMso is asynchronous and can result in timing issues:
xlName.RefersToRange.Copy
docWord.Bookmarks(xlName.Name).Range.PasteAndFormat 16 'wdFormatOriginalFormatting
I have an Access db to track metrics and "number crunch" data to build PowerPoint presentations. I do about 40 presentations per month, and they are 98% charts.
I run queries one at a time (using SQL statements), take the resulting data and copy it into an Excel template (I made a mock table in this "template" so the chart is already built and formatted), then copy the chart as a picture into a PowerPoint template.
So there is a lot of manual work.
How can I run multiple queries in Access with VBA on the same dataset/table (I have to do sales by quarter, by month, by region, by state, by site and all of these are Top5 aggregate, hence the reasons for the charts), and then send the resulting data to a specific Excel workbook, while defining what goes into what cell range?
If I get all the data into Excel, and have the charts ready to go, is there some VBA that will take the charts from Excel (activeworksheet) and paste them into PowerPoint as pictures in a quad view layout?
Can I do the same thing with an Access to PowerPoint approach and cut out Excel?
I am a novice at best.
You don't need to use Excel at all ! Use MS Access Charts in a report and some VBA code to put them into Powerpoint directly. There is already an example here
One "gotcha" is if you generate graphs in a group ie you design the report with a graph that is inside a group - so when you run the report you will get numerous graphs created.
It is a bit tricky to get hold of each of these graphs and drop them into Powerpoint but here is some code that will take care of it. This works in Access 2003
'Loop through all the controls in this report and pickout all the graphs
For Each c In pReport.Controls
'Graphs initially appear to be in an Object Frame
If TypeOf c Is ObjectFrame Then
'Check the Class of the object to make sure its a Chart
If Left$(c.Class, 13) = "MSGraph.Chart" Then
'Check if this graph must be cloned (required if the graph is in a group in the MS Access report)
If Not IsGraphToBeCloned(pReport.Name, c.ControlName) Then
InsertGraphToPptSlide c, "", pReport.Name
Else
InsertGraphGroupToPpt pReport.Name, c
End If
End If
End If
Next
This will find all the graphs in the report, if the graph is in a group then we call the InsertGraphGroupToPPt function.
The trick here is that we know we have the same base graph multiple times - but populated with different data. So in Powerpoint what you need to do is paste the base graph into powerpoint slides n times - where n is the number of groups and then update the graphs query properties
eg
Function UpdateGraphInPowerpoint(sql As String, OrigGraph As ObjectFrame, Groups As dao.Recordset, GroupName As String, ReportName As String) As Boolean
//Copyright Innova Associates Ltd, 2009
On Error GoTo ERR_CGFF
On Error GoTo ERR_CGFF
Dim oDataSheet As DataSheet
Dim Graph As Graph.Chart
Dim lRowCnt, lColCnt, lValue As Long, CGFF_FldCnt As Integer
Dim CGFF_Rs As dao.Recordset
Dim CGFF_field As dao.Field
Dim CGFF_PwrPntloaded As Boolean
Dim lheight, lwidth, LLeft, lTop As Single
Dim slidenum As Integer
Dim GraphSQL As String
Dim lGrpPos As Long
'Loop thru groups
Do While Not Groups.EOF
'We want content to be added to the end of the presentation - so find out how many slides we already have
slidenum = gPwrPntPres.Slides.Count
OrigGraph.Action = acOLECopy 'Copy to clipboard
slidenum = slidenum + 1 'Increment the Ppt slide number
gPwrPntPres.Slides.Add slidenum, ppLayoutTitleOnly 'Add a Ppt slide
'On Error Resume Next 'Ignore errors related to Graph caption
gPwrPntPres.Slides(slidenum).Shapes(1).TextFrame.TextRange.Text = ReportName & vbCrLf & "(" & Groups.Fields(0).Value & ")" 'Set slide title to match graph title
gPwrPntPres.Slides(slidenum).Shapes(1).TextFrame.TextRange.Font.Size = 16
gPwrPntPres.Slides(slidenum).Shapes.Paste 'Paste graph into ppt from clipboard
Set Graph = gPwrPntPres.Slides(slidenum).Shapes(2).OLEFormat.Object
Set oDataSheet = Graph.Application.DataSheet ' Set the reference to the datasheet collection.
oDataSheet.Cells.Clear ' Clear the datasheet.
GraphSQL = Replace(sql, "<%WHERE%>", " where " & GroupName & " = '" & Groups.Fields(0).Value & "'")
Set CGFF_Rs = ExecQuery(GraphSQL)
CGFF_FldCnt = 1
' Loop through the fields collection and get the field names.
For Each CGFF_field In CGFF_Rs.Fields
oDataSheet.Cells(1, CGFF_FldCnt).Value = CGFF_Rs.Fields(CGFF_FldCnt - 1).Name
CGFF_FldCnt = CGFF_FldCnt + 1
Next CGFF_field
lRowCnt = 2
' Loop through the recordset.
Do While Not CGFF_Rs.EOF
CGFF_FldCnt = 1
' Put the values for the fields in the datasheet.
For Each CGFF_field In CGFF_Rs.Fields
oDataSheet.Cells(lRowCnt, CGFF_FldCnt).Value = IIf(IsNull(CGFF_field.Value), "", CGFF_field.Value)
CGFF_FldCnt = CGFF_FldCnt + 1
Next CGFF_field
lRowCnt = lRowCnt + 1
CGFF_Rs.MoveNext
Loop
' Update the graph.
Graph.Application.Update
DoEvents
CGFF_Rs.Close
DoEvents
Groups.MoveNext
Loop
UpdateGraphInPowerpoint = True
Exit Function
End Function
Since you are a novice, perhaps you should break the task down into parts and automate the parts one at a time. Each step will provide benefits (i.e. time savings) and you can learn as you go.
It is hard to make specific recommendations based upon lack of specific information (what version etc.). That having been said, perhaps a good first step would be to link the Excel tables to the access queries so that the spreadsheets can auto-update every month and you will not have to cut and paste data from Access into Excel. You can do this linking entirely within Excel.
If you are using Excel 2007 click on "Data" in the Ribbon and then click on "From Access".
What you're asking is a lot of work:
Via VBA you'd have to open Excel (Excel Application manipulation from Access) , update your charts (Range manipulation, Data Update) if you have the rights then I would suggest having your pivot charts connected to the Access data and not pasted into the workbook, nevertheless I've been in enough situations where that was not possible. Then you would have to open your PowerPoint presentation and copy from the Excel to the PowerPoint. I've done all of these and know how much work it can save by creating a macro (via VBA) to do this. It's a lot of code.