I'm using VBA in powerPoint to update links to Excel Objects in my PowerPoint and everything is working well. The only Issue I have is that sometimes the Select File dialogue box opens behind the active Powerpoint and the only way to select it is to CTRL+ALT+Del and select the Excel File Chooser and set it as active. Is there a way to make it always be the active dialogue box when it opens? Here's the code I'm using:
Sub UpdateLinks()
Dim sld As Slide
Dim sh As Shape
Dim strNms As String
Dim intI As Integer
Dim strNewPath
Dim ExcelFile
Dim exl As Object
Set exl = CreateObject("Excel.Application")
'Set exl = exl.ActiveWindow
'exl.Active = True
'Open a dialog box to promt for the new source file.
ExcelFile = exl.Application.GetOpenFilename(, , "Select Excel File")
'Go through every slide
For Each sld In ActivePresentation.Slides
For Each sh In sld.Shapes
If sh.Type = msoLinkedOLEObject Then
With sh.LinkFormat
strNms = .SourceFullName
intI = InStr(1, strNms, "!")
strNewPath = ExcelFile & Mid(strNms, intI, Len(strNms) - intI + 1)
.SourceFullName = strNewPath
End With
End If
Next sh
Next sld
ActivePresentation.UpdateLinks
End Sub
Thanks.
Suggestion: try this version of a file picker instead:
Sub FileDialogExample()
' Courtesy of John Wilson
' www.pptalchemy.co.uk
Dim fd As FileDialog
Dim sFilename As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Filters.Clear
.Filters.Add "Excel Files", "*.xls, *.xlsx"
.InitialFileName = Environ("USERPROFILE") & "\Desktop\"
.AllowMultiSelect = False
If .Show = True Then sFilename = .SelectedItems(1)
End With
'do whatever with sFilename
MsgBox "You picked " & sFilename
End Sub
Related
I'm trying to set the default directory for the VBA function GetOpenfilename. I managed to get it working before but lost the code before saving it.
Sub Sample2()
Dim myFile As Variant
Dim i As Integer
Dim myApp As Excel.Application
Dim strCurDir As String
Set myApp = New Excel.Application
ChDrive ("H:\")
ChDir ("H:\99 - Temp")
'Open File to search
myFile = myApp.GetOpenFileName(MultiSelect:=True)
If myFile <> False Then
If IsArray(myFile) Then '<~~ If user selects multiple file
For i = LBound(myFile) To UBound(myFile)
Debug.Print myFile(i)
Next i
Else '<~~ If user selects single file
Debug.Print myFile
End If
Else
Exit Sub
End If
End Sub
I tried several variations of this code and the posts I found are very old. It is going to be part of a bigger code in Outlook 2016.
Try the FileDialog property of the Excel object instead...
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
Dim myFile As Variant
With xlApp.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
.ButtonName = "Select"
.Title = "Select File"
.InitialFileName = "H:\99 - Temp\"
If .Show = 0 Then Exit Sub 'user cancelled
For Each myFile In .SelectedItems
Debug.Print myFile
Next myFile
End With
Set xlApp = Nothing
I am trying to Copy information from a tab in file opened via File Dialog and paste it into "ThisWorkbook"
Below is my attempt. I keep getting the error
"object doesn't support this property or method"
on the line in bold font.
Sub UpdateWeeklyJobPrep()
Dim xlFileName As String
Dim fd As Office.FileDialog
Dim source As Workbook
Dim currentwk As Integer
Dim wksheet As String
Dim target As ThisWorkbook
Dim fso As Object
Dim sourcename As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Calc the current fiscal week
currentwk = WorksheetFunction.WeekNum(Now, vbMonday)
wksheet = "FW" & currentwk
With fd
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
If .Show Then
xlFileName = .SelectedItems(1)
Else
Exit Sub
End If
End With
'Opens workbook
Workbooks.Open (xlFileName), ReadOnly:=True
'Get file name from path
Set fso = CreateObject("Scripting.FileSystemObject")
sourcename = fso.GetFileName(xlFileName)
sourcename = Left(sourcename, InStrRev(sourcename, ".") - 1)
'Copy/Paste Code Here
**Workbooks(sourcename).Activate**
Workbooks(sourcename).Worksheets(wksheet).Column("F").Copy
target.Activate
target.Sheets("Data Source").Column("C").PasteSpecial
'close workbook with saving changes
source.Close SaveChanges:=False
Set source = Nothing
End Sub
I think I have a solution. Primarily, as mentioned above in my comment, you should use a variable to hold your new, open workbook.
Sub UpdateWeeklyJobPrep()
Dim xlFileName As String
Dim fd As Office.FileDialog
Dim source As Workbook
Dim currentwk As Integer
Dim wksheet As String
Dim fso As Object
Dim sourcename As String
Dim mainWB As Workbook
Set mainWB = ThisWorkbook
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Calc the current fiscal week
currentwk = WorksheetFunction.WeekNum(Now, vbMonday)
wksheet = "FW" & currentwk
With fd
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
If .Show Then
xlFileName = .SelectedItems(1)
Else
Exit Sub
End If
End With
'Opens workbook
Dim newWB As Workbook
Set newWB = Workbooks.Open(xlFileName, ReadOnly:=True)
'Copy/Paste Code Here
mainWB.Sheets("Data Source").Column("C").Values = newWB.Worksheets(wksheet).Column("F").Values
newWB.Close savechanges:=False
Set newWB = Nothing
End Sub
I also changed the Copy/PasteSpecial bit, assuming you just needed values. Note since you're copying a whole column this might take time. You'd probably instead want to minimize that range to the used rows only, but I'll leave that as an exercise for the reader.
I need some help with some bizzare VBA code behavior in Powerpoint. Purpose is simple - update Excel links on a Powerpoint presentation. I have a presentation with objects linked to an Excel file. On running the code from Powerpoint, a user is prompted to select the source Excel file on the harddrive, and the location of this Excel file is used to replace the previous location of the Excel file, already saved in the PowerPoint presentation.
You run the macro, check the links, their path is updated. You click save, close the presentation. You open the presentation and all is good.
Now let’s say you change the name of the Excel file. You run the macro, check the links, their path is updated. You click save, close the presentation. You open the presentation and ONLY HALF THE LINKS ARE UPDATED. Could somebody take a look? Thanks!
Private Sub CommandButton1_Click()
Dim xlApp As Object
Dim xlWorkBook As Object
Dim pptSlide As Slide
Dim pptShape As Shape
Dim oldString, tempString, newString As String
Dim intLength As Integer
Dim sPath As String
Dim ExcelFileName As String
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
' Set the title of the dialog box.
.Title = "Please select the file to update links in the presentation"
' Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "Excel Workbook", "*.xlsx"
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
newString = .SelectedItems(1) 'replace txtFileName with your textbox
End If
End With
'show "macro running" screen
UserForm1.Show False
'open excel file with links
Set xlApp = CreateObject("Excel.Application")
Set xlWorkBook = xlApp.Workbooks.Open(newString, True, False)
'grab old full path to replace link in objects
For Each pptSlide In ActivePresentation.Slides
For Each pptShape In pptSlide.Shapes
If pptShape.Type = msoLinkedOLEObject Then
tempString = pptShape.LinkFormat.SourceFullName
intLength = InStr(tempString, "!")
oldString = Mid(tempString, 1, intLength - 1)
GoTo 1
End If
If pptShape.Type = msoChart Then
oldString = pptShape.LinkFormat.SourceFullName
GoTo 1
End If
Next pptShape
Next pptSlide
1
'replace old full path to new full path
For Each pptSlide In ActivePresentation.Slides
For Each pptShape In pptSlide.Shapes
If pptShape.Type = msoLinkedOLEObject Or pptShape.Type = msoChart Then
With pptShape.LinkFormat
If InStr(1, UCase(.SourceFullName), UCase(oldString)) Then
.SourceFullName = Replace(.SourceFullName, oldString, newString)
End If
End With
pptShape.LinkFormat.Update
End If
'DoEvents
Next pptShape
'DoEvents
Next pptSlide
'close excel file with links
xlWorkBook.Close (False)
xlApp.Quit
Set xlApp = Nothing
Set xlWorkBook = Nothing
'hide "macro running" screen
UserForm1.Hide
End Sub
I am trying to copy a sheet from one file and then paste it to an established tab in about 6 files in an established folder. I have this code, but it only works for the first file in the folder. It is also creating a blank workbook for some reason. Any suggestions?
Sub LoopThroughFiles()
Dim wbk As Workbook
Dim Filename As String
Dim FirstFile As String
Dim FileDirectory As String
Dim x As Workbook
Set x = Workbooks.Open("test.xlsx")
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.AllowMultiSelect = False
.Show
If .SelectedItems.Count = 0 Then
MsgBox "You did not select a folder"
Exit Sub
Else
FileDirectory = .SelectedItems(1) & "\"
End If
End With
Set wbk = Workbooks.Add
Filename = Dir(FileDirectory)
FirstFile = Filename
Do Until Filename = ""
Dim new_wb As Workbook
Set new_wb = Workbooks.Open(FileDirectory & Filename)
If FirstFile = Filename Then
x.Sheets("report").UsedRange.Copy
new_wb.Sheets("roster").Range("a1").PasteSpecial
End If
new_wb.Close savechanges:=True
Filename = Dir
Loop
MsgBox "All store totals have been added"
End Sub
Sub LoopThroughFiles_Paste_Roster()
Dim wbk As Workbook 'New workbook the data is added to
Dim Filename As String
Dim FirstFile As String
Dim FileDirectory As String
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("Copy Doc 1")
Set y = Workbooks.Open("Copy Doc 2")
'display the folder picker dialog box so user can select a folder
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.AllowMultiSelect = False
.Show
If .SelectedItems.Count = 0 Then
MsgBox "You did not select a folder"
Exit Sub
Else
FileDirectory = .SelectedItems(1) & "\"
End If
End With
'retrieve the name of the first file in the folder using Dir
Filename = Dir(FileDirectory)
FirstFile = Filename
'Loop through all the files in the folder
'open the file
Do Until Filename = ""
Set wbk = Workbooks.Open(FileDirectory & Filename, UpdateLinks:=False, Password:="Password123")
With wbk
x.Sheets("report").UsedRange.Copy
wbk.Sheets("roster").Range("a1").PasteSpecial
y.Sheets("Setup").UsedRange.Copy
wbk.Sheets("PTO Taken and Req").Range("a1").PasteSpecial
End With
'save and close the file
'get the next file in the folder
wbk.Close savechanges:=True
Filename = Dir
Loop
MsgBox "All pages have been updated"
End Sub
I've written some Word VBA which takes an Excel file and updates Labels (ActiveX Control) in the Word file. The only thing is this Excel file will change path and filename each month. Instead of editing 2 variables each month, how do I add an Open File dialog box so the user selects the Excel file to be used?
Here is what I have now:
Sub Update()
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
PathWork = "C:\My Documents\2015-05 Report\"
CalcFile = "May2015-data.xlsx"
Set exWb=objExcel.Workbooks.Open(FileName:=PathWork & CalcFile)
ThisDocument.date.Caption=exWb.Sheets("Data").Cells(1,1)
End Sub
Here is a simplified macro which will allow the user to select only Macro-Enabled Excels. I couldn't comment on the previous answer as I have not earned enough reputation to comment on an answer. Please mind it.
Public Sub GetCaptionFromExcel()
Dim objExcel As New Excel.Application, exWb As Workbook
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select Macro-Enabled Excel Files"
.Filters.Add "Macro-Enabled Excel Files", "*.xlsm", 1
If .Show <> -1 Then Exit Sub
Set exWb = objExcel.Workbooks.Open(.SelectedItems(1))
'*** Use the values from excel here***
MsgBox exWb.Sheets("Data").Cells(1, 1)
'*** Close the opened Excel file
exWb.Close
End With
End Sub
You could try something like this
Replace PathWork and CalcFile with Dialogbox
With Dialogs(wdDialogFileOpen)
If .Display Then
If .Name <> "" Then
Set exWb = Workbooks.Open(.Name)
sPath = exWb.Path
End If
Else
MsgBox "No file selected"
End If
End With
Complete CODE should look like this
Option Explicit
Sub Update()
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
Dim sPath As String
'// Dialog box here to select excel file
With Dialogs(wdDialogFileOpen)
If .Display Then
If .Name <> "" Then
Set exWb = Workbooks.Open(.Name)
sPath = exWb.Path
End If
Set exWb = objExcel.Workbooks.Open(FileName:=sPath)
ActiveDocument.Date.Caption = exWb.Sheets("Data").Cells(1, 1)
Else
MsgBox "No file selected"
End If
End With
Set objExcel = Nothing
Set exWb = Nothing
End Sub