< návrat zpět
MS Excel
Téma: copyfile-rename 
Zaslal/a fortes 22.12.2019 14:42
dobrý den, našel jsem VBA co mě funguje jak má. jde mě o to že VBA upravý originální soubor a já bych potřeboval aby vytvořil kopii souboru a ten upravil. nevím jak na to
Private Sub CommandButton2_Click()
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
'File Path of Text File
FilePath = Cells(5, 1) & ListBox1.Value & "\" & ListBox2.Value
'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile
'Open the text file in a Read State
Open FilePath For Input As TextFile
'Store file content inside a variable
FileContent = Input(LOF(TextFile), TextFile)
FilePath = FilePath & "_"
'Clost Text File
Close TextFile
'Find/Replace
FileContent = Replace(FileContent, "L M30", ";edit")
'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile
'Open the text file in a Write State
Open FilePath For Output As TextFile
'Write New Text data to file
Print #TextFile, FileContent
'Close Text File
Close TextFile
End Sub
Vikous(29.12.2019 20:50)#045396 
Dobrý den, postup uvedený v
https://office.lasakovi.com/excel/vba-soubory/excel-vba-kopirovat-prejmenovat-soubory/ by nepomohl?
Po zkopírování souboru by pak mohlo stačit dát podmínku aby makro otevřelo kopírovaný soubor a v něm provedlo potřebné kroky.
citovat
fortes(30.12.2019 8:22)#045397 
zkoušel jsem a jediné na co sem dokázal přijít, že jsem přejmenoval koncovku souboru
FilePath = FilePath & "_"
citovat
Vikous(30.12.2019 15:37)#045404 
fortes napsal/a:
zkoušel jsem a jediné na co sem dokázal přijít, že jsem přejmenoval koncovku souboru
FilePath = FilePath & "_"
viz. zpráva ve vzkazech - zkusím na to pak více mrknout a snad se něco podaří vymyslet:)
citovat
elninoslov(31.12.2019 1:47)#045413 
Tak napr.
Private Sub CommandButton2_Click()
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
Dim ExtensionLenght As Byte
'File Path of Text File
FilePath = Cells(5, 1) '& ListBox1.Value & "\" & ListBox2.Value
'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile
'Open the text file in a Read State
Open FilePath For Input As TextFile
'Store file content inside a variable
FileContent = Input(LOF(TextFile), TextFile)
ExtensionLenght = Len(FilePath) - InStrRev(FilePath, ".") + 1
FilePath = Left$(FilePath, Len(FilePath) - ExtensionLenght) & "_" & Right$(FilePath, ExtensionLenght)
'Clost Text File
Close TextFile
'Find/Replace
FileContent = Replace(FileContent, "L M30", ";edit")
'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile
'Open the text file in a Write State
Open FilePath For Output As TextFile
'Write New Text data to file
Print #TextFile, FileContent
'Close Text File
Close TextFile
End Subcitovat
marjankaj(31.12.2019 12:16)#045415 
A čo tak najprv vytvoriť si kopiu toho súboru a ten môžeš potom upravovať?
citovat
fortes(31.12.2019 12:39)#045416 
děkuju, elninoslov vyřešil problém v naprostou spokojenost. vědět co potřebuju udělat je jedna věc, ale převézt tu myšlenku do praxe do VBA už je složitější, samozřejmě jak pro koho :-) j¨mám ještě další představy co vylepšit takže budu bojivat dál, ale funkcionalita je vyřešená.
citovat