Lotus Notes external doc files search text - lotus-notes

By using the follow code i can find a strin in eny external file ( .txt ).
Function "funcCheckIfStrinInFileExist" use Instr function to find position of the string
and return 0 if an integer if string exist in myRecord (see bellow ).
Is there any way to search a .doc file (or .docx) , because (or .PDF !!!) if file is word document return error.
Thanks in advance.
Do While fileName$ <> ""
varCheckIfStrinInFileExist = funcCheckIfStrinInFileExist( fileName, myString )
If varCheckIfStrinInFileExist Then
Messagebox "i found the text in file : " + fileName
End If
fileName$ = Dir$()
Loop
'************************************************************************
funcCheckIfStrinInFileExist ( fileName, myString ) as integer
.......................................
Do Until Eof(filenum%)
'Read a line of data
Input #filenum%, myRecord
positionOfChar = Instr(myRecord$, varString)
If Cint(positionOfChar) > 0 Then
funcCheckIfStrinInFileExist = True
Close filenum
Exit Function
End If
Loop

Related

Is there a way to retrieve information from a text file in vba?

I need to save information from a text file into an array. But I dont know what the specific syntax is.
The information from the text file is about 2000 lines, which obviously you cant store within the vba script. The text looks like the below in one
35SLFR0006350
35SLFR0026350
35SLFR0106350
BARSQR1306000
C280BD1016000
C280BD1016000_mitre
C280BD1016000_square
C280FR0006000
C280MU0006000
C280MU0026000
C280SH0006000
C280SH0006000_outer frame
C305BD0006000
C305BD0006000_mitre
C305BD0006000_square
C305BD0016000
C305BD0016000_mitre
C305BD0016000_square
C305BD2006000
C305BD2006000_mitre
C305BD2006000_square
C305FR0006000
C305MU0006000
C305MU0026000
C305MU0046000
C305SH0006000
C305SH0006000_Un E frame
C340BD1006000_mitre
C340BD1006000_Right,Left,Horizontal
C340BD1006000_Right,Left,Vertical
C340BD1006000_square
C340FR00060000
C340MU0006000
C340MU0026000
C340SH0006000
If you want to save your input file as an array you can first read the whole file and save it as one whole string.
You can then use the Split function with the delimiter \n to return an array, where every element corresponds to one line of the file.
Const file As String = "<pathToFile>"
Dim ResultArray() As String
Dim tempString As String
Dim fn As Integer
fn = FreeFile()
Open file For Input As fn
While Not EOF(fn)
Line Input #fn, LineString
tempString = tempString & LineString & "\n"
Wend
ResultArray = Split(tempString, "\n")

Lotus Notes : Not able to access properties file in lotuscript

Hi friends i want to access the properties file from machine from the specified path. For java Agent i used Properties method and extracted the data from the properties file. but now i want it to be done in lotuscript. I tried using properties method but it didnt work so i thought to read properties with the below code.
'Dim ColFileName As String
'ColFileName="C:\abcd.properties"
Open ColFileName For Input As 1
Do While Not EOF(1)
Line Input #1,txt$
MsgBox "TEXT FILE:"+txt$
In properties file i have a written it as
col=start
where i want to get the property of the col using getProperty method in java same way for lotusscript.
I added the above code but it is not working. Can anyone tell what mistake i have committed.
In Options
%Include "lserr.lss" 'This is just a list of constants.
Add these functions somewhere:
%REM
Function fstreamOpenFile(sPath As String, bTruncate As Boolean, bConfirmExists As Boolean) As NotesStream
<dl>
<dt>sPath</dt><dd>Filepath of the file to be opened/created.</dd>
<dt>bTruncate</dt><dd>Boolean. True if file is for output and any existing file should be replaced rather than appended to.</dd>
<dt>bConfirmExists</dt><dd>Boolean. If True, and the opened file is empty, then an ErrFileNotFound error will be thrown.</dd>
</dl>
%END REM
Function fstreamOpenFile(sPath As String, bTruncate As Boolean, bConfirmExists As Boolean) As NotesStream
Dim session as New NotesSession Dim stream As NotesStream
Set stream = session.Createstream()
If Not stream.Open(sPath) Then Error ErrOpenFailed, {Could not open file at "} + sPath + {"}
If bConfirmExists And stream.Bytes = 0 Then Error ErrFileNotFound, {File at "} + sPath + {" is missing or empty.}
If bTruncate Then Call stream.Truncate()
Set fstreamOpenFile = stream
End Function
Function fsPropertyFileValue(sFilePath As String, sPropertyName As String, sDefaultValue As String) As String
On Error GoTo ErrorQuietly
Dim stream As NotesStream
Dim sLine As String
Dim sLeft As String
Dim iLeftLen As Integer
Set stream = fstreamOpenFile(sFilePath, False, True)
sLeft = sPropertyName + "="
iLeftLen = Len(sLeft)
Do
sLine = stream.Readtext
If Left(sLine, iLeftLen) = sLeft Then
fsPropertyFileValue = Right(sLine, Len(sLine) - iLeftLen)
Exit Function
End If
Loop Until stream.Iseos
ReturnDefault:
fsPropertyFileValue = sDefaultValue
Exit Function
ErrorQuietly:
Print Now, Error$
Resume ReturnDefault
End Function
(Notes: I have not tested/debugged fsPropertyFileValue. The html tags in the comment is because, when editing an agent or script library, the designer client will parse and display the HTML tags.)
Then you can use fsPropertyFileValue("C:\abcd.properties", "col", "start") to get the value of the col property within C:\abcd.properties and, if that fails, use "start".

How to remove file extension from file name (VBA)

I have a filename variable that contains : "Filename.csv" . To extract the filename from a path I use: Filename=Dir([fStr]) where fStr is retrieved from the file that I selected.
I only need the filename without ".csv". How do I remove the ".csv" extension?
It's best to use a function like GetBaseName() instead of relying on functions to replace text. Windows allows periods to appear within the base filename so something like this is legitimate:
My .csv for Bob.csv
Using Replace() would result in:
My for Bob
Not what you're looking for. A better approach would be:
Filename = CreateObject("Scripting.FileSystemObject").GetBaseName(fStr)
You can use the replace function:
Filename = replace(Dir([fStr]),".csv","")
Too late to answer it, might be helpful for future
Try this
Mid(fileName, 1, InStr(1, fileName, ".") - 1)
It will work with any file extension or filename
For example:
fileName : ThisIsMyFile.csv
My code runs on various systems which may not allow scripting. I rewrote this to get around this limitation.
Function FileGetBaseNameNoExt(aFilenameStr As String) As String
Dim TmpCnt As Integer
Dim TmpStr As String
FileGetBaseNameNoExt = aFilenameStr
If InStr(aFilenameStr, ".") = False Then
Exit Function
End If
TmpCnt = 1
TmpStr = Left(Right(aFilenameStr, TmpCnt), 1)
While TmpStr <> "."
TmpCnt = TmpCnt + 1
TmpStr = Left(Right(aFilenameStr, TmpCnt), 1)
Wend
'Make Sure the Filename is Not Something Odd like .csv
If TmpCnt < Len(aFilenameStr) Then
FileGetBaseNameNoExt = Left(aFilenameStr, Len(aFilenameStr) - TmpCnt)
End If
End Function

CSV quotes + comma separators and BREAK LINE : import in EXCELwithout break line

I have this kind of CSV :
So when I import in EXCEL 2013 with "get data from text file",
1) how to say separator IS QUOTES + COMMA,
2) For Excel, BREAK LINE in fields are new line of data... how to say it's not ?
I know that CSV are a long topic on the Web, but there is no obvious solution.
So thanks for your help.
Step 1: I would first fix the file:
Sub ImportData()
Dim dataTextFile As String
Dim outTextFile As String, splitPart As String, missingPart As String, ret As String
'---Read the file---
Open "C:\Data.txt" For Input As #1
dataTextFile = Input$(LOF(1), 1)
Close #1
dataTextFile = Replace(dataTextFile, vbNewLine, "[NEW_LINE]")
ret = GetRegex("(.*?,.*?,.*?),")
outTextFile = outTextFile & ret & vbNewLine
Do While 1
If (ret = GetRegex(dataTextFile, ",(.*?,.*?,.*?)")) <> "" Then
Exit Do
Else
outTextFile = outTextFile & ret & vbNewLine
dataTextFile = Right(dataTextFile, Len(dataTextFile) - Len(ret))
End If
Loop
'---Write back to the file---
Open "C:\Data.txt" For Output As #1
Write #1, outTextFile
Close #1
End Sub
You can find the GetRegex function here: link. Or just use the general Regex object. Warning: I haven't tested the code so you might need to do some tinkering.
Step 2: Next you simply import the file normally via the Import Wizard as comma separated file.
Step 3: Lastly replace "[NEW_LINE]" with New Line characters via macro or whatever other way.
Alternatively you may just as well do steps 2-3 in the same macro - and use the Right function or a Regex to extract the column to your output worksheet.

Character changes when importing to Access and exporting to Excel

I'm working on an Access database in which I import csv files converted from xls
Usually this works, but recently one file has some fields where characters change within the field after being imported into Access
For example:
a dash changes to û
a beginning double quote changes to ô
an end double quote changes to ö
From what I have read it has something to do with 7 or 8 bit character codes.. which is not something I really understand.
My questions are, is there any way to prevent this character change or is there something better than what I've tried already?
Or are there any potential problems that I haven't come across with what seems to work in my example below?
Here's what I've tried so far that seems to work
From the original Excel file Save as unicode text file (something new for me)
ActiveWorkbook.SaveAs Filename:= _
"D:\NewFiles\ReportList.txt", FileFormat:=xlUnicodeText _
, CreateBackup:=False
Then import into the database with the following code
DoCmd.TransferText acImportDelim, "ReportList Import Specification", "tbl_ReportList", "D:\NewFiles\ReportList.txt", True
This seems to import the text into the database correctly.
Other people work with the data and then export a new report from Access to Excel.
That changes the font to MS Sans Serif and changes the characters again but not the same changes as when it was imported.
After the Excel report is exported, and I change the font to Arial the characters are correct again.... at least so far.
I haven't run into this character change in the past and my solution seems to work, but I'm not sure if there are other potential problems or if there's anything I missed. I haven't found the answer to this specific question yet.
Thanks for taking time to help with this.
Here is a method that I have used in the past to circumvent the character encoding issues.
I suspect this method should also work between Excel and Access -- although Access is not really something I am familiar with.
This sub specifies the file's full name & path, and a destination for a new filename & path. These could be the same if you want to overwrite existing.
NOTE On a few simple tests, I can't get this to read a file saved as "Unicode" from Excel, but it works perfectly on files saved as "Tab Delimited TXT" files and CSV/comma-separated files, too.
Sub OpenAndSaveTxtUTF8()
Dim txtFileName as String
Dim newTxtFileName as String
txtFileName = "D:\NewFiles\ReportList.txt"
newTxtFileName = "D:\NewFiles\UTF8_ReportList.txt"
WriteUTF8(ReadTextFile(txtFileName), newTxtFileName)
End Sub
This sub calls on two functions which I borrowed from sources credited in the code comments. The WriteUTF8 creates a proper UTF8 file from the contents of ReadTextFile which returns a string of the full file contents.
Function ReadTextFile(sFileName As String) As String
'http://www.vbaexpress.com/kb/getarticle.php?kb_id=699
Dim iFile As Integer
On Local Error Resume Next
' \\ Use FreeFile to supply a file number that is not already in use
iFile = FreeFile
' \\ ' Open file for input.
Open sFileName For Input As #iFile
' \\ Return (Read) the whole content of the file to the function
ReadTextFile = Input$(LOF(iFile), iFile)
Close #iFile
On Error GoTo 0
End Function
This function requires a reference to the ADODB library, or, you can Dim objStream As Object and the code should still work for you.
Function WriteUTF8(textString$, myFileOut$)
'Modified from http://www.vbaexpress.com/forum/showthread.php?t=42375
'David Zemens - February 12, 2013
'Requires a reference to ADODB?
' UTF8() Version 1.00
' Open a "plain" text file and save it again in UTF-8 encoding
' (overwriting an existing file without asking for confirmation).
'
' Based on a sample script from JTMar:
' http://bytes.com/groups/asp/52959-save-file-utf-8-format-asp-vbscript
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
Dim objStream As ADODB.Stream
' Valid Charset values for ADODB.Stream
Const CdoBIG5 = "big5"
Const CdoEUC_JP = "euc-jp"
Const CdoEUC_KR = "euc-kr"
Const CdoGB2312 = "gb2312"
Const CdoISO_2022_JP = "iso-2022-jp"
Const CdoISO_2022_KR = "iso-2022-kr"
Const CdoISO_8859_1 = "iso-8859-1"
Const CdoISO_8859_2 = "iso-8859-2"
Const CdoISO_8859_3 = "iso-8859-3"
Const CdoISO_8859_4 = "iso-8859-4"
Const CdoISO_8859_5 = "iso-8859-5"
Const CdoISO_8859_6 = "iso-8859-6"
Const CdoISO_8859_7 = "iso-8859-7"
Const CdoISO_8859_8 = "iso-8859-8"
Const CdoISO_8859_9 = "iso-8859-9"
Const cdoKOI8_R = "koi8-r"
Const cdoShift_JIS = "shift-jis"
Const CdoUS_ASCII = "us-ascii"
Const CdoUTF_7 = "utf-7"
Const CdoUTF_8 = "utf-8"
' ADODB.Stream file I/O constants
Const adTypeBinary = 1
Const adTypeText = 2
Const adSaveCreateNotExist = 1
Const adSaveCreateOverWrite = 2
On Error Resume Next
Set objStream = CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeText
objStream.Position = 0
objStream.Charset = CdoUTF_8
'We are passing a string to write to file, so omit the following line
' objStream.LoadFromFile myFileIn
'And instead of using LoadFromFile we are writing directly from the COPIED
' text from the unsaved/temp instance of Notepad.exe
objStream.WriteText textString, 1
objStream.SaveToFile myFileOut, adSaveCreateOverWrite
objStream.Close
Set objStream = Nothing
If Err Then
WriteUTF8 = False
Else
WriteUTF8 = True
End If
On Error GoTo 0
End Function

Resources