VBA的例子:
Function CreateNewWorkbook(Optional strBookName As String = "", _
Optional intNumSheets As Integer = 3) As Workbook
' This procedure creates a new workbook file and saves it by using the path
' and name specified in the strBookName argument. You use the intNumsheets
' argument to specify the number of worksheets in the workbook;
' the default is 3.
Dim intOrigNumSheets As Integer
Dim wkbNew As Excel.Workbook
On Error GoTo CreateNew_Err
intOrigNumSheets = Application.SheetsInNewWorkbook
If intOrigNumSheets <> intNumSheets Then
Application.SheetsInNewWorkbook = intNumSheets
End If
Set wkbNew = Workbooks.Add
If Len(strBookName) = 0 Then strBookName = Application.GetSaveAsFilename
wkbNew.SaveAs strBookName
Set CreateNewWorkbook = wkbNew
Application.SheetsInNewWorkbook = intOrigNumSheets
CreateNew_End:
Exit Function
CreateNew_Err:
Set CreateNewWorkbook = Nothing
wkbNew.Close False
Set wkbNew = Nothing
Resume CreateNew_End
End Function
Function CreateNewWorkbook(Optional strBookName As String = "", _
Optional intNumSheets As Integer = 3) As Workbook
' This procedure creates a new workbook file and saves it by using the path
' and name specified in the strBookName argument. You use the intNumsheets
' argument to specify the number of worksheets in the workbook;
' the default is 3.
Dim intOrigNumSheets As Integer
Dim wkbNew As Excel.Workbook
On Error GoTo CreateNew_Err
intOrigNumSheets = Application.SheetsInNewWorkbook
If intOrigNumSheets <> intNumSheets Then
Application.SheetsInNewWorkbook = intNumSheets
End If
Set wkbNew = Workbooks.Add
If Len(strBookName) = 0 Then strBookName = Application.GetSaveAsFilename
wkbNew.SaveAs strBookName
Set CreateNewWorkbook = wkbNew
Application.SheetsInNewWorkbook = intOrigNumSheets
CreateNew_End:
Exit Function
CreateNew_Err:
Set CreateNewWorkbook = Nothing
wkbNew.Close False
Set wkbNew = Nothing
Resume CreateNew_End
End Function
解决方案 »
- 如何 响应 点击ctListBar1 的 一级 和 二级菜单?
- vb6如何处理unicode编码的字符呀?
- 有没有办法监控某一类应用程序打开了ACCESS数据库,具体是那个数据库,请高手指点
- vb写的dll能否在非管理员用户下注册
- 真奇怪! 用visdata 生成的access 表, 用visdata打开,再用dbgird 访问时, 第一个字段的第一个数据项为空。试过两个数据表也是这样。
- 求高手。利用鼠标控制桌面背景切换。关机等
- 怎样取得系统年份??
- 请教!!数据库问题 100分
- 用报表设计器如何动态实现报表的横打,竖打的自动选择选择
- 我想问一个关于制作安装的问题?
- 老板又有新要求了,同志们帮帮忙!
- ★ ◆ ☆ ◇ ★如何用VB调用屏保程序,并且自动写入一个屏保的密码,还有就是如何用程序终止该屏保???
dim mydoc As Word.Document
dim fpath as string
fpath = "...."
Set appwd = CreateObject("word.application") '创建 word application 对象
With appwd
.Visible = False
Set mydoc = .Documents.Open(fpath) '调用word文档
mydoc.Activate
.....
mydoc.SaveAs Trim(fpath)
.......
end with
http://msdn.microsoft.com/code/default.asp?url=/code/sample.asp?url=/msdn-files/026/000/006/msdncompositedoc.xml
'Synopsis: Export (save) a VBA Project from the IDE to the Exporter
'
'Function input: VBProj - [IN] The VB Project to export
' ApplicationGUID - [IN] The MT Project application object GUID
' ApplicationRegPath - [IN] The MT Project registry path
'
'Function output: -
'
'Possible errors:
' icNotStandalone
' icAmbiguousDesigner
' icUnknownComponent
' icCompileFail
' icIOError
' icRegisterServerFail '======================================================================= Public Function ExportVBAProject(ByRef VBProj As VBIDE.VBProject, _
ByVal ApplicationGUID As String, _
ByVal ApplicationRegPath As String) As Boolean
Dim VBComp As VBIDE.VBComponent
Dim TheStream As Stream
Dim NumDesigners As Integer
Dim FileSystem As New Scripting.FileSystemObject
Dim OriginalInProcServ As String
Dim TmpFileRoot As String
Dim ProjRef As Variant
TmpFileRoot = TempFileName(FileSystem.GetTempName)
TmpFileRoot = Left(TmpFileRoot, (Len(TmpFileRoot) - 4))
' Only works for standalone projects
'
If (VBProj.Type <> vbext_pt_StandAlone) Then
ExportVBAProject = False
Err.Raise icNotStandalone, scErrSource, scErrNotStandalone
Exit Function
End If
' Setup temp directory and remove any existing files from export folder
'
CleanDir m_sTmpDir
' Get the prog ID from the Project and designer names
'
NumDesigners = 0
For Each VBComp In VBProj.VBComponents
If (VBComp.Type = vbext_ct_ActiveXDesigner) Then
msVBProjProgID = VBProj.Name & "." & VBComp.Name
NumDesigners = NumDesigners + 1
End If
Next
If (NumDesigners <> 1) Then
Err.Raise icAmbiguousDesigner, scErrSource, scErrAmbiguousDesigner
End If
' Get the inproc server for the ProgID before registering anything. This mean it can be
' put back afterwards
'
GetMTProjectInfo ProgId:=msVBProjProgID, _
RegPath:=ApplicationRegPath, _
InProcServ:=OriginalInProcServ
' Create the DLL
'
VBProj.BuildFileName = TmpFileRoot & ".DLL"
On Error GoTo MakeCompiledFileFailed
VBProj.MakeCompiledFile
On Error GoTo 0
' Get the new FriendlyName before unregistering
'
GetMTProjectInfo ProgId:=msVBProjProgID, _
RegPath:=ApplicationRegPath, _
FriendlyName:=msFriendlyName
' Unregister the DLL, these should not be registered on the local machine
'
UnregisterDLL TmpFileRoot & ".DLL"
' Reregister original DLL for this ProgID if there was one
'
If (OriginalInProcServ <> "") Then
RegisterDLL OriginalInProcServ
End If ' Cache some of these as read only properties
'
msVBProjApplicationRegPath = ApplicationRegPath
' Write VBA Project details to separate stream
'
On Error GoTo WriteError
Set TheStream = moTheStorage.CreateStream("THE_PROPERTIES", axAccessWrite)
TheStream.Put icVBA_PROPERTIES
TheStream.Put ApplicationGUID
TheStream.Put msVBProjApplicationRegPath
TheStream.Put msFriendlyName
TheStream.Put msVBProjProgID
#If APC62 Then
#Else
TheStream.Put VBProj.BuildFileName
TheStream.Put VBProj.Description
TheStream.Put VBProj.HelpContextID
TheStream.Put VBProj.HelpFile
TheStream.Put VBProj.Name
#End If
Set TheStream = Nothing
#If APC62 Then
Set TheStream = moTheStorage.CreateStream("THE_PROJECTFILE", axAccessWrite)
TheStream.Put icVBA_PROJECTFILE
VBProj.SaveAs TmpFileRoot & ".VBA"
StreamFromFile TmpFileRoot & ".VBA", TheStream
Set TheStream = Nothing
#Else
' Write VBA Project reference typelib details to separate stream
'
Set TheStream = moTheStorage.CreateStream("THE_REFERENCES", axAccessWrite)
TheStream.Put icVBA_REFERENCES
TheStream.Put VBProj.References.Count
For Each ProjRef In VBProj.References
TheStream.Put ProjRef.Guid
TheStream.Put ProjRef.Major
TheStream.Put ProjRef.Minor
Next
Set TheStream = Nothing
' Loop over the VBA Project contents exporting each bit and saving it to a stream
'
For Each VBComp In VBProj.VBComponents
Dim TmpFile As String
TmpFile = FileSystem.GetTempName
' Export VBA code
'
VBComp.Export TmpFileRoot & ".TMP"
' VBA code type specific stuff, some exports create two files
'
Set TheStream = moTheStorage.CreateStream(VBComp.Name, axAccessWrite)
TheStream.Put VBComp.Type
Select Case VBComp.Type
Case icVBA_DESIGNER
StreamFromFile TmpFileRoot & ".TMP", TheStream
StreamFromFile TmpFileRoot & ".DSX", TheStream
TheStream.Put VBComp.Name
Case icVBA_MSFORM
StreamFromFile TmpFileRoot & ".TMP", TheStream
StreamFromFile TmpFileRoot & ".FRX", TheStream
Case icVBA_MODULE
StreamFromFile TmpFileRoot & ".TMP", TheStream
Case icVBA_CLASS
StreamFromFile TmpFileRoot & ".TMP", TheStream
Case Else
' This should never happen
Set TheStream = Nothing
ExportVBAProject = False
Err.Raise icUnknownComponent, scErrSource, scErrUnknownComponent
End Select
Set TheStream = Nothing
Next
#End If ' Write the MT Project DLL to the stream too
'
Set TheStream = moTheStorage.CreateStream("THE_DLL", axAccessWrite)
TheStream.Put icVBA_DLL
StreamFromFile TmpFileRoot & ".DLL", TheStream
Set TheStream = Nothing
moTheStorage.Commit True
ExportVBAProject = True
Exit Function
MakeCompiledFileFailed:
Set TheStream = Nothing
ExportVBAProject = False
' Re-describe automation errors, pass through other, more descriptive errors.
' Note, the HRESULT is E_FAIL in both cases.
'
Err.Raise icCompileFail, scErrSource, scErrCompileFail
Exit Function
WriteError:
Set TheStream = Nothing
ExportVBAProject = False
Err.Raise icIOError, scErrSource, scErrIOError
End Function
http://msdn.microsoft.com/code/default.asp?url=/msdn-files/026/000/006/Servers/Visual%20Basic/Export/export_cls.asp