deleting last character in xml file

L

Laura

I’m trying to make a macro in access that looks in a specific directory if
the number of pdf files is greater than 1. In this case, I have to open the
..xml document, that is placed in another directory, and delete the last
character (and close the document).
I don´t know how to access the last position (ok, with the EOF() function,
but I don’t know how to delete easyly (like in word))
Do you know if there is any fast-method to delete this?
Thank you!!

With Application.FileSearch
.FileName = "*.pdf"
.LookIn = "C:\OUT\WP51\" & directorio & "PDF"
.SearchSubFolders = True
.Execute
If .FoundFiles.Count > 1 Then
MsgBox ("nº of found documents more than one")
Dim nom As String
With Application.FileSearch
.FileName = "*.xml"
.LookIn = "C:\OUT\WP51\" & directorio & ""
.SearchSubFolders = False
.Execute

If .FoundFiles.Count > 0 Then
nom = .FoundFiles.Item(1)
Open nom For Random As #2

MsgBox ("document open")

'???
Close #1

Else
MsgBox ("the xml file doesn’t exist")
End If
End With
Else
MsgBox ("Nº of documents less than one")
End If
End With
 
S

Steve Yandl

Laura,

For something like this, I'd borrow an object from scripting and use the
FileSystemObject. Read the xml file into a text string, use the split
function with vbCrLf as a separator to create an array with the lines of
your xml file being the elements of the array. ReOpen the xml file for
writing, write back all but the last line, trim the last character of the
last line and right that back as well and close the xml file.

The example below can be incorporated into your VBA routine although the
example is actually the contents of a vbs file that I modified for your
specific example. It is set to remove the last character of the last line
of an xml file named fileA.xml, located in C:\Test.

____________________________

Const ForReading = 1
Const ForWriting = 2

Set FSO = CreateObject("Scripting.FileSystemObject")
Set objXMLfile = FSO.OpenTextFile("c:\Test\fileA.xml", ForReading)

strContents = objXMLfile.ReadAll
objXMLfile.Close

arrLines = Split(strContents, vbCrLf)

Set objXMLfile = FSO.OpenTextFile("c:\Test\fileA.xml", ForWriting)

For i = 0 to UBound(arrLines) - 1
objXMLfile.WriteLine arrLines(i)
Next

strLastLine = arrLines(UBound(arrLines))
strLastLine = Left(strLastLine, Len(strLastLine) - 1)
objXMLfile.WriteLine strLastLine

objXMLfile.Close

Set objXMLfile = Nothing
Set FSO = Nothing
_____________________________

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top