View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Sharad Sharad is offline
external usenet poster
 
Posts: 123
Default Excel Interop Catch Event for Saving a file as xls and as xml?


The event to catch is Workbook_BeforeSave, and in excel in this event
set Cancel = True , which will cancel the normal saving done by execl,
and then invoke your own method of saving

e.g. (In excel, Thisworkbook Class)
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Cancel = True
'Your code to invoke your procedure in C
End Sub

Within Excel itself the code for what you want to do will be as under.
May be it will help you to do your code in C.
code in excel ( in Thisworkbook Class):

Public notAgain As Boolean

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Dim xlsName As String, xlsPath As String, xlsFolder As String
Dim xlmName As String, xlmPath As String, xlmFolder As String
Dim fPos As Integer
Dim fs
If notAgain Then Exit Sub
notAgain = True
Cancel = True
xlsPath = Application.GetSaveAsFilename _
(InitialFileName:="", FileFilter:="Workbook (*.xls), (*.xls)")
If xlsPath = "False" Then 'user canceled saving
notAgain = False
Exit Sub
End If
ThisWorkbook.SaveAs xlsPath
xlsName = ThisWorkbook.Name
fPos = InStr(1, xlsPath, xlsName)
xlsFolder = Left(xlsPath, fPos - 1)
xlmFolder = xlsFolder & "xml\"
Set fs = CreateObject("Scripting.FileSystemObject")
If Not fs.FolderExists(xlmFolder) Then
fs.CreateFolder (xlmFolder)
End If
fPos = InStr(1, xlsName, ".xls")
xlmName = Left(xlsName, fPos - 1) & ".xlm"
xlmPath = xlmFolder & xlmName
ThisWorkbook.SaveCopyAs xlmPath
notAgain = False
End Sub


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!