12. september 2001 - 10:17Der er
7 kommentarer og 1 løsning
Indlæsning af fil i word-skabelon
Jeg har brug for lidt visual basic kode der kan indlæse nogle linier fra en tekstfil. Oplysningerne fra linierne skal bruges til at sætte nogle forvalgte værdier på en række felter i word-dokumentet.
Jeg kan ikke finde eksempler på filindlæsning nogen steder. Hjælp ønskes og belønnes.
Det ganske nemt, start med at sætte bogmærker der hvor teksten skal indsættes, optag din makro og tildel den en kontroltast. For at hjælpe brugerne, kan du evt. skrive en vejledning i skabelonen med skjult tekst (der ikke udskrives). Jeg bruger ofte denne metode. \\\\AMH
Jeg ønsker at kunne indlæse en tekstfil (fx en lidt .INI lignende fil), og de strenge jeg får derfra, vil jeg kunne tildele som værdier til enkelte adskilte formularfelter i skabelonen, fx. ved document_new hændelsen.
AMH>> OK :-) vi har alle grænser. I dette tilfælde har jeg frisk viden, idet jeg lige har lavet to tilføjelsesprogrammer til Word, som bruger INI filer til navne i Menu\'er (bl.a på forskellige sprog) samt nogle flere ting.
Du kender selvfølgelig kommandoen System.PrivateProfileString - right ? Den løser jo nok ikke dit problem, idet du vil have alle Keys i en Section indlæst i f.eks. en Combobox - right ?
Jeg har ikke koden her, men jeg skal lige se, om jeg kan finde den på nettet - ellers når jeg kommer hjem.
Jeg fandt det - det har virket fint for mig, at bruge noget af disse koder på min egen måde - prøv ad. Yderligere hjælp via fd@win-consult.com ------------------------------------------------ Eventhough, you can read out INI file sequentially, certain API calls will also get you all sections/keys of INI files.
The belowmentioned API calls result in following test routine in which array: arrSections is populated with all sections in sINIFile using function GetSections and array arrKeys is populated with all Keys in same INI file using function GetKeys.
Sub Test() Dim arrSections, arrKeys, x, y Dim sINIFile As String sINIFile = \"d:\\projects\\ella factuur\\factuurnummer.ini\" arrSections = GetSections(sINIFile)
For x = 0 To UBound(arrSections) Debug.Print \"section no: \" & (x + 1) Debug.Print arrSections(x) Debug.Print \"keys:\" Debug.Print \"------\" arrKeys = GetKeys(sINIFile, arrSections(x)) For y = 0 To UBound(arrKeys) Debug.Print \"key no: \" & (y + 1) Debug.Print arrKeys(y) Next Erase arrKeys Next End Sub
Paste the following in a standard module and you can use GetKeys and GetSections to retrieve all entries.
Public Declare Function GetPrivateProfileSection Lib \"kernel32\" _ Alias \"GetPrivateProfileSectionA\" _ (ByVal sSectionName As String, _ ByVal sReturnedString As String, _ ByVal lSize As Long, _ ByVal sFileName As String) As Long
Public Declare Function GetPrivateProfileString Lib \"kernel32\" _ Alias \"GetPrivateProfileStringA\" _ (ByVal sSectionName As String, _ ByVal sKeyName As String, _ ByVal sDefault As String, _ ByVal sReturnedString As String, _ ByVal lSize As Long, _ ByVal sFileName As String) As Long
Public Declare Function GetProfileSection Lib \"kernel32\" _ Alias \"GetProfileSectionA\" _ (ByVal sSectionName As String, _ ByVal sReturnedString As String, _ ByVal lSize As Long) As Long
Public Declare Function GetProfileString Lib \"kernel32\" _ Alias \"GetProfileStringA\" _ (ByVal sSectionName As String, _ ByVal sKeyName As String, _ ByVal sDefault As String, _ ByVal sReturnedString As String, _ ByVal lSize As Long) As Long
Private m_sINIFile As String \'INIFile
\'Defines the buffer size for the return values from various API calls. Private Const INITIAL_LENGTH As Long = 1000 \'Assumed max. length of buffer Private Const ADDITIONAL_LENGTH As Long = 2000 \'Buffer length increment
Public Sub PadName(ByRef Name As String) On Error GoTo ER \'If the name is \"\", then make it \" \". If Len(Name) = 0 Then Name = \" \" End If Exit Sub ER: End Sub
Private Function GetStringValue(ByVal INIFile As String, _ ByVal SectionName As String, _ ByVal KeyName As String, _ ByVal DefaultValue As String) As String On Error GoTo ER
Dim sResult As String Dim lResult As Long Dim lLength As Long
\'Assume the result\'s maximum length. lLength = INITIAL_LENGTH
Do \'Create the buffer for the return value. sResult = Space$(lLength)
\'Check to see that the value fit into the buffer. If (lResult + 2) <> lLength Then Exit Do End If
\'Although there\'s a chance that the result fits _exactly_ \'into the buffer, we should try again with a larger buffer. lLength = lLength + ADDITIONAL_LENGTH Loop \'Return only the used portion of the buffer. GetStringValue = Left$(sResult, lResult) Exit Function ER: \'There was some kind of problem, return the default value. GetStringValue = DefaultValue End Function
Public Function GetSections(ByVal INIFile As String) As Variant Dim sSections As String Dim lSections As Long
On Error GoTo ER
\'Fetch a list of section names. sSections = GetStringValue(INIFile, vbNullString, vbNullString, \"\")
\'If there are no section names, return an array with no elements. lSections = Len(sSections) If lSections <= 1 Then GoTo ER Else \'Remove the _*one*_ trailing NULL characters. Although all lists returned \'by the API calls end in a double-NULL, this is the only circumstances \'where the return length includes both of them - it normally only includes \'only one. sSections = Left$(sSections, lSections - 1) End If
\'Split the list of section names into an array of strings. GetSections = Split(sSections, Chr$(0)) Exit Function
ER: \'There was some kind of problem, return an empty array. GetSections = Array() End Function
Public Function GetKeys(ByVal INIFile As String, _ ByVal SectionName As String) As Variant Dim sKeys As String Dim lKeys As Long
On Error GoTo ER \'Prevent the Section name from being misinterpreted as vbNullString. PadName SectionName
\'Fetch a list of keys. sKeys = GetStringValue(INIFile, SectionName, vbNullString, \"\")
\'If there are no keys, return an array with no elements. lKeys = Len(sKeys) If lKeys = 0 Then GoTo ER Else \'Remove the trailing NULL character. sKeys = Left$(sKeys, lKeys - 1) End If
\'Split the list of keys into an array of strings. GetKeys = Split(sKeys, Chr$(0))
Exit Function ER: \'There was some kind of problem, return an empty array. GetKeys = Array() End Function ---------------------------------------------------------
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.