VBA Print not printing last row

qnguyen

New Member
Joined
Aug 7, 2019
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I am trying to print an XML file with contents from several cells. The XML file needs a beginning and end wrapper, however the end wrapper is never appearing.

Here is the code I wrote

VBA Code:
Dim XMLFile As Integer, FilePath As Variant
    FilePath = Application.GetSaveAsFilename(InitialFileName:=Format(Date, "yyyy-m-d") & "_Promotion_Import", FileFilter:="Xml (*.xml), *.xml")
   
    If FilePath = False Then Exit Sub
   
    XMLFile = FreeFile
   
    Open FilePath For Output As XMLFile
   
    Print #XMLFile, "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbNewLine & "<promotions xmlns=""http://www.demandware.com/xml/impex/promotion/2008-01-31"">"
    CurrentRow = 2
    With Worksheets("XML Output")
        Do While CurrentRow <= 5
            Print #XMLFile, .Range("A" & CurrentRow)
            Print #XMLFile, .Range("B" & CurrentRow)
            CurrentRow = CurrentRow + 1
        Loop
    End With
    Print #XMLFile, "</promotions>" 'This is the line that should be printed, but is not
    Close TextFile

The last line of the file should be "</promotions>", but this never seems to actually be written in the file.

Edit: When I checked the file again after ~10 minutes, the last line DOES appear. But it seems to require a few minutes to actually be written in. Notepad++ notified me that the file was changed and needed to be reloaded. Not sure why it takes so long to write this last line.
 

Some videos you may like

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

jimrward

Well-known Member
Joined
Feb 24, 2003
Messages
1,784
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
Check your close statement as to which file you are closing
 

Watch MrExcel Video

Forum statistics

Threads
1,127,331
Messages
5,624,057
Members
416,007
Latest member
csf

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
Top