Results 1 to 15 of 15

Thread: Reading from Text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2019
    Posts
    15

    Reading from Text file

    Hey Guys!

    Maybe someone can help me with this ..

    Just a chunk of a bigger script

    If (bWordExtras) Then
    ReportProgress " Writing System Information"
    WriteHeader 1,"System Information"
    oWord.Selection.Style = wdStyleBodyText
    oWord.Selection.TypeText "text from file C:\file.txt" & vbCrLf
    End If

    Is there an easy way to include text from file.txt in that part of the script, I guess it needs to read the file and then print this back ?

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Reading from Text file

    You'd need to read the file first (or concurrently).

    ----

    EDIT: Take a look at the "Open" and "Line Input" statements. That's probably how I'd first attack the problem.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2019
    Posts
    15

    Re: Reading from Text file

    Quote Originally Posted by Elroy View Post
    You'd need to read the file first (or concurrently).

    ----

    EDIT: Take a look at the "Open" and "Line Input" statements. That's probably how I'd first attack the problem.

    Thank you! very helpful !
    And yes ... I can read it at the top of the script and keep it in variable .. not an issue

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2019
    Posts
    15

    Re using variables ?

    Hi Guys,

    So I have a larger open source script ... and I wanted to read information from a file and and then re use the data somewhere down stream in the script.


    Script ( Fetches text from txt file )
    Code:
    Const ForReading = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\randomdata.txt")
    Do Until objFile.AtEndOfStream
        strData = ""
        strSearchString = objFile.ReadLine
        intStart = InStr(strSearchString, "<data01>")
        If intStart <> 0 Then
            intStart = intStart + 13
            strText = Mid(strSearchString, intStart, 500)
            For i = 1 to Len(strText)
                If Mid(strText, i, 1) = "MoreData:" Then
                    Exit For
                Else
                    strData = strData & Mid(strText, i, 1)
                End If
            Next
        End If
    Loop
    I added this at the top of the original script and I tried to re use variable strData but it will not print anything
    Code:
    	If (bWordExtras) Then 
    		ReportProgress " Recording"
    		WriteHeader 1,"Name 01"
    		oWord.Selection.Style = wdStyleBodyText
    		oWord.Selection.TypeText "Test" & strData & vbCrLf
    	End If
    Any suggestion would be appreciated ! thank you in advance.
    Last edited by Shaggy Hiker; Dec 5th, 2019 at 03:29 PM. Reason: Added CODE tags.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Re using variables ?

    If you added the block at the bottom to the top of the script shown above it then strData will not have been assigned a value yet. Of course I am not sure that is what you meant nor am I sure what your are using. You say script which would imply VBScript but you have posted in a VB section [there is a different section for VBScript] It is also possible that you are talking about Office VBA which has its own location for questions as well. While all three shared many things in common VBScript, VBA and VB are 3 different things.

    So let us know which environment you are actually using and show us the actual code in the position it occurs so we can see what you are really doing.

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2019
    Posts
    15

    Re: Re using variables ?

    Thank you, Sorry it is VBScript .vbs ....

    I will repost this


    But to clarify the big block ( open file, search file ) is at the top and the below is the little block that writes to word... so strData should be populated ?

    Thank you

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Re using variables ?

    If the code occurs in the positions posted then yes it should be populated unless for whatever reason it is not being populated by the code above it or there is code that changes/clears the var somewhere between the population of the var and the bolded line near the bottom.

    Basically once a var has been assigned a value that value remains in the var until it is either cleared/changed via code or goes out of scope.

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2019
    Posts
    15

    Re: Re using variables ?

    this might be stupid question what should I be looking for ? to see if some other parts of code might be clearing it ?, Nothing should be changing it.

  9. #9
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Issues with variable passing ?

    Because you didn't enclose your code in code tags it is very difficult to follow the flow of the various loops and if conditions properly.

    You also haven't made it clear where the first snippet of code resides and where the second snippet of code resides.

    Given those issues, I (and many others here) wouldn't normally bother to try to help because my time would likely be spent asking you for clarification, you providing clarification that possibly needs more clarification, me asking for that further clarification, etc. Which turns into a time sink that I'm not interested in getting in to on this site.

    But since you are new, I'll give you the benefit of the doubt that you'll pick up on how to post a question in a way that helps us help you the next time you have a question.

    All that being said, about the only thing I can suggest given your post above is to try changing this:

    Code:
    Do Until objFile.AtEndOfStream
    strData = ""
    to this:

    Code:
    strData = ""
    Do Until objFile.AtEndOfStream
    I can see no reason why you would be clearing out strData each time through the loop if you want it to keep getting appended to throughout the course of that initial block of code you posted.

    Good luck.

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Re using variables ?

    Those first 3 lines in the bottom piece appear to be calling sub routines or functions, it is possible that there is something in one of those that is changing the value.

    You should check to see if there is a value in the var right after the loop statement.
    Been ages since I did anything with vbscript not sure if it has the msgbox statement or not. If it does then I would suggest using msgbox to display the value of that var at various points after the loop to see if there is a value and when that value is lost.

  11. #11
    Hyperactive Member
    Join Date
    Jan 2018
    Posts
    264

    Re: Re using variables ?

    What does your input file look like? If the matching line isn't the last one in the file, then that loop will clear out the strData variable anyhow. Also your check for MoreData will never work since that Mid function is only asking for a single character and you would need 9 for a match.

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Re using variables ?

    Good catch on the variable being cleared in the loop. When I saw the code there was no indents and I missed that the line that cleared it was inside the loop.

  13. #13
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Issues with variable passing ?

    How many threads do you need on the same thing? And in both threads you were warned about posting code wihout the [code][/code] blocks...
    [

    -tg
    Last edited by Shaggy Hiker; Dec 6th, 2019 at 01:08 PM. Reason: Removed no longer valid link
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Reading from Text file

    I merged the three together so that there is one discussion on the topic. I now understand why there were three threads, too, or at least I think I do. The first thread was older, and incomplete. I can understand re-asking the question in the more complete form. Then I assume you lost the thread and started a third. Now they are all in one place.



    Adding CODE tags makes formatting much nicer.
    My usual boring signature: Nothing

  15. #15

    Thread Starter
    New Member
    Join Date
    Dec 2019
    Posts
    15

    Re: Reading from Text file

    Thank you! everyone.

Posting Permissions

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



Click Here to Expand Forum to Full Width