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.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Check your close statement as to which file you are closing
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,168
Members
448,870
Latest member
max_pedreira

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