PDA

Click to See Complete Forum and Search --> : I give up!!!


madddog
Nov 15th, 1999, 04:28 AM
Can somebody post a good example of an code that reads an ini file, then passes that information to a variable or string to open another file. Ex. Word or Excel
Thats all I need to finish my little program and I haven't got much hair left.

Thanks in advance,

Mike

MartinLiss
Nov 15th, 1999, 04:38 AM
Are you having problems reading the ini file or just passing the info to Word or Excel?

------------------
Marty

madddog
Nov 15th, 1999, 07:12 AM
During debug it sail past the loading of the ini file and hangs up where I am trying to pass the ini value to an open.doc command. I can force my program to open the file in the code but can't get it to read the value. I need to set the ini file so that other people I work with can access different templete files according to the customers they are working with.

My ini files look something like this:

filename=s:\mech\honda\honda.ini

[File Locations]
part1=s:\mech\honda\0000-000001.doc
part2=s:\mech\honda\0000-000002.doc
part3=s:\mech\honda\0000-000003.doc
part4=s:\mech\honda\0000-000004.doc
part5=s:\mech\honda\0000-000005.doc

I've read so much info on creating and reading ini files, that confused wouldn't be an appropriate word to describe my state of mind.

Thanks,

Mike

Serge
Nov 15th, 1999, 08:40 AM
The structure of your INI file is very easy read from. If i understood you correctly, you want to open each file from the INI file.
Here it is (assuming that INI filename is C:\MyFile.ini:


Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

'--------Put this on any event you want
Dim lRet As Long
Dim strBuffer As String
Dim i As Integer

'So your count starts with 1 (i.e. Part1)
i = 1
Do
strBuffer = Space(255)
lRet = GetPrivateProfileString("File Locations", "Part" & CStr(i), "", strBuffer, Len(strBuffer), "C:\MyFile.ini")
If lRet Then
strBuffer = Left(strBuffer, lRet)
'strBuffer is now holding the path to the file
'So, here should go your Open file routine
End If
i = i + 1
Loop While lRet > 0



Regards,

------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)

madddog
Nov 15th, 1999, 09:12 AM
Thanks, I'll try it tonight and let you know
how it works in the morning. I'm still open
for any other suggestions.

Thanks again,

Dazed and Confused.....Mike

MartinLiss
Nov 15th, 1999, 10:00 AM
Here is some code you can use to have Word open your file once you have isolated strBuffer using Serge's code.
Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Sub RunFile(ByVal File$, FilePath$, RunStyle)

Dim sTemp As String
Dim sMsg As String
Dim x As Integer

sTemp = GetActiveWindow()
x = ShellExecute(sTemp, "Open", File$, "", FilePath$, RunStyle)

If x < 32 Then
Select Case x
Case 0
sMsg = "The file could not be run due to insufficient system memory or a corrupt program file"
Case 2
sMsg = "File Not Found"
Case 3
sMsg = "Invalid Path"
Case 5
sMsg = "Sharing or protection error"
Case 6
sMsg = "Separate data segments are required for each task "
Case 8
sMsg = "Insufficient memory to run the program"
Case 10
sMsg = "Incorrect Windows version"
Case 11
sMsg = "Invalid Program File"
Case 12
sMsg = "Program file requires a different operating System "
Case 13
sMsg = "Program requires MS-DOS 4.0"
Case 14
sMsg = "Unknown program file type"
Case 15
sMsg = "Windows program does not support protected memory mode"
Case 16
sMsg = "Invalid use of data segments when loading a second instance of a program"
Case 19
sMsg = "Attempt to run a compressed program file"
Case 20
sMsg = "Invalid dynamic link library"
Case 21
sMsg = "Program requires Windows 32-bit extensions"
Case 31
sMsg = "No application found for this file"
End Select

MsgBox sMsg
End If

End Sub


Private Sub Form_Load()
Dim strBuffer As String
Dim strFile As String
Dim strPath As String
Dim nPos As Integer

' Once you have strBuffer using Serge's code, you don't need this line
strBuffer = "part1=s:\mech\honda\0000-000001.doc"

strBuffer = Right(strBuffer, Len(strBuffer) - Len("part1="))
nPos = InStrRev(strBuffer, "\")
strFile = Right(strBuffer, Len(strBuffer) - nPos)
strPath = Left(strBuffer, nPos - 1)

RunFile strFile, strPath, vbNormalFocus
End Sub


------------------
Marty

madddog
Nov 17th, 1999, 11:45 AM
Gentlemen,

I want to thank you for your patience
and understanding. I finally got all
my program to work, and works great.

Thanks Again,

Mike