想要使Wd模板动态化

时间:2019-02-11 08:25:25

标签: excel vba ms-word

我有以下宏,并且我想要Wd模板(Template fisa de esantionare.dotm)使其动态化,并由不同的用户运行。谢谢

Modyfi这行代码:  .Documents.Add“ C:\ Users \ stefan.georgescu \ Desktop \ Template fisa de esantionare.dotm”

Sub ExportExcelDataToWordDocument()

    'Dim wdExcelApp As Application               'Excel is the default library (optional)
    Dim wdWordApp As Word.Application            'Word app
    Dim WB As Workbook
    Dim LastRow As Long

  Application.ScreenUpdating = False

' Creating a new instance of Word
    Set wdWordApp = New Word.Application 'instantiate a new instance of Word 2010


    With wdWordApp

        ' Making Word Visible on the screen
        .Visible = True             'iff false, document is invisible.

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' create a new Word Document based on the specified template
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        .Documents.Add "C:\Users\stefan.georgescu\Desktop\Template fisa de esantionare.dotm"

        'as before, copy the whole table from sheet to clipboard.
        Set WB = ThisWorkbook
        With WB.Sheets("GestiuneSSC")
        LastRow = .Range("A:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Range("A1", "F" & LastRow).Select
        Selection.Copy
        wdWordApp.Selection.GoTo what:=-1, Name:="bookmark1" ' -1 means "wdgotobookmark"
        wdWordApp.Selection.Paste      'paste from the clipboard to the Word Doc.

        End With

        '****************
                'as before, copy the whole table from sheet to clipboard.
        Sheets("AlimATM").Select
        Set WB = ThisWorkbook
        With WB.Sheets("AlimATM")
        LastRow = .Range("A:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Range("A1", "F" & LastRow).Select
        Selection.Copy
        wdWordApp.Selection.GoTo what:=-1, Name:="bookmark2" ' -1 means "wdgotobookmark"
        wdWordApp.Selection.Paste      'paste from the clipboard to the Word Doc.

        End With

  '****************
                'as before, copy the whole table from sheet to clipboard.
        Sheets("DepRidAngajati").Select
        Set WB = ThisWorkbook
        With WB.Sheets("DepRidAngajati")
        LastRow = .Range("A:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Range("A1", "F" & LastRow).Select
        Selection.Copy
        wdWordApp.Selection.GoTo what:=-1, Name:="bookmark3" ' -1 means "wdgotobookmark"
        wdWordApp.Selection.Paste      'paste from the clipboard to the Word Doc.

        End With

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Save WORD Document
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim TheFileName As String
        TheFileName = "C:\Users\stefan.georgescu\Desktop\Fisa de esantionare.docx"

        '......................................
        'Autofit tables in Wdoc
        Dim t As Table
        For Each t In ActiveDocument.Tables
        t.AutoFitBehavior wdAutoFitContent
        Next
        '.......................................

        '(SaveAs is for Office 2003 and earlier - deprecated)
        .ActiveDocument.SaveAs2 TheFileName
            'replaces existing .doc iff exists


        ' Close Documents and Quit Word
        .ActiveDocument.Close 'close .DOCx
        .Quit 'exit Word
    End With

  Application.ScreenUpdating = True

    'MEMORY CLEANUP
    Set wdWordApp = Nothing 'garbage collection
    'Set wdExcelApp = Nothing 'OPTIONAL


End Sub

1 个答案:

答案 0 :(得分:0)

您确实应该将模板放在所有用户都可以访问的公共位置,而不是将单独的副本放在用户的桌面上。也就是说,您可以使用:

.Documents.Add "C:\Users\" & Environ("Username") & "\Desktop\Template fisa de esantionare.dotm"