Appending to an XML file.

dicktimmerman

New Member
Joined
Jan 11, 2018
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi all, I'm quite new(bie) on writing XML files, so i'm hearing here to see if anyone can help (teach) me some tricks.

I have following code to write a "basic file". It is to transfer vehicle data to the authorities in Germany, therefore there is some German in it.
Point is, I do NOT want to upload 4, or other amount of files depending on the amount of registrations, separate files to that authority, I wan to send only one file with one or more records.

Code:
<container xmlns:vwn="http://www.kba.de/verwendungsnachweis">Sub WriteXMLFile()


Dim fullpath As String


    fullpath = "P:\Agricultural Equipment\04 - CoC (re)Issue\01 - (Re)issued CoC\99 - XML_Files\VWN-YANMAR-" & Format(Now(), "YYYYMMDD") & "-125687.xml"
    Set ObjXML = CreateObject("ADODB.Stream")
    ObjXML.Charset = "iso-8859-1"
    
    ObjXML.Open
    ObjXML.WriteText ("<?xml version='1.0' encoding='UTF-8'?>" & vbLf)
    ObjXML.WriteText ("<Container xmlns:vwn=http://www.kba.de/verwendungsnachweis>" & vbLf)
    ObjXML.WriteText ("     <vwn:Header>" & vbLf)
    ObjXML.WriteText ("         <vwn:Manufacturer_contact_person>" & Range("ContactPersonMFG") & "</vwn:Manufacturer_contact_person>" & vbLf)
    ObjXML.WriteText ("         <vwn:Manufacturer_email>" & Range("EmailContact").Value & "</vwn:Manufacturer_email>" & vbLf)
    ObjXML.WriteText ("         <vwn:Manufacturer>YANMAR EUROPE B.V.</vwn:Manufacturer>" & vbLf)
    ObjXML.WriteText ("         <vwn:Date_of_transmission>" & Format(Now(), "YYYY-MM-DD") & "</vwn:Date_of_transmission>" & vbLf)
    ObjXML.WriteText ("     </vwn:Header>" & vbLf)
    ObjXML.WriteText ("     <vwn:Records>" & vbLf)
    ObjXML.WriteText ("         <Recordnumber>1</Recordnumber>" & vbLf)
    ObjXML.WriteText ("         <EURecord>" & vbLf)
    ObjXML.WriteText ("             <EvidenceOfUtilizationVZF" & vbLf)
    ObjXML.WriteText ("                 <DateOfChange>" & Format(Now(), "YYYY-MM-DD") & "</DateOfChange>" & vbLf)
    ObjXML.WriteText ("                 <NumberRegistrationCertificatePart2>" & ZBIISerial & "</NumberRegistrationCertificatePart2>" & vbLf)
    ObjXML.WriteText ("                 <DateOfFillingOutRegistrationCertificatePart2>" & Format(Now(), "YYY-MM-DD") & "</DateOfFillingOutRegistrationCertificatePart2>" & vbLf)
    ObjXML.WriteText ("                 <Status>F</Status>" & vbLf)
    ObjXML.WriteText ("             <VehicleDataVZ>" & vbLf)
    ObjXML.WriteText ("                 <VehicleIdentificationNumber>" & SerialNo & "</VehicleIdentificationNumber>" & vbLf)
    ObjXML.WriteText ("                 <CheckDigitVehicleIdentificationNumber>" & CheckDigit & "</CheckDigitVehicleIdentificationNumber>" & vbLf)
    ObjXML.WriteText ("                 <CodeOfManufacturer>" & ManufCode & "</CodeOfManufacturer>" & vbLf)
    ObjXML.WriteText ("                 <CodeOfType>" & Left(TransfType, 3) & "</CodeOfType>" & vbLf)
    ObjXML.WriteText ("                 <CodeOfVariantVersion>" & Mid(TVVCode, 4, 5) & "</CodeOfVariantVersion>" & vbLf)
    ObjXML.WriteText ("             </VehicleDataVZ>" & vbLf)
    ObjXML.WriteText ("             <TechnicalDataVZ>" & vbLf)
    ObjXML.WriteText ("                 <Make>YANMAR</Make>" & vbLf)
    ObjXML.WriteText ("                 <TypeDescription>" & TransfType & "</TypeDescription>" & vbLf)
    ObjXML.WriteText ("                 <VariantDescription>" & TransfVar & "</VariantDescription>" & vbLf)
    ObjXML.WriteText ("                 <VersionDescription>" & TransfVer & "</VersionDescription>" & vbLf)
    ObjXML.WriteText ("                 <CommercialName>" & CommName & "</CommercialName>" & vbLf)
    ObjXML.WriteText ("                 <MaximumMassTechnicalPermitted>" & TPMMass & "</MaximumMassTechnicalPermitted>" & vbLf)
    ObjXML.WriteText ("                 <MinimumMassPermittedWhole>" & UnlMassMin & "</MinimumMassPermittedWhole>" & vbLf)
    ObjXML.WriteText ("                 <MaximumMassPermittedWhole>" & UnlMassMax & "</MaximumMassPermittedWhole>" & vbLf)
    ObjXML.WriteText ("                 <NumberTypeApproval>" & EUTypeApprNo & "</NumberTypeApproval>" & vbLf)
    ObjXML.WriteText ("                 <DateTypeApproval>" & Format(EUTypeApprReleaseDate, "YYYY-MM-DD") & "</DateTypeApproval>" & vbLf)
    ObjXML.WriteText ("                 <NumberOfAxles>" & QtyAxles & "</NumberOfAxles>" & vbLf)
    ObjXML.WriteText ("                 <TowableMassBraked>" & InerBrTrailMass & "</TowableMassBraked>" & vbLf)
    ObjXML.WriteText ("                 <TowableMassUnbraked>" & UnbrTrailMass & "</TowableMassUnbraked>" & vbLf)
    ObjXML.WriteText ("                 <Capacity>" & EngCap & "</Capacity>" & vbLf)
    ObjXML.WriteText ("                 <MaximumPower>" & Round(MAX_Net_PWR, 0) & "</MaximumPower>" & vbLf)
    ObjXML.WriteText ("                 <RatedSpeed>" & Max_PWR_Speed & "</RatedSpeed>" & vbLf)
    ObjXML.WriteText ("                 <NumberOfSeats>1</NumberOfSeats>" & vbLf)
    ObjXML.WriteText ("                 <MaximumSpeed>" & Round(FWD_Dsgn_Spd, 0) & "</MaximumSpeed>" & vbLf)
    ObjXML.WriteText ("                 <SoundLevelStationary>" & StationarySNDLevel & "</SoundLevelStationary>" & vbLf)
    ObjXML.WriteText ("                 <SoundLevelEngineSpeed>" & SNDEngineSpeed & "</SoundLevelEngineSpeed>" & vbLf)
    ObjXML.WriteText ("                 <SoundLevelDriveBy>" & MovingSNDLevel & "</SoundLevelDriveBy>" & vbLf)
    ObjXML.WriteText ("                 <MaximumMassAxle1>" & MaxMassAxle1 & "</MaximumMassAxle1>" & vbLf)
    ObjXML.WriteText ("                 <MaximumMassAxle2>" & MaxMassAxle2 & "</MaximumMassAxle2>" & vbLf)
    ObjXML.WriteText ("                 <MaximumMassAxlePermitted1>" & MaxMassAxle1 & "</MaximumMassAxlePermitted1>" & vbLf)
    ObjXML.WriteText ("                 <MaximumMassAxlePermitted2>" & MaxMassAxle2 & "</MaximumMassAxlePermitted2>" & vbLf)
    ObjXML.WriteText ("                 <NumberPoweredAxles>" & QtyAxles & "</NumberPoweredAxles>" & vbLf)
    ObjXML.WriteText ("                 <StaticMass>" & VloadMax & "</StaticMass>" & vbLf)
    ObjXML.WriteText ("                 <TyreAxle1>" & FrontwheelMTD & "</TyreAxle1>" & vbLf)
    ObjXML.WriteText ("                 <TyreAxle2>" & RearwheelMTD & "</TyreAxle2>" & vbLf)
    ObjXML.WriteText ("                 <MinimumLength>" & LengthMin & "</MinimumLength>" & vbLf)
    ObjXML.WriteText ("                 <MaximumLength>" & LengthMax & "</MaximumLength>" & vbLf)
    ObjXML.WriteText ("                 <MinimumWidth>" & WidthMin & "</MinimumWidth>" & vbLf)
    ObjXML.WriteText ("                 <MaximumWidth>" & WidthMax & "</MaximumWidth>" & vbLf)
    ObjXML.WriteText ("                 <MinimumHeight>" & HeightMin & "</MinimumHeight>" & vbLf)
    ObjXML.WriteText ("                 <MaximumHeight>" & HeightMax & "</MaximumHeight>" & vbLf)
    ObjXML.WriteText ("                 </TechnicalDataVZ>" & vbLf)
    ObjXML.WriteText ("             </EvidenceOfUtilizationVZF>" & vbLf)
    ObjXML.WriteText ("         </EURecord>" & vbLf)
    ObjXML.WriteText ("     </vwn:Records>" & vbLf)
    ObjXML.SaveToFile fullpath, 2
    ObjXML.Close
<container xmlns:vwn="http://www.kba.de/verwendungsnachweis"><container xmlns:vwn="http://www.kba.de/verwendungsnachweis"><container xmlns:vwn="http://www.kba.de/verwendungsnachweis">

For each vehicle I write (via Excel, VBA and word) a Certificate of Conformity, the data used in this Co is basis for the XML file.
If I have more than one vehicle I have to add "<recordnumber>X</recordnumber>" and a repetition of the lines <eurecord> upto and enclosing </eurecord>.

Since I have NO idea at all, i'm asking for help here. Please take in account that i'm not an educated programmer, I've learned by google search.
Any suggestion on streamlining the initial code is welcome. Never to old to learn.</container></container></container></container>
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I noticed that a lot of XML code I've written has been gone, I've used the "
Code:
", is there another way to show XML Code?
 
Upvote 0

Forum statistics

Threads
1,214,568
Messages
6,120,272
Members
448,953
Latest member
Dutchie_1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top