2012-10-05 29 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文檔。