2012-10-05 31 views
0

下面是我的代码如下,我的问题是我想在创建文档时把我想要的文件名..但我不知道如何。你可以帮我吗。插入宏w /所需的文件名

这里是我的代码:

Dim oWord As Word.Application 
    Dim odoc As Word.Document 
    Dim oWModule As VBIDE.VBComponent 
    Dim sCode As String 
    Dim oCommandBar As Office.CommandBar 
    Dim oCommandBarButton As Office.CommandBarControl 


    ' Create an instance of Word, and show it to the user. 
    oWord = New Word.Application() 

    ' Add a Document. 
    odoc = oWord.Documents.Add 

    ' Create a new VBA code module. 
    oWModule = odoc.VBProject.VBComponents.Item("ThisDocument") 

    sCode = "Sub FileSaveAs" & vbCr & _ 
     " msgbox ""Save As has been Disabled!"" " & vbCr & _ 
     "end sub" 

    ' Add the VBA macro to the new code module. 
    oWModule.CodeModule.AddFromString(sCode) 

    oWord.Visible = True 
    ' Set the UserControl property so that Excel does not shut down. 
    'oWord.UserControl = True 

    ' Release the variables. 
    oCommandBarButton = Nothing 
    oCommandBar = Nothing 
    oWModule = Nothing 
    odoc = Nothing 
    oWord = Nothing 

    ' Force garbage collection. 
    GC.Collect() 

回答

0

你能添加文件后运行odoc.saveas? 在PowerPoint来分配filename-

+0

正如你所看到的我禁用了保存为,所以当我创建我有一个openfiledialog和savefiledialog,这样它会被自动保存。我想用我在savefiledialog中写的名称开始我的文档。 – Andrew

0

添加的代码

' Add a Document. 
    odoc = oWord.Documents.Add 
    odoc.Name = "DesiredNameHere" 

行重命名word文档。