zhuxuhong 发表于 2005-11-23 19:44:00

[VBA]批量替换图块

<DIV>我在做一个块批量替换的程序。原先图中有二、三十种块,现在用其他的图块来替换,如GC120--&gt;10211、GC119--&gt;12111。这种对应关系我已经在ACCESS数据库中已经建立,现在就是在考虑程序运行效率的问题。我想了两种方法:</DIV>
<DIV>1)一次性将对应关系读入数组中保存,这样就只需访问数据库一次就行。替换时再从数组中找对应关系。</DIV>
<DIV>2)每次都从数据库中去找对应关系。</DIV>
<DIV>请问版主,上面两种方法,哪种应该会运行快点?还有没有其他更好的方法?</DIV>

王咣生 发表于 2005-11-23 19:57:00

<P>你可以考虑做成INI文件,格式</P>
<P><BR>GC120=10211<BR>GC119=12111<BR>GC121=12112<BR>GC122=12113<BR>GC123=12114</P>
<P>然后用API函数读取.</P>
<P>VB读取INI文件程序:</P>
<P>Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _<BR>&nbsp;&nbsp;&nbsp; (ByVal lpApplicationName As String, _<BR>&nbsp;&nbsp;&nbsp;&nbsp; ByVal lpKeyName As Any, ByVal lpDefault As String, _<BR>&nbsp;&nbsp;&nbsp;&nbsp; ByVal lpReturnedString As String, ByVal nSize As Long, _<BR>&nbsp;&nbsp;&nbsp;&nbsp; ByVal lpFileName As String) As Long</P>
<P>Public Function ReadIni(ByVal file As String, ByVal section As String, _<BR>ByVal key As String, ByVal default As String) As String<BR>&nbsp;&nbsp;&nbsp; Dim x As Long<BR>&nbsp;&nbsp;&nbsp; Dim sSection As String, sEntry As String, sDefault As String<BR>&nbsp;&nbsp;&nbsp; Dim sRetBuf As String, iLenBuf As Integer, sFileName As String<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; 'Six arguments<BR>&nbsp;&nbsp;&nbsp; sSection$ = section<BR>&nbsp;&nbsp;&nbsp; sEntry$ = key<BR>&nbsp;&nbsp;&nbsp; sDefault$ = default<BR>&nbsp;&nbsp;&nbsp; sRetBuf$ = String$(256, 0)&nbsp; '256 null character<BR>&nbsp;&nbsp;&nbsp; iLenBuf% = Len(sRetBuf$)<BR>&nbsp;&nbsp;&nbsp; sFileName$ = file<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; 'function will return a value of 12 in this case<BR>&nbsp;&nbsp;&nbsp; x = GetPrivateProfileString(sSection$, sEntry$, sDefault$, sRetBuf$, iLenBuf%, sFileName$)<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; 'sValue$ will contain the string 'PACKARD BELL'<BR>&nbsp;&nbsp;&nbsp; sValue$ = Left$(sRetBuf$, x)<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; 'return value<BR>&nbsp;&nbsp;&nbsp; ReadIni = sValue$<BR>&nbsp;&nbsp;&nbsp; <BR>End Function</P>
<P>Sub Test_ReadIni()<BR>&nbsp;&nbsp;&nbsp; MsgBox ReadIni("d:\test.ini", "TEST", "2", "null")<BR>End Sub</P>
<P>文件d:\test.ini的内容:</P>
<P><BR>1=hello<BR>2=world<BR>3=!</P>
页: [1]
查看完整版本: [VBA]批量替换图块