VBA code to change linked picture image name , within Powerpoint - vba

I have a Powerpoint with images inserted using Link and Insert.
I need to change the file names that they link to.
I haven't got access to the source folder.
Therefore need to make the change to via VBA.
I can change the folder location, just not the name of the file.
All that needs changing is the 'Doncaster' to 'London'
X:\Central\Buildings\District\Images\Forecast_Doncaster.png
X:\Central\Buildings\District\Images\History_Doncaster.png
X:\Central\Buildings\District\Images\Current_Doncaster.png
I tried but it doesn't work
submarket ="London"
sh.TextFrame.TextRange = Replace(sh.TextFrame.TextRange, "Doncaster", submarket
Can anyone please help?

The code you posted will change the text in the shape but not the link to a picture. For that you need something more like this:
Sub Test()
Call EditLink(ActiveWindow.Selection.ShapeRange(1), ".png", ".jpg")
End Sub
Sub EditLink(oSh As Shape, strOriginalText As String, strNewText As String)
With oSh.LinkFormat
.SourceFullName = Replace(.SourceFullName, strOriginalText, strNewText)
End With
End Sub
In this example, I'm changing the link to point to a JPG of the same name as the original file which is a PNG. Modify as needed to change directory names for your project

A slight amendment to Steve's answer which I used and it worked! thank you.
ActiveWindow.View.GotoSlide s.SlideIndex
Call EditLink(sh, "Doncaster", "London")

Related

How to change default path in save on close prompt?

I'm trying to create a template that automatically changes folder suggested by the save prompt to a specified location. I've managed to get it partially working using the following code (modified from here):
Sub FileSave()
Dim UserSaveDialog As Dialog
Set UserSaveDialog = Dialogs(wdDialogFileSaveAs)
'save changes if doc has been saved previously
If ActiveDocument.Path <> "" Then
ActiveDocument.Save
Exit Sub
End If
With UserSaveDialog
.Name = "C:\Users\david\Downloads"
If .Display Then
UserSaveDialog.Execute
End If
End With
End Sub
Using this code, my macro correctly intercepts the default save behaviour (or Ctrl+S), however it doesn't intercept the save dialog when closing the file. I've tried basically copying this code to a new Sub called Document_BeforeSave, FileExit, FileCloseEx and FileCloseAllEx (yes, I am having difficulty with all the different objects and what they do :) all to no avail.
I'm not sure the same code will even work in this event, but I don't even get any indication that it has failed to work, so it seems I'm using the wrong event.
Turns out I somehow missed AutoClose (MS Docs), which does what I want.

Activate window of another Visio instance

Currently I have a file name stored in string called filename. The file stored in the string is currently open. Issue is, this file could some times be opened in another instance of Visio.
I want to activate the file that is stored in filename string
My current method does not capture this - The code below only checks if the filename exists among the current/one instance of Visio.
For Each objDoc In objVisio.Documents
If objDoc.Name = filename Then
objDoc.activate
Exit for
End If
Next
How can I activate this file to bring it forward?
windows(filename & " - Microsoft Visio").activate
is not working either
I've tried
Dim objVisio as Visio.Application
Set objVisio = GetObject(filename).Application
which isn't working (maybe due to filename string only having the file name and not the entire file path as well)
Any other brute force methods available out there?
Any help is appreciated!
Try something like this:
objVisio.Application.Caption
Or
AppActivate "Microsoft Visio"
I guess another option is to look into this: https://msdn.microsoft.com/en-us/library/office/ff766749.aspx
I haven't worked extensively with Visio in VBA, so I am interested to see the true answer here.

I have the file name open...Now how do I actually get the file itself to open?

I have a code that allows me to use an open file dialog to select a file. Once selected, the file path displays in Form3. What I'm trying to do from there is click Command_Button1 (Open) and physically open the file.
I can provide codes and JPEGs...but won't cloud this up until it is necessary :) I'm not using Common Dialog so the code is somewhat lengthy.
I looked online for a few hours yesterday and the closest I got was how to get it to open an Excel file...but this isn't Excel.
P.S. I'm new to VBA...so if this isn't as simple as a few lines of code and I'm missing the difficulty of it, please let me know. It seems like it should be a simple enough process...
Edit 1:
#mehow, Do you think it could be because the whole path name isn't displaying? I double clicked Open and nothing happened like you said
If you already know the path then..
Private Sub CommandButton1_Click()
dim myPath as String
myPath = "C:\files\file1.exe"
Dim shell As Object
Set shell = CreateObject("Shell.Application")
shell.Open myPath
End Sub

FORMTEXT Appearing in Word File Name Generated from Protected Form-Field Value and BookMark for VB Script

I am using the following VB Script in a Word 2010 Doc saved as a Microsoft Word Macro-Enabled Template that is protected for form fields:
Sub SaveWithBkMarkedText()
'This code saves the Word file using the bookmark value for Maintenance Memo.
'The file is also saved to a folder in KnowHow for files related to this template.
Dim FileName As String
FileName = ActiveDocument.Bookmarks("mmn").Range.Text
'Use the C:\ code when saving the file locally
ActiveDocument.SaveAs "C:\Download\TemplatesFolders\" & FileName & ".doc"
MsgBox "Your Draft has been saved to KnowHow's Release Documentation site." & _
&vbCrLf & "The file name uses the MM that you included earlier: " & FileName, _
vbInformation + vbOKOnly, "Draft Saved to Minerva"
End Sub
The value entered into the Form Field for a FORMTEXT legacy-form object uses the Bookmark as the file name. Example, if the user enters 12345 as the value, the file is saved using this value as the filename: 12345.doc. This worked fine until a week ago when the filename is now being Prefixed with FORMTEXT 12345.doc. I have tried using this same VB script in older versions of Word on a different machine, and created from a NEW Template with the script added in from scratch, and the same issue is appearing on that machine as well. Prior to this, I was able to update my template with NO problem, but now I can't update this one FORMTEXT field without it affecting the whole file. I can update any other FORMTEXT in the template that does not use the Bookmark value as the file name, and it works. Also, I have tried changing the Bookmark Reference to another FORMTEXT object, as well as saving the file as a .DOCX and the same problem occurs regardless. What is causing the FORMTEXT to appear in the filename?
You have to un-protect the document (template) and then make the VBA programming. Once it is done, then you can protect it again (for filling forms) and you will not see the "FORMTEXT" in the filename when you run the macro.
Hope it helps.
To the OP, did you resolve this issue? I'm now having the same problem, I'm using form field text with bookmarks and using VB.net to get the bookmark.text which is now prefixed with FORMTEXT, just using a bookmark on it own and its OK, no prefix. I'm going to try and remove the first 9 characters from the result using code, a workaround, yes but it might work.
Know this is an old thread but ran into same issue. As a workaround...
Replace FORMTEXT with null "". In OP circumstance:
Dim FileName As String
FileName = ActiveDocument.Bookmarks("mmn").Range.Text
FileName = Replace(FileName, "FORMTEXT ", "")
Not a "fix" for the issue or elegant but it works.
Had the same issue. Simply delete current bookmark and add a new one. If that does not work than instead of using the following:
FileName = ActiveDocument.Bookmarks("mmn").Range.Text
try using:
Selection.GoTo What:=wdGoToBookmark, Name:="mmn"
Filename = Selection.Text

PPT2010 insert a imge can't get the original filename

In MS-Powerpoint 2010, insert an image which has a name ABC.gif.
When I use the code to read the .pptx file, I can't get the original image's file name which should be ABC.gif, but instead I get "Image1.gif.
Can someone tell me how can I get the original image's fileName ABC.gif.
Using Microsoft.Office.Interop.PowerPoint, for the code operate.
Not sure if you are using VB.Net or C# so I will explain it using VBA code. You can easily adapt it to your code.
To get the filename you have to ensure that you do not just "Insert" the image but "Insert And Link" See the below screenshot.
Remember MS-PP will rename the picture to "Picture 1, Picture 2" etc in this case as well but then you can always use LinkFormat.SourceFullName to get the full Filename + Path. And once you have that, you can easily retrieve the file name.
See this example
Option Explicit
Sub Sample()
Dim img As Shape
For Each img In ActivePresentation.Slides(1).Shapes
'~~> Type is 11 when you insert and link an image
If img.Type = msoLinkedPicture Then
Debug.Print img.LinkFormat.SourceFullName
Debug.Print img.Name
End If
Next
End Sub
SCREENSHOT