ajakeway
asked on
VBA Default Directory for GetOpenFilename accessued from other Office program
Context: My goal is to update links between PPT and Excel, and have the user select the new file to link to. We have monthly files with just the numbers changing (eg, table sizes are all fixed).
I'm trying to set the default directory that Excel shows when using the GetOpenFilename. The trick is my code is in PowerPoint, and I am opening the Excel file with the CreateObject("Excel.Applic ation"). The code I tried is here, but when it opens the GetOpenFilename, it goes only to MyDocuments:
Set exl = CreateObject("Excel.Applic ation")
ChDrive Left(ActivePresentation.Pa th, 2)
strNewDrive = Left(ActivePresentation.Pa th, 2)
ChDir ActivePresentation.Path
strNewDir = ActivePresentation.Path
ExcelFileNew = exl.Application.GetOpenFil ename(, , "Select Excel File")
' The rest of the code is a bit clumsy, but attached.
I'm trying to set the default directory that Excel shows when using the GetOpenFilename. The trick is my code is in PowerPoint, and I am opening the Excel file with the CreateObject("Excel.Applic
Set exl = CreateObject("Excel.Applic
ChDrive Left(ActivePresentation.Pa
strNewDrive = Left(ActivePresentation.Pa
ChDir ActivePresentation.Path
strNewDir = ActivePresentation.Path
ExcelFileNew = exl.Application.GetOpenFil
' The rest of the code is a bit clumsy, but attached.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
JSRWilson - thanks! Perfect. Works exactly as I need it to!! To be dynamic as I required (and as our files are saved), I changed this line:
.InitialFileName = "C:\Users\Optiplex\Desktop \" 'to this:
.InitialFileName = ActivePresentation.Path
.InitialFileName = "C:\Users\Optiplex\Desktop
.InitialFileName = ActivePresentation.Path
ASKER
Jamie - I'm not seeing this would work within a simple *.pptm file, but the code looks like it was the right direction too. Thanks for the response.
ASKER
Thought clearly about my end goal and not just the process I *thought* I wanted to follow. Quick and elegent solution.
ASKER
Full code (for posterity)
'PPT Link Updater
'originally from: http://www.ozgrid.com/forum/showthread.php?t=147605
Sub M1()
Dim sld As Slide
Dim sh As Shape
Dim ExcelFileNew
Dim ExcelFileOld As String
Dim intLenFileOld As Integer
Dim intLenFileNew As Integer
Dim exl As Object
Dim strNewDrive, strNewDir As String
Dim fd As FileDialog
Set exl = CreateObject("Excel.Applic ation")
Set fd = PowerPoint.Application.Fil eDialog(ms oFileDialo gOpen)
With fd
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Excel Files", "*.xls,*xlsx,*xlsm"
.InitialFileName = ActivePresentation.Path
If .Show = True Then strFilename = .SelectedItems(1)
End With
'MsgBox "Filename= " & Right(strFilename, Len(strFilename) - InStrRev(strFilename, "\"))
Call stripPath(ExcelFileNew, filenameNew)
For Each sld In ActivePresentation.Slides
sld.Select
For Each sh In sld.Shapes
If sh.Type = msoLinkedOLEObject Then
With sh.LinkFormat
LinkOld = .SourceFullName
Call stripReference(LinkOld, fullpathOld)
Call stripPath(fullpathOld, filenameOld)
LinkNew = ExcelFileNew & Mid(LinkOld, Len(fullpathOld) + 1, 99)
' Only replace it if the file name is similar...
' so "extras" like Cash balances are not replaced by the Trust Fees file
If Left(filenameOld, 25) = Left(filenameNew, 25) Then
.SourceFullName = LinkNew
End If
End With
End If
Next sh
Next sld
ExitMySub:
End Sub
'
Sub stripPath(fullPath, filename)
'This will take c:\folder\workbook.xlsx* and provide workbook.xlsx*
Dim filenamePosition As Long
filenamePosition = InStrRev(fullPath, "\")
filename = Mid(fullPath, filenamePosition + 1, Len(fullPath) - filenamePosition)
End Sub
'
Sub stripReference(fullReferen ce, filename)
'This will take *workbook.xls!Graphs![work book.xls]G raphs Chart 1 and provide *workbook.xls
Dim referencePosition As Long
referencePosition = InStr(1, fullReference, "!")
filename = Left(fullReference, referencePosition - 1)
End Sub
'
Sub showStrings(ExcelFileNew, filenameNew, LinkOld, fullpathOld, filenameOld, LinkNew)
'This is just a debugging function to display the variables
MsgBox ("ExcelFileNew: " & ExcelFileNew & vbNewLine _
& "filenameNew: " & filenameNew & vbNewLine _
& "LinkOld: " & LinkOld & vbNewLine _
& "fullpathOld: " & fullpathOld & vbNewLine _
& "filenameOld: " & filenameOld & vbNewLine _
& "LinkNew: " & LinkNew)
End Sub
'PPT Link Updater
'originally from: http://www.ozgrid.com/forum/showthread.php?t=147605
Sub M1()
Dim sld As Slide
Dim sh As Shape
Dim ExcelFileNew
Dim ExcelFileOld As String
Dim intLenFileOld As Integer
Dim intLenFileNew As Integer
Dim exl As Object
Dim strNewDrive, strNewDir As String
Dim fd As FileDialog
Set exl = CreateObject("Excel.Applic
Set fd = PowerPoint.Application.Fil
With fd
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Excel Files", "*.xls,*xlsx,*xlsm"
.InitialFileName = ActivePresentation.Path
If .Show = True Then strFilename = .SelectedItems(1)
End With
'MsgBox "Filename= " & Right(strFilename, Len(strFilename) - InStrRev(strFilename, "\"))
Call stripPath(ExcelFileNew, filenameNew)
For Each sld In ActivePresentation.Slides
sld.Select
For Each sh In sld.Shapes
If sh.Type = msoLinkedOLEObject Then
With sh.LinkFormat
LinkOld = .SourceFullName
Call stripReference(LinkOld, fullpathOld)
Call stripPath(fullpathOld, filenameOld)
LinkNew = ExcelFileNew & Mid(LinkOld, Len(fullpathOld) + 1, 99)
' Only replace it if the file name is similar...
' so "extras" like Cash balances are not replaced by the Trust Fees file
If Left(filenameOld, 25) = Left(filenameNew, 25) Then
.SourceFullName = LinkNew
End If
End With
End If
Next sh
Next sld
ExitMySub:
End Sub
'
Sub stripPath(fullPath, filename)
'This will take c:\folder\workbook.xlsx* and provide workbook.xlsx*
Dim filenamePosition As Long
filenamePosition = InStrRev(fullPath, "\")
filename = Mid(fullPath, filenamePosition + 1, Len(fullPath) - filenamePosition)
End Sub
'
Sub stripReference(fullReferen
'This will take *workbook.xls!Graphs![work
Dim referencePosition As Long
referencePosition = InStr(1, fullReference, "!")
filename = Left(fullReference, referencePosition - 1)
End Sub
'
Sub showStrings(ExcelFileNew, filenameNew, LinkOld, fullpathOld, filenameOld, LinkNew)
'This is just a debugging function to display the variables
MsgBox ("ExcelFileNew: " & ExcelFileNew & vbNewLine _
& "filenameNew: " & filenameNew & vbNewLine _
& "LinkOld: " & LinkOld & vbNewLine _
& "fullpathOld: " & fullpathOld & vbNewLine _
& "filenameOld: " & filenameOld & vbNewLine _
& "LinkNew: " & LinkNew)
End Sub
Yep I should have pointed out that the "default" was to my own desktop!
Example:
Open in new window
You can then use that path to pre-define the path in your code or within a user folder/file selection dialog box.
You can also see/modify the settings using regedit.exe here:
HKEY_CURRENT_USER\Software