PDA

View Full Version : Solved: special characters from text file to word



white_flag
05-17-2010, 05:03 AM
hello

I have the following code:

Private Sub fill()
nFile = FreeFile()
Set fs = CreateObject("Scripting.FileSystemObject")
For j = 1 To 3
If Controls("OptionButton" & j) = True Then
Set f = fs.GetFile(ActiveDocument.Path & "\bkmk." & j)
Open ActiveDocument.Path & "\bkmk." & j For Input As #nFile
Do While Not EOF(nFile)
Input #nFile, sTemp
For i = 1 To 2
If Len(sTemp) > 0 Then
nEqualsSignPos = InStr(sTemp, "=")
nEqualsSignPos = nEqualsSignPos + 1
If InStr(sTemp, Controls("ComboBox" & i).Value) Then
'Controls("Frame" & i).Caption = Mid(sTemp, nEqualsSignPos)
Call FillBM("book" & i, Mid(sTemp, nEqualsSignPos))
'Controls("Frame" & i).Caption = Mid(sTemp, nEqualsSignPos)
End If
End If
Next
Loop
Close nFile
End If
Next
End Sub
it is working good but I would like to insert from text file in word some special characters and word to understand that are the special characters and to put them.some one can help me?

thx

fumei
05-17-2010, 10:02 AM
We are going to need a bit more detail than that.

white_flag
05-25-2010, 07:17 AM
ok ..I had to do something else. now, I have some time for VBA.

please look in attachment. I like to know if it is possible to insert via an text file combination of keybords (and those one to become in word special characters like chr(13) or something else).

my res-one is: I like to have an title and after the title to start the paragraph, the paragraph to be defined in text file.

the bookmarks from word file it is like this Title1 (space) Title2 (space) bmk1 (space) bmk2(space) etc...

after text will be inserted the text from file to be like this:

Title1 Title 2
bmk1
bmk2
Title3 Title 4
bmk3
bmk4
etc...
in text file after the "=" I do not know what can be write that the line to go down one step.and i do not meen like this line


Call FillBM("title" & i, Chr$(13) & .Value)

so, if it is possible, a bit of help will be great.

fumei
05-25-2010, 02:19 PM
Your userform errors out for me as you have:
For j = 1 To 2
and there is only bkmk.1 - no 2

I am have a hard time understanding what you are doing. It seems overly complicated.

white_flag
06-02-2010, 12:27 AM
good morning

I have the following code:

Private Sub fill()
nFile = FreeFile()
Set fs = CreateObject("Scripting.FileSystemObject")
For j = 1 To 2
If Controls("OptionButton" & j) = True Then
Set f = fs.GetFile(ActiveDocument.Path & "\bkmk." & j)
Open ActiveDocument.Path & "\bkmk." & j For Input As #nFile
Do While Not EOF(nFile)
Input #nFile, sTemp
For i = 1 To 2
If Len(sTemp) > 0 Then
nEqualsSignPos = InStr(sTemp, "=")
nEqualsSignPos = nEqualsSignPos + 1
If InStr(sTemp, Controls("ComboBox" & i).Value) _
Then
With Me.Controls("ComboBox" & i)
If .ListIndex > 0 Then

Call FillBM("book" & i, Chr(13) _
& Mid(sTemp, nEqualsSignPos))
Else
Call FillBM("book" & i, "")
End If
End With
End If
End If
Next
Loop
Close nFile
End If
Next
End Sub


This code open an text file and put it in the memory, from there, insert the matching string after "an marker" (in this case the marker is "=").What I Like to know is who can be to replace ending string (Return) with an diffrent character(@).
ex: text from file
txt1=This is line 1
This is line 2
This is line 3@
txt2=Different line 1
Different line 2
Different line 3@

this code it is puting just the text till find the end of the string (Return):
This is line 1
Different line 1
and I want to be inserted like this:
This is line 1
This is line 2
This is line 3
....
thanks for the help.
A.

fumei
06-02-2010, 01:25 PM
I will repeat. Your userform - again - fails, as there is no j = 2.

It does work once, but - again - I do not understand what you want to happen. And - again - and i have mentioned this in a number of posts, this seems very complicated, and I am not sure it needs to be.

if you want to have multiple lines, it would be, I think, better to use an INI file, rather than trying to read a text file line by line.

Further, why are you even using a FileSystemObject when you are opening the file as a FreeFile? You have:
Set fs = CreateObject("Scripting.FileSystemObject")

' AND
Set f = fs.GetFile(ActiveDocument.Path & "\bkmk." & j)

.......and never actually use them. Why?

white_flag
06-02-2010, 02:30 PM
thx Gerry that you give an look to this.
the j=2 will be used in the future. and it is not necessary for the moment.This is just simplify (and here I am stuck, that is why I put't like this). my problem is: I would like if it is possible to put entire paragraph via an text file. in this paragraph involve also multiple lines not just an long long line. I was thinking it is possible to put two markers that will tell to VBA to read the file and wen see the first marker "=" to take the string till the second marker "@". And this string to be inserted in an bookmark.
I like to separate input data from VBA code.The text from .txt will be change't more often then the VBA code. I just want to make my life easier. but to make something easier it is not that easy :)

white_flag
06-02-2010, 02:32 PM
can you tell more about an ini file?

white_flag
06-02-2010, 05:13 PM
I try this but ..it is not going

sTemp = Replace(sTemp, Chr(13), "@")

white_flag
06-03-2010, 12:32 AM
like this:

Private Sub fill()
nFile = FreeFile()
For j = 1 To 2
If Controls("OptionButton" & j) = True Then
Open ActiveDocument.Path & "\bkmk." & j For Input As #nFile
While Not EOF(nFile)
Input #nFile, sTemp
sTemp = Replace(sTemp, "@", vbCrLf)
'sTemp = Replace(sTemp, vbCrLf, "@")
For i = 1 To 2
If Len(sTemp) > 0 Then
nEqualsSignPos = InStr(sTemp, "=")
nEqualsSignPos = nEqualsSignPos + 1
If InStr(sTemp, Controls("ComboBox" & i).Value) Then
With Me.Controls("ComboBox" & i)
If .ListIndex > 0 Then
Call FillBM("book" & i, Chr(13) & Mid(sTemp, nEqualsSignPos))
Else
Call FillBM("book" & i, "")
End If
End With
End If
End If
Next
Wend
Close nFile
End If
Next
End Sub
I try to replace vbCrLf with "@" like this the string end will be @ not vbCrLf but it is not going ..another idea?

white_flag
06-03-2010, 12:46 AM
or like this:


Private Sub fill()
For j = 1 To 2
If Controls("OptionButton" & j) = True Then
Open ActiveDocument.Path & "\bkmk." & j For Input As #1
Do While Not EOF(1)
Input #1, sTemp
sTemp = Replace(sTemp, "@", vbCrLf)
For i = 1 To 2
If Len(sTemp) > 0 Then
nEqualsSignPos = InStr(sTemp, "=")
nEqualsSignPos = nEqualsSignPos + 1
If InStr(sTemp, Controls("ComboBox" & i).Value) Then
With Me.Controls("ComboBox" & i)
If .ListIndex > 0 Then
Call FillBM("book" & i, Chr(13) & Mid(sTemp, nEqualsSignPos))
Else
Call FillBM("book" & i, "")
End If
End With
End If
End If
Next
Loop
Close 1
End If
Next
End Sub

white_flag
06-04-2010, 02:13 AM
I used this function:

Public Function strFindBetween(strToSearch As String, _
strStartPoint As String, strEndPoint As String, Optional _
skipInstance As Integer = 0) As String

'Quit the function if infomation provided doesn't fit requirements
If strToSearch = "" Or strStartPoint = "" Or strEndPoint = "" Then Exit Function
If Not InStrB(strToSearch, strStartPoint) > 0 _
And InStrB(strToSearch, strEndPoint) > 0 Then Exit Function

Dim trimStrLine As String
Dim strResult As String
Dim skipInst As Integer

trimStrLine = strToSearch

'Skip instances of first string; default doesn't skip
For skipInst = 0 To skipInstance
trimStrLine = Mid$(trimStrLine, InStr(trimStrLine, strStartPoint) + Len(strStartPoint))
Next skipInst

'Trim the line to the end of the second specified string
trimStrLine = Mid$(trimStrLine, 1, InStr(trimStrLine, strEndPoint) - 1)

'Assign the string between the two given string-points
strFindBetween = trimStrLine

End Function

then I change the code in this:


Sub insert_text()
On Error GoTo E_Handle
Dim strImport As String
Dim lngChars As Long
Dim intFile As Integer
intFile = FreeFile
Open ActiveDocument.Path & "\bkmk.1" For Input As intFile
lngChars = LOF(intFile)
strImport = Input(lngChars, intFile)
For i = 1 To 2
With Me.Controls("ComboBox" & i)
If .ListIndex > 0 Then
Call FillBM("book" & i, strFindBetween(strImport, _
Controls("ComboBox" & i).Value & "=", "@"))
Else
Call FillBM("book" & i, "")
End If
End With
Next
Close intFile

sExit:
On Error Resume Next
Reset
Exit Sub
'E_Handle:
'MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
'Resume sExit
End Sub

this solution satisfy me. I can insert the multiple line of text from an text file via VBA. The text that will be inserted it is between two markers. in this case the value of combobox and "@".

ps. today will be here 28°C (this is lovely)