插入具有所需文件名的宏

时间:2012-10-05 09:24:11

标签: vb.net visual-studio-2010 vba ms-office

下面是我的代码,我的问题是我想在创建文档时输入我想要的文件名..但我不知道如何。你能帮帮我吗?

这是我的代码:

    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()

2 个答案:

答案 0 :(得分:0)

添加文档后可以运行odoc.saveas吗? 在Powerpoint中,分配文件名 -

答案 1 :(得分:0)

添加代码行

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

重命名word文档。