Log in

View Full Version : Problem with opening a TXT-file from macro



RandomGerman
03-21-2017, 12:10 PM
Hi all,

I have a problem, which is really confusing me, as the code is working well on one machine and not working on a second. At last, I don't really think it is a problem of the code, but we'll see. Maybe our gurus Show me I'm wrong - wouldn't be the first time. ;)

What the macro should do: Open a hidden text file (.txt) - read the path written inside this text file - follow that path - open a presentation hidden at the end of this path - copy slide 4 from this hidden presentation - close it - close the text file - paste slide 4 into the active presentation.


Sub PasteInSlide4()
Dim src As Presentation
Dim strpath1 As String
Dim strpath2 As String
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String

strpath1 = Environ("USERPROFILE") & "\AppData\Roaming\Microsoft\AddIns\"

FilePath = strpath1 & "System_File_Location.txt"

TextFile = FreeFile

Open FilePath For Input As TextFile

FileContent = Input(LOF(TextFile), TextFile)

strpath2 = FileContent

Set src = Application.Presentations.Open(strpath2)

src.Slides(4).Copy

src.Close

Close TextFile

ActivePresentation.Slides.Paste (ActiveWindow.View.Slide.SlideIndex)
ActiveWindow.View.GotoSlide (ActiveWindow.View.Slide.SlideIndex - 1)

End Sub


It works fine on one machine and it does not on another machine. It stops working quite early on the second machine, leaving the run time error "PowerPoint could not open the file". At first, I didn't know, if "the file" is the txt or the hidden PowerPoint file (src in the code). I tested opening the hidden PowerPoint file directly, leaving out the txt, which worked, so the problem seems to be the text file. Now the confusing matter: Both machines run Windows 7, both machines have PPT2007, the name of the txt-file is the same, the path/location is the same, and admin rights and the security settings of both machines are the same, too.

Any ideas, what else I could check? There must be a difference somewhere on the two machines making one able to open the txt out of the macro and the other one not. But where? What else should I compare?

I'm happy about any suggestion.

Thank you
RG

John Wilson
03-22-2017, 01:13 AM
I wouldn't use LOF (length of file)

See if this is more reliable


Dim src As Presentation

Dim strpath1 As String
Dim strpath2 As String
Dim intFileNum As Integer
Dim strFilePath As String

strpath1 = Environ("USERPROFILE") & "\AppData\Roaming\Microsoft\AddIns\"
strFilePath = strpath1 & "System_File_Location.txt"
intFileNum = FreeFile
Open strFilePath For Input As intFileNum
Line Input #intFileNum, strpath2
MsgBox strpath2
' etc

RandomGerman
03-22-2017, 03:29 AM
Thank you, John. I'm sad, it is still the same. :( Your version works fine on the same machine as my first try and doesn't work on the other machine. The same error message appears.

John Wilson
03-22-2017, 11:00 AM
Are you sure the file is named correctly and in that location.

This macro will check


Sub isIt_There()

Dim fso As Object
Dim strpath
strpath = Environ("APPDATA") & "\Microsoft\AddIns\System_File_Location.txt"
Set fso = CreateObject("scripting.filesystemobject")
MsgBox fso.fileexists(strpath)
End Sub

If you are sure it's present and the macro returns False it usually means you do not have permission to access that location. Not likely though.

Paul_Hossler
03-22-2017, 07:05 PM
Any chance that USERPROFILE on the non-working machine has spaces in it?

On that machine, what is strpath1? (from Environ("USERPROFILE") & "\AppData\Roaming\Microsoft\AddIns\")

RandomGerman
03-24-2017, 02:55 AM
Thank you, John and Paul!

Both suggestions brought no new result.

@Paul: There is no space in USERPROFILE and it shouldn't be a problem with the command Environ("USERPROFILE") anyway, as I also used it, when I directly opened the hidden presentation, just to verify it is the TXT, PowerPoint can't open, not the PPTX.

@John: Your macro responded TRUE, which means, as far as I understand, we have no problem with access and file name and loccation are correct.

John Wilson
03-24-2017, 11:48 AM
Just checking both PCs have English language versions?

RandomGerman
03-25-2017, 01:23 AM
German. Both.

John Wilson
03-25-2017, 05:14 AM
Is the folder "AddIns" not normally called something else in German?

I would try creating a folder "MyFiles" in ....\AppData\ Roaming and access it with Environ("APPDATA") & "\MyFiles\nameoffile.txt"

RandomGerman
03-26-2017, 06:12 AM
No, it is the same folder name in German - and otherwise it should not have worked on one machine, I think. I'm not optimistic, that another folder could do better, as everything went well, when I left out the TXT and directly opened up the hidden pptx from the macro, because I stored the hidden pptx in the same Folder. But I will try.

RandomGerman
03-31-2017, 01:47 AM
Same problem as before. For some strange reason, one machine can't open the txt-file, no matter where it is located.

John Wilson
03-31-2017, 09:25 AM
That is certainly strange!

Does this work?


Sub chexText()
Dim FSO As Object
Dim strText As String
Dim oTextStream As Object
Const fsoForReading = 1
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oTextStream = FSO.OpenTextFile(Environ("APPDATA") & "\Microsoft\AddIns\System_File_Location.txt", fsoForReading)
strText = oTextStream.ReadAll
MsgBox strText
oTextStream.Close
End Sub

RandomGerman
04-04-2017, 07:29 AM
Thank you, John - this time the result is close to it: The path shown in the message box is correct - the only thing I'm not sure about, are three symbols appearing in front of the file path name: Í»¿. I have no idea, where they come from and if they will disturb the process of finding the wanted presentation.

To try out I combined your code with what we had before. Correct?


Sub PasteInSlide4()
Dim FSO As Object
Dim strText As String
Dim oTextStream As Object
Dim src As Presentation

Const fsoForReading = 1
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oTextStream = FSO.OpenTextFile(Environ("APPDATA") & "\Microsoft\AddIns\System_File_Location.txt", fsoForReading)
strText = oTextStream.ReadAll
Set src = Application.Presentations.Open(strText)
src.Slides(4).Copy
src.Close
oTextStream.Close
ActivePresentation.Slides.Paste (ActiveWindow.View.Slide.SlideIndex)
ActiveWindow.View.GotoSlide (ActiveWindow.View.Slide.SlideIndex - 1)
End Sub

John Wilson
04-05-2017, 01:27 AM
Interesting and maybe the reason it fails on some PCs. Can you email me the System_File_Location.txt file itself?

john ATSIGN pptalchemy.co.uk

RandomGerman
04-06-2017, 10:32 AM
It looks like the strange symbols in deed were the troublemakers. We don't know how they appeared, but it seems they were an invisible part of the text file. We deleted the whole file, created a new one (same name, same location), but this time we typed the path letter by letter manually, instead of copying it from the explorer. And now the symbols don't appear in your testing macro anymore, and the original code works well. So, my theory is, that somehow the symbols came into the txt file when the path was pasted into it the first time, and remained there, when we changed the path later. However, now everything works fine. Sometimes the slow way (typing) might be the better one.

John, thank you very much for all your thoughts and your help.