Consulting

Results 1 to 15 of 15

Thread: Problem with opening a TXT-file from macro

  1. #1
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    Location

    Problem with opening a TXT-file from macro

    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

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    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
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    Location
    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.
    Last edited by RandomGerman; 03-22-2017 at 03:31 AM. Reason: Typing mistakes

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    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.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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\")
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    Location
    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.

  7. #7
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Just checking both PCs have English language versions?
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  8. #8
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    Location
    German. Both.

  9. #9
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    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"
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  10. #10
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    Location
    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.

  11. #11
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    Location
    Same problem as before. For some strange reason, one machine can't open the txt-file, no matter where it is located.

  12. #12
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    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
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  13. #13
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    Location
    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

  14. #14
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    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
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  15. #15
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •