borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Unexpected behavior with for each loop)Next Topic (VBA Row Number) New Topic New Poll Post Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : saving files using macro
Poster Message
yenfeng
Level: Protégé

Registered: 06-03-2009
Posts: 4

icon saving files using macro

Hi..
I am facing a problem now with vbscript as I am new to this program and really would appreciate it if you could render me some help.
I would like to save particular sheets in an excel file as mdi files and save it into specific folder.
I had succesfully saved 3 files but I have no idea why the last file couldn't be saved no matter how I edited the code.
Please help me..
Code as below...
Option Explicit

Public Function MonthDays(myMonth As Long) As Long
MonthDays = Day(DateSerial(Year(Date), myMonth + 1, 1) - 1)

End Function
Sub save()
'
' saving Macro
' Macro recorded 4/8/2008 by superuser
'
ActiveWorkbook.save
Sheets(Array("HL_by Team", "HL_Pipline", "OD Sales")).Select
With ActiveSheet.PageSetup
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="Microsoft Office Document Image Writer", Collate:=True, PrToFileName:="U:\Assets Intranet\Leasing\Daily\Leasing Sales MIS(Daily).mdi"
Dim WSName As String, CName As String, Directory As String, savename As String, Cmonth As String, mthfolder As String, WSNameMth As String
WSName = "HL_Pipeline"
CName = "A5"
WSNameMth = "HL_Pipeline"
Cmonth = "A68"
Directory = "U:\Liabilities\secured Loans\Leasing Loans\Daily Update\2009\"
savename = Sheets(WSName).Range(CName).Text
mthfolder = Sheets(WSNameMth).Range(Cmonth).Text
On Error GoTo errorsub:
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="Microsoft Office Document Image Writer", Collate:=True, PrToFileName:=Directory & mthfolder & "\" & savename & ".mdi"
ActiveWorkbook.save
Sheets(Array("Daily MIS")).Select
With ActiveSheet.PageSetup
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="Microsoft Office Document Image Writer", Collate:=True, PrToFileName:="U:\Assets Intranet\Leasing\Daily\HL Portfolio MIS.mdi"

Dim WSName1 As String, CName1 As String, Directory1 As String, savename1 As String, Cmonth1 As String, mthfolder1 As String, WSNameMth1 As String, Text As String
WSName1 = "Daily MIS"
Text = "B5"
CName1 = "HL Portfolio MIS" & Text
WSNameMth1 = "Daily MIS"
Cmonth1 = "A68"
Directory1 = "U:\Liabilities\Secured Loans\Leasing Loans\Daily Update\2009\"
savename1 = Sheets(WSName1).Range(CName1).Text
mthfolder1 = Sheets(WSNameMth1).Range(Cmonth1).Text
On Error GoTo errorsub:
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="Microsoft Office Document Image Writer", Collate:=True, PrToFileName:=Directory1 & mthfolder1 & "\" & savename1 & ".mdi"

errorsub:
Beep
MsgBox "Changes not saved!", vbExclamation, Title:=savename & ".xls"
'
End Sub
The first 3 files can be saved but not the last file and I have no idea why. A5, B5 and A68 represents the name I want to save as part of my file name.
Please help.
Thanks!

24-03-2009 at 02:48 AM
View Profile Send Email to User Show All Posts | Quote Reply
patkam
Level: Scholar

Registered: 19-04-2004
Posts: 36
icon Re: saving files using macro

Please verify:

Are you absolutely sure that CName = "A5" and Cmonth = "A68" applies to all the worksheets?

If one of these cell's output (.text) is empty or NULL then there's no way Excel nor Microsoft Office Document Image Writer can save a file without a name.

____________________________
Knowledge is there to share, not to hide!
-< PATKAM >-

14-04-2009 at 03:31 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : saving files using macro
Previous Topic (Unexpected behavior with for each loop)Next Topic (VBA Row Number) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder