Working with files in VB 6.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • diSangro
    New Member
    • Jan 2007
    • 69

    #46
    Well...that's not really what I said;

    I posted the code for Sub FindReplace(... )

    This Sub treats the rtf as Word object (NOT as text file!) and replaces field codes as follows:

    -deactivate field codes
    -replace the searched string (i.e.: a field code name)
    -activate your field codes again

    then I think you have problems in viewing replaced field code names due to their lenght, when lenght exceeds the cell lenght of your table you cant see them.
    This is because field codes cant be viewed on more lines, even if you put a newline inside it.
    So you should replace a field code with a normal text string, in this way your text can be viewed on more lines.

    To do this I suggested to use Sub FindReplace(... ) adding the line:

    Sfind = "^dMACROBUT TON QueryVeld " & Sfind

    Just after opening your rtf file as Word object (NOT as text file!).
    Then even if you use:

    Sreplace = "pippopippopipp opippopippopipp opippopippopipp opippopippopipp opippopippopipp opippo"


    Your field code name (still passed through Sfind as parameter) will be replaced with the text string Sreplace, which, being normal text, can be displayed on more lines.


    You can also pass Sreplace as a string comin from a text file, this shouldn't be a problem.

    diSangro

    Originally posted by amitp
    According to u i need to read the RTF file as plain text file line by line. Consider 'MyLine ' in which i'm reading a file line by line. If there is a text "<<s_naam>> " present in the line which is another file name. I want to replace "<<s_naam>> " with its whole content. How can i do that. Can this code will be do.

    Actually my requirement is that previously my RTF file has only field names which is to be replaced by a value. But now the RTF file consists of field names which are another file names. So i need to replace the field name with the whole content of another file so that the files will be appended. I tried this but in some case it works fine but there comes a blank line between two files and in some case the file becomes corrupt. Need ur help. Thanx in Advance.

    Set FsamIn = CreateObject("S cripting.FileSy stemObject")
    Set F1In = FsIn.OpenTextFi le(s_naam, ForReading, TristateFalse)
    lsWholeFile = F1In.ReadAll
    lsMyLine = Replace(lsMyLin e, "<<s_naam>> ", lsWholeFile)

    Comment

    • amitp
      New Member
      • Dec 2006
      • 49

      #47
      i tried this but this is not working at all. Could u answer my next question in which i'm opening the RTF file as text file. Bcoz if i use WORD object it takes around 5-10 minutes to generate the whole report. So i want to use the FILESYSTEMOBJEC T. Thanking You

      Comment

      • diSangro
        New Member
        • Jan 2007
        • 69

        #48
        'Complete code ( please dont say it doesn't work cause it does on my pc) :

        Code:
        Sub FieldCode_onoff(wrdDoc As Variant, onoff As Boolean)
        
        wrdDoc.ActiveWindow.View.ShowFieldCodes = Not wrdDoc.ActiveWindow.View.ShowFieldCodes
        wrdDoc.Application.DisplayStatusBar = True
        wrdDoc.Application.ShowWindowsInTaskbar = True
        wrdDoc.Application.ShowStartupDialog = True
        With wrdDoc.ActiveWindow
        .DisplayHorizontalScrollBar = True
        .DisplayVerticalScrollBar = True
        .DisplayLeftScrollBar = False
        .StyleAreaWidth = InchesToPoints(0)
        .DisplayVerticalRuler = True
        .DisplayRightRuler = False
        .DisplayScreenTips = True
        With wrdDoc.ActiveWindow.View
        .ShowAnimation = True
        .Draft = False
        .WrapToWindow = False
        .ShowPicturePlaceHolders = False
        .ShowFieldCodes = onoff
        .ShowBookmarks = False
        .FieldShading = wdFieldShadingWhenSelected
        .ShowTabs = False
        .ShowSpaces = False
        .ShowParagraphs = False
        .ShowHyphens = False
        .ShowHiddenText = False
        .ShowAll = False
        .ShowDrawings = True
        .ShowObjectAnchors = False
        .ShowTextBoundaries = False
        .ShowHighlight = True
        .DisplayPageBoundaries = True
        .DisplaySmartTags = True
        End With
        End With
        End Sub
        '-----------------------------------------------------------------------------
        
        
        Sub FindReplace(ByVal Sfind As String, ByVal Sreplace As String, ByVal Inputfile As String, ByVal Outputfile As String)
        
        Set wrdApp = CreateObject("Word.Application")
        wrdApp.Visible = True
        Set wrdDoc = wrdApp.Documents.Open(Inputfile)
        
        Sfind = "^dMACROBUTTON QueryVeld " & Sfind
        
        Call FieldCode_onoff(wrdDoc, True)
        
        
        wrdApp.Application.Selection.Find.ClearFormatting
        wrdApp.Application.Selection.Find.Replacement.ClearFormatting
        
        With wrdApp.Selection.Find
        .Text = Sfind
        .Replacement.Text = Sreplace
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        End With
        wrdApp.Application.Selection.Find.Execute Replace:=wdReplaceAll
        Call FieldCode_onoff(wrdDoc, False)
        ' Save to ouput file
        With wrdDoc
        .SaveAs (Outputfile)
        .Close ' close the document
        End With
        wrdApp.Quit ' close the Word application
        Set wrdDoc = Nothing
        Set wrdApp = Nothing
        
        
        End Sub
        '----------------------------------------------How to use:
        
        Private Sub CommandButton1_Click()
        
        Dim wrdApp As Word.Application
        Dim IFile, OFile As String
        Dim Sfind, Sreplace
        IFile = "\NEW\Minchione.rtf" ' an existing rtf file in c:\NEW\ where Sfind occurs
        OFile = "\NEW\Minchione2.rtf"
        Sfind = "<<s_naam>>" ' string to find
        Sreplace = "pippopippopippopippopippopippopippopippopippopippopippopippopippopippopippo" ' string to replace
        
        Call FindReplace(Sfind, Sreplace, IFile, OFile)
        End Sub
        
        'Using the last Sub I could replace <<s_naam>> with:
        'pippopippopippopippopippopippopippopippopippopippopippopippopippopippopippo
        'displayed on 2 lines
        Originally posted by amitp
        i tried this but this is not working at all. Could u answer my next question in which i'm opening the RTF file as text file. Bcoz if i use WORD object it takes around 5-10 minutes to generate the whole report. So i want to use the FILESYSTEMOBJEC T. Thanking You
        Last edited by Killer42; Mar 13 '07, 09:36 PM. Reason: Please use [CODE]...[/CODE] tags around your code.

        Comment

        • amitp
          New Member
          • Dec 2006
          • 49

          #49
          Thank u very much. This is working fine. Actually i wanted to read the RTF file as text file. So i have a problem while reading. I want to append a RTF file at the end of the other. But before attaching i want to remove the last text "\par }" of the first file and then attach the same file just after that. but i don't know how to do that. I read the input file to a output file like this. I think u know the RTF text file, i've posted to in earlier reply.

          ---opening the Input File
          Set fs = CreateObject("S cripting.FileSy stemObject")
          Set F = fs.OpenTextFile (lsInFile, ForReading, TristateFalse)

          ----Creating a blank output file to write
          Set Fs1 = CreateObject("S cripting.FileSy stemObject")
          Set F1 = Fs1.CreateTextF ile(lsOutFile, ForAppending)

          Do While F.AtEndOfStream <> True
          myLine = F.ReadLine
          F1.Write myLine
          Loop

          Now I want to write file F again at the end of F1. So before doing this i need to remove the last text of F1 "\par }" from it. How can i do that. Need ur help.Thanking You

          Comment

          Working...