Microsoft Office Forums Read text in a file

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-22-2018, 08:16 AM
Firemaster Firemaster is offline Read text in a file Windows 7 64bit Read text in a file Office 2010
Novice
Read text in a file
 
Join Date: Nov 2018
Posts: 2
Firemaster is on a distinguished road
Default Read text in a file

Good afternoon all, I have posted this elsewhere but think i got a bit lost on that site so i thought i would ask here. I am not a VBA coder at all but need to cokmplete a small task. i have 2 scripts the info from is used in the second. So the first piece of the script is to read the contents of a text file lets start there:

So hopefully this will read a file in the location whcih consists of just a few letters and numbers. Then this will be available to use in the second part of the script. Is there anyway of showing that it has read the file contents like temporarily adding a message box to prove it has read the txt file. At the moment it faills over on the 'open the text file

Function TextFile_PullData()
'PURPOSE: Send All Data From Text File To A String Variable
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
Dim strUser As string

' get the current user name
strUser = CreateObject("WScript.Network").UserName
'or use strUser = CreateObject("WScript.Shell").ExpandEnvironmentStr ings("%USERNAME%")

'File Path of Text File
FilePath = "C:\Users\" & strUser & "\Temp\VFile.txt"

'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile

'Open the text file
Open FilePath For Input As TextFile

'Store file content inside a variable
FileContent = Input(LOF(TextFile), TextFile)

'Close Text File
Close TextFile

'Report Out Text File Contents
MsgBox FileContent

'have the function return the data to the calling code
TextFile_PullData = FileContent
End Function

Second part it takes the Information and adds it to this script in the TextFile_PullData

Sub UpdateSubject() Dim SaveCode As String Dim KeyWord As String Dim objItem As MailItem

KeyWord = "TSD"

SaveCode = TextFile_PullData
Set objItem = GetCurrentItem()
objItem.Subject = "[" + KeyWord + "=" + SaveCode + "] " + objItem.Subject
End Sub

Function GetCurrentItem() As Object Dim objApp As Outlook.Application

Set objApp = Application


On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select

Set objApp = Nothing
End Function
Reply With Quote
  #2  
Old 11-22-2018, 09:31 PM
gmayor's Avatar
gmayor gmayor is offline Read text in a file Windows 10 Read text in a file Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,024
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

It is not clear what the relevance of the keyword is or what the content of the textfile is, but the following provides the additional error trapping you need.
Code:
Option Explicit

 Function TextFile_PullData() As String
'Graham Mayor - https://www.gmayor.com - Last updated - 23 Nov 2018 

 'PURPOSE: Send All Data From Text File To A String Variable
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
Dim fso As Object
    'File Path of Text File
    FilePath = Environ("USERPROFILE") & "\Temp\VFile.txt"
    'or for the User Temp folder
    'FilePath = Environ("TEMP") & "\VFile.txt"
    'However these are not the same folder

    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(FilePath) Then
        'Determine the next file number available for use by the FileOpen function
        TextFile = FreeFile
        'Open the text file
        Open FilePath For Input As TextFile
        'Store file content inside a variable
        FileContent = Input(LOF(TextFile), TextFile)
        'Close Text File
        Close TextFile
        'Report Out Text File Contents
        TextFile_PullData = FileContent
    Else
        TextFile_PullData = ""
        Beep
        MsgBox FilePath & vbCr & " is not available"
    End If
    Set fso = Nothing
End Function

'Second part it takes the Information and adds it to this script in the TextFile_PullData

Sub UpdateSubject()
Dim SaveCode As String
Dim KeyWord As String
Dim objItem As MailItem

    KeyWord = "TSD"
    SaveCode = TextFile_PullData
    If SaveCode = "" Then Exit Sub
    Set objItem = GetCurrentItem()
    objItem.Subject = "[" + KeyWord + "=" + SaveCode + "] " + objItem.Subject
    Set objItem = Nothing
End Sub

Function GetCurrentItem() As Object
Dim objApp As Outlook.Application

    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.currentItem
    End Select

    Set objApp = Nothing
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 11-27-2018, 05:08 AM
Firemaster Firemaster is offline Read text in a file Windows 7 64bit Read text in a file Office 2010
Novice
Read text in a file
 
Join Date: Nov 2018
Posts: 2
Firemaster is on a distinguished road
Default

The key word is totally irrelevant all this does is adds what we require to an email subject. the script which i have at the moment prompts for the contents of the TXT file and then inserts it into the subject line.
The idea is that the application we have can write the info to a text file which will be something along the lines of FFF00001/001

Then from the script we take the contents of that text file and the script takes that info automaticaly and adds it to the subject line along with the key word. So with the key word it will be TSD=FFF00001/001. Does that make sense?

The location of the file is C:\users\%username%\Temp\VFfile.Txt (As used in CMD)
so out of the script you supplied how is that added?
Reply With Quote
  #4  
Old 11-28-2018, 04:18 AM
gmayor's Avatar
gmayor gmayor is offline Read text in a file Windows 10 Read text in a file Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,024
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

You need to change the following section - the rest seems OK
Code:
Sub UpdateSubject()
Dim SaveCode As String
Dim KeyWord As String
Dim objItem As Object
Dim strSubject As String
    KeyWord = "TSD"
    SaveCode = TextFile_PullData
    If SaveCode = "" Then Exit Sub
    Set objItem = GetCurrentItem()
    strSubject = objItem.Subject
    objItem.Subject = "[" & KeyWord & "=" & SaveCode & "] " & strSubject
    Set objItem = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read text in a file Macro to read text file and organize data in excel srinidhi.mv88 Excel Programming 5 05-19-2015 12:06 AM
Read text in a file Read only file NienkeG Word 2 08-03-2014 03:57 PM
Read the newest file in a folder, how? DID PowerPoint 1 07-09-2014 01:40 AM
Read text in a file Read text Report file with VBA and write parsed fields to Excel workbook tpcervelo Excel Programming 1 01-05-2012 10:14 PM
PowerPoint can't read x.ppt file? chronic student PowerPoint 0 05-30-2006 05:35 AM


All times are GMT -7. The time now is 08:24 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft