R
Rich Wallace
Hi all,
I am placing some code that will need to create and modify a single file
during the 'lifecycle' of a worksheet and I'm curious if I can make a
reference to the one file outside of all of my internal procedure and
functions.
(Is this possible?)I.E.:
When a user opens a workbook, I grab the active sheet and create an XML
document based on data within the sheet:
------------------------Begin code snippet------------------------
Sub Workbook_Open
Call CreateXML("Root")
End Sub
Public Function CreateXML(sRootName as String) As Boolean
Dim oWorkSheet As Worksheet
Dim oWorkBook As Workbook
Dim sXmlFileName As String
Dim sDivisionName As String
Set oWorkBook = ActiveWorkbook
Set oWorkSheet = ThisWorkbook.Worksheets("Sheet1")
sXMLFileName = "C:\Test.XML"
sDivisionName = oWorkBook.Names("Division").Value
iFileNum = FreeFile
Open sXMLFileName For Output As #iFileNum
Print #iFileNum, "<?xml version=""1.0""?>"
Print #iFileNum, "<Sheet1>"
Print #iFileNum, "<Inventory><Division>"
Print #iFileNum, sDivisionName
Print #iFileNum, "</Division>"
CreateXML = True
End Function
Private Sub Worksheet_Change(ByVal Target As Range)
'Trap changed Cells and write them to #iFileNum
End Sub
Sub Workbook_BeforeClose(Cancel As Boolean)
Print #iFileNum, "</Inventory>"
Print #iFileNum, "</Sheet1>"
If iFileNum > 0 Then Close #iFileNum
End Sub
------------------------End code snippet------------------------
Any ideas??
TIA
-Rich
I am placing some code that will need to create and modify a single file
during the 'lifecycle' of a worksheet and I'm curious if I can make a
reference to the one file outside of all of my internal procedure and
functions.
(Is this possible?)I.E.:
When a user opens a workbook, I grab the active sheet and create an XML
document based on data within the sheet:
------------------------Begin code snippet------------------------
Sub Workbook_Open
Call CreateXML("Root")
End Sub
Public Function CreateXML(sRootName as String) As Boolean
Dim oWorkSheet As Worksheet
Dim oWorkBook As Workbook
Dim sXmlFileName As String
Dim sDivisionName As String
Set oWorkBook = ActiveWorkbook
Set oWorkSheet = ThisWorkbook.Worksheets("Sheet1")
sXMLFileName = "C:\Test.XML"
sDivisionName = oWorkBook.Names("Division").Value
iFileNum = FreeFile
Open sXMLFileName For Output As #iFileNum
Print #iFileNum, "<?xml version=""1.0""?>"
Print #iFileNum, "<Sheet1>"
Print #iFileNum, "<Inventory><Division>"
Print #iFileNum, sDivisionName
Print #iFileNum, "</Division>"
CreateXML = True
End Function
Private Sub Worksheet_Change(ByVal Target As Range)
'Trap changed Cells and write them to #iFileNum
End Sub
Sub Workbook_BeforeClose(Cancel As Boolean)
Print #iFileNum, "</Inventory>"
Print #iFileNum, "</Sheet1>"
If iFileNum > 0 Then Close #iFileNum
End Sub
------------------------End code snippet------------------------
Any ideas??
TIA
-Rich