I have a powerpoint presentation with a slide that has several labels on it. I need the value of the labels to be pulled from an excel-sheet that is located in the same folder as the powerpoint presentation file.
The code I use to populate the labels so far is this:
Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
If Wn.View.CurrentShowPosition = 1 Then
'load variable values?
Dim xlApp As Object
Dim xlWorkBook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlWorkBook = xlApp.Workbooks.Open(ActivePresentation.Path & "\QSheet.xlsx", True, False)
BrownRetail = xlWorkBook.Sheets(1).Range("B3").Value
lblBrownTruckRetail.Caption = "$" & CCur(BrownRetail)
Set xlApp = Nothing
Set xlWorkBook = Nothing
End If
End Sub
This works, except it is quite slow loading (10-15 seconds to start the presentation) and the labels show with a white background instead of being transparent. Also, it seems odd to me that the label values stay persistent even in design view.
Is there some better way to pull a value to populate a label from an excel-sheet?
Related
I want to link a "TimePicker" housed on an Excel macro enabled spreadsheet (it has three textboxes for hrs, min, AM/PM with associated SpinnerButtons [SBs] operating with SB_Change events code) to an unbound Objectframe (OLE1) of an Access form. The user will select the time settings which will then be transferred to the appropriate Access form textbox. I'm using a command button click routine to perform the link to the Objectframe. Ideally I would like to use the TimePicker within OLE1. On running the code the textboxes appear in OLE1, but not the SBs. Also the code quits running as soon as it hits the .Action line.
I like the idea of using the OLE1 object, but it is such a "black box" with all the different properties specified with only strings. What am I missing? Thank you for any and all suggestions. The code:
Private Sub GetTimePicker_Click()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim obOLE1 As ObjectFrame
Set xlApp = CreateObject("Excel.Application")
Set xlWB = xlApp.Workbooks.Open(" [filepath] \TimePicker2.xlsm") ' specify file
Set obOLE1 = Me.OLE1
xlApp.Visible = True
With obOLE1
.SourceDoc = " [filepath] /TimePicker2.xlsm"
.Class = "Excel.SheetMacroEnabled.12"
.OLETypeAllowed = acOLEEither
.AutoActivate = 0 'vbOLEActivateManual
.SetFocus
.SourceItem = "Shapes.Range(Array('txtHr', 'txtMin', 'txtAPM', 'SP1', 'SP2', 'SP3', 'Label1'))"
.Verb = acOLEVerbOpen
.Action = acOLELinked
End With
End Sub
Using MS Word (in my case 2010 version), I have constructed a form with Content Control elements to be filled out by the user. Now I want certain entries (that I already gave titles to) be shown in a chart inside the same Word document (not in a separate Excel document).
This should be an automated process, so that if the user changes one of the Content Control entries, the chart updates itself automatically; I would also be OK if the user had to press a button in order to update the chart (but the user shouldn't have to click around a lot, since I must assume the user to have little skills.)
So I inserted an Excel chart object in my Word form document. I also wrote some VBA code inside this Excel object to read the Content Control values from the Word document as source for the chart. But I think what I really need is the VBA code to be in my Word document itself (for example to be executed upon click on a button by the user), yet I don't know how to address the Excel chart object and the cells within.
My VBA code inside the Excel object is:
Sub ChartDataAcquirer()
Dim wdApp As Object
Dim wdDoc As Object
Dim DocName As String
Dim ccX As String
Dim ccY As String
Dim datapairs As Integer
'''''''''' Variables '''''''''
DocName = "wordform.docm"
ccX = "titleX"
ccY = "titleY"
datapairs = 5
''''''''''''''''''''''''''''''
Set wdApp = GetObject(, "Word.Application")
Set wdDoc = wdApp.Documents(DocName)
Dim i As Integer
For i = 1 To datapairs
With ActiveSheet.Cells(i + 1, 1) ' The first row contains headline, therefore i+1
.Value = wdDoc.SelectContentControlsByTitle(ccX & i).Item(1).Range.Text ' The CC objects containing the x values have titles "titleX1", "titleX2" ..., therefore "ccX & i"
On Error Resume Next
.Value = CSng(wdDoc.SelectContentControlsByTitle(ccX & i).Item(1).Range.Text) ' To transform text into numbers, if user filled the CC object with numbers (which he should do)
End With
With ActiveSheet.Cells(i + 1, 2)
.Value = wdDoc.SelectContentControlsByTitle(ccY & i).Item(1).Range.Text
On Error Resume Next
.Value = CSng(wdDoc.SelectContentControlsByTitle(ccY & i).Item(1).Range.Text)
End With
Next
End Sub
I guess I need a similar code that is placed in and operates from the Word form document itself, but that is where I am stuck...
The following is demo code that shows how to access an embedded Excel chart.
Note that the Name (Shapes([indexValue])) of your chart Shape is probably different than in this code. You'll need to check and change that assignment. Also, your chart may be an InlineShape rather than a Shape, so you may need to adjust that bit, as well.
This code checks whether the Shape is actually a chart. If it is, the Chart object is accessed as well as its data sheet. Via that, it's possible to get the actual workbook, the worksheets, even the Excel application if you should need it.
Sub EditChartData()
Dim doc As Word.Document
Dim shp As Word.Shape
Dim cht As Word.Chart
Dim wb As Excel.Workbook, ws As Excel.Worksheet, xlApp As Excel.Application
Set doc = ActiveDocument
Set shp = doc.Shapes("MyChart")
If shp.HasChart Then
Set cht = shp.Chart
cht.ChartData.Activate
Set wb = cht.ChartData.Workbook
Set xlApp = wb.Application
Set ws = wb.ActiveSheet
Debug.Print ws.Cells(1, 2).Value2
End If
Set ws = Nothing
Set wb = Nothing
Set cht = Nothing
Set xlApp = Nothing
End Sub
In my database, I have a VBA script that opens an Excel spreadsheet and removes the first row so the new first row is the header row.
When I save the file in Access VBA and open the file in Excel, nothing is there.
If I reimport the spreadsheet, the data is visible.
I changed my script around and if I don't save the changes the file is ok. If I save the changes then this problem appears.
dim sheetpath as string
dim xl as excel.application
dim xlbook as excel.workbook
dim xlsheet as excel.worksheet
sheetpath = "c:\users\me\export.xlsx"
set xl = createobject("Excel.Application")
set xlbook = GetObject(sheetpath)
xl.visible = true
set xlsheet = xlbook.Worksheets(1)
If xlsheet.Range("a1").mergecells = true then
xlsheet.cells.unmerge
end if
if xlsheet.range("a1") = "Values" then
xlsheet.rows(1).delete
end if
xlbook.close savechanges:=true
xl.application.qit
set xl = nothing
set xlbook = nothing
set xlsheet = nothing
Try a reboot.
But before you do that, check in Task Manager / Process Explorer, if you have multiple Excel processes still running.
Instead of
set xl = createobject("Excel.Application")
set xlbook = GetObject(sheetpath)
do
Set xlbook = GetObject(sheetpath)
Set xl = xlbook.Application
What's this?
xl.application.qit
It should be
xl.Quit
And clear your references in the correct order, from bottom to top:
set xlsheet = nothing
set xlbook = nothing
set xl = nothing
I need to find a way to extract chart data labels from a PowerPoint chart to Excel, as many times the PowerPoint chart given to me has it's linked data broken.
I wrote the code below, but I have no clue what to do after For Each datapoint In chtnow.SeriesCollection(1).Points...
Sub Extract_Datalabels()
'Goal: To extract datalabels of Chart's series collection and write to excel
Dim datapoint As Point
Dim sh As Shape
Dim sld As Slide
Dim chtnow As Chart
Dim label As DataLabel
Dim xlApp As New Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlworksheet As Excel.Worksheet
Set xlWorkbook = xlApp.Workbooks.Add
Set xlworksheet = xlWorksheets.Add
xlApp.Visible = True
Set chtnow = ActiveWindow.Selection.ShapeRange(1).Chart
For Each datapoint In chtnow.SeriesCollection(1).Points
'Extract data labels
If datapoint.HasDataLabel Then
[No clue how to write to Excel]
End If
Next
End Sub
If everything else works ok with the code, this is an easy way to write to the first column of the xlworksheet in excel:
Dim cnt As Long
If datapoint.HasDataLabel Then
cnt = cnt + 1
xlworksheet.Cells(cnt, 1) = datapoint.label
End If
However, I am not sure that after setting xlApp.Visible = True you would be allowed to do something like this Set chtnow = ActiveWindow.Selection.ShapeRange(1).Chart.
You had a couple of type errors with your example, but this should get the job done for you. You will need to add a reference to the Microsoft Excel [A Number] Object Library in order to use the Excel object type and all derivatives.
All testing was done using a bar chart.
Sub Extract_Datalabels()
''Goal: To extract datalabels of Chart's series collection and write to excel
Dim datapoint As ChartPoint
Dim chtnow As Chart
Dim xlApp As New Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlworksheet As Excel.Worksheet
Dim Row As Long
Let xlApp.SheetsInNewWorkbook = 1
Set xlWorkbook = xlApp.Workbooks.Add
Set xlworksheet = xlWorkbook.Worksheets(1)
Let xlApp.Visible = True
Call VBA.DoEvents
Set chtnow = ActiveWindow.Selection.ShapeRange(1).Chart
Let Row = 1
For Each datapoint In chtnow.SeriesCollection(1).Points
'Extract data labels
If datapoint.HasDataLabel Then
Let xlworksheet.Cells(Row, 1) = datapoint.DataLabel.Text
End If
Let Row = Row + 1
Next
End Sub
So for background, I get a excel workbook from another department that is full of a information for a specific account that I then take and use certain parts to create graphs in word. Is there a way I could create a macro what would grab the data from Ex. C22:H34, put it into a template word document and possibly auto populate the graphs as well? I want to make it a process that I can hand off to other people to do, so the simpler it is to execute, the better.
Here is what I have
Sub AutoNew()
'
' AutoNew Macro
'
'
Dim xlapp As Object
Dim xlbook As Object
Dim xlsheet As Object
Dim bstartApp As Boolean
Dim i As Long
On Error Resume Next
Set xlapp = GetObject(, "Excel.Application")
If Err Then
bstartApp = True
Set xlapp = CreateObject("Excel.Application")
End If
On Error GoTo 0
Set xlbook = xlapp.Workbooks.Open("C:\Users\MattsonC\Documents\work\Copy of
3202_2018_Renewal Rate Workbook v2 EDIT.xlsx")
Set xlsheet = xlbook.Sheets(1)
With xlsheet.Range("A1")
For i = 1 To .CurrentRegion.Rows.Count - 1
ActiveDocument.Variables(.Offset(i, 0)).Value = .Offset(i, 2)
Next i
End With
xlbook.Close
If bstartApp = True Then
xlapp.Quit
End If
Set xlapp = Nothing
Set xlbook = Nothing
Set xlsheet = Nothing
ActiveDocument.Range.Fields.Update
End Sub
I have variables done like {DOCVARIABLE LLY} in all the places I want data to go in the template, and renamed the cells in excel with the same variable name.
I can't get any output to happen in my Word document, any suggestions?
Thank you!
go back to the beginning
insert a document variable in a new word document using following sequence (word 2016)
insert tab … text … quick parts … field … categories: document automation … field names: docVariable … put in variable name xxxx
then run this code
Sub aaa()
'ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes ' toggle field code view
Stop
ActiveWindow.View.ShowFieldCodes = True
Stop
ActiveWindow.View.ShowFieldCodes = False
ActiveDocument.Variables("xxxx").Value = "abc123"
ActiveDocument.Range.Fields.Update
Stop
ActiveDocument.Variables("xxxx") = "xyz987"
ActiveDocument.Fields.Update
End Sub
if that works, then use the code with the document that you are having trouble with and figure out if your field names are what you think they are