OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Developer Forums > VBA > Code Critique

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 28-01-2006, 15:42
shelbym's Avatar
shelbym shelbym is offline
Senior Member
 
Join Date: Nov 2002
Location: Cheyenne, WY
Posts: 1,769
Blog Entries: 9
Send a message via ICQ to shelbym Send a message via AIM to shelbym Send a message via MSN to shelbym Send a message via Yahoo to shelbym
Default FileSystemObject

I have been using the FileSystemObject to write and read a simple text file I use to save settings for forms. Here is how I write the file.
Code:
Dim fs, objTextStream

Set fs = CreateObject("Scripting.FileSystemObject")
Set objTextStream = fs.CreateTextFile(GMSManager.GMSPath & "OBCNconfig.ini", True) 'Creates the Preference Text File

objTextStream.WriteLine txtPath ' Adds Users Preference for Path to be searched
objTextStream.WriteLine chkSearchSubDir 'Adds Users Preference for Searching SubFolders
objTextStream.Close
And here is how I read the file.
Code:
Dim fs, objTextStream

Set fs = CreateObject("Scripting.FileSystemObject")
Set objTextStream = fs.OpenTextFile(GMSManager.GMSPath & "OBCNconfig.ini", 1, False)
    
txtPath.Value = objTextStream.Readline
chkSearchSubDir = objTextStream.Readline
    
objTextStream.Close
So two questions first should I use the FileSystemObject to read/write files or would it be better to use something else like: Open "MyFile" For Append As #1 And if I use the Open how to you read it line by line so you can send what you read back to the form. Thanks for the thoughts.

Shelby
Reply With Quote
  #2  
Old 29-01-2006, 00:13
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default

Shelby,

I personally prefer the native VBA file functions simply because it is not dependent on the presence of the FileSystemObject library on the target machine.

Here is how you open a file and write some text to it:

Code:
Open "C:\File.txt" For Output As #1
Print #1, "Some text for line1"
Print #1, "Some text for line2"
Close #1
Here is how you read the text file line by line:

Code:
Dim sLine As String
Open "C:\File.txt" For Input As #1
While Not EOF(1)
    Line Input #1, sLine
    ' Do something with the read line
Wend
Close #1
Seems pretty simple and it works without any external components.

I also frequently use Windows API to read settings from INI files. This way I don't have to write any text parsers or anything to extract the data from the INI files myself. Here are the functions I use for this:

Code:
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
Private Declare Function WritePrivateProfileString Lib "kernel32" _
                Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
                ByVal lpKeyName As Any, ByVal lpString As Any, _
                ByVal lpFileName As String) As Long

Public Function GetConfigData(ByVal strSection As String, ByVal strKey As String, ByVal strDefValue As String, ByVal strINIFile As String) As String
    Dim n As Long
    Dim strVal As String
    strVal = Space$(260)
    n = GetPrivateProfileString(strSection, strKey, strDefValue, strVal, Len(strVal), strINIFile)
    GetConfigData = Left$(strVal, n)
End Function

Public Sub SetConfigData(ByVal strSection As String, ByVal strKey As String, ByVal strValue As String, ByVal strINIFile As String)
    WritePrivateProfileString strSection, strKey, strValue, strINIFile
End Sub
Reply With Quote
  #3  
Old 09-02-2009, 17:50
m31uk3
Guest
 
Posts: n/a
Default

Is there a similar way to do the same thing for xml config files?

Specifically I think it could be useful to be able to change the Pansose Preferences from within a Function/Sub...

These preferences are surprisingly enough stored here... C:\Documents and Settings\~\Application Data\Corel\Graphics13\User Draw\PanosePreferences.xm

Best,
Lukel
Reply With Quote
  #4  
Old 09-02-2009, 18:08
m31uk3
Guest
 
Posts: n/a
Default

Found this via Search...

http://forum.oberonplace.com/showthr...ghlight=Panose

However this still seems to be overridden by the settings in the PanosePreferences.xml file.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding a File in Sub Folders shelbym CorelDRAW/Corel DESIGNER VBA 10 10-05-2004 08:02


All times are GMT -5. The time now is 05:08.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com