2017-03-02 78 views
0

我想用「創建發票」按鈕在Access中創建一個表單。我試着讓它做到以下幾點:VBA通​​過訪問輸入表格在Excel中創建發票

  • 複製excel「invoice template」;

  • 將excel「發票模板」從文件夾「模板」移動到文件夾「發票」;

  • 打開「發票發送寄存器」(excel文件);

  • 打開「customer adresses register」(excel文件);

  • 將發票模板副本重命名爲可以在文本框中輸入的內容;

  • 等一下;

  • 打開新創建的excel文件,在excel中填寫模板。

這是我想出了代碼:

Private Sub Knop1_Click() 

    'Open register with customer adresses 
    Dim xlApp As Object 
    Set xlApp = CreateObject("Excel.Application") 
    xlApp.Visible = True 
    xlApp.Workbooks.Open "\\WDMyCloudEX4\Zakelijk\Documenten\Betalingen\Faktuur adressen.xlsx", True, False 
    Set xlApp = Nothing 

    'Open folder with invoices 
    Call Shell("explorer.exe" & " " & "\\WDMyCloudEX4\Zakelijk\Documenten\SFMT\Afleveringsbonnen 2008-heden\2017\Nieuwe systeem (1-3-2017)", vbNormalFocus) 

    'Make a copy of the invoice template 
    FileCopy "\\WDMyCloudEX4\Zakelijk\Documenten\SFMT\Afleveringsbon.xlsx", "\\WDMyCloudEX4\Zakelijk\Documenten\SFMT\Afleveringsbonnen 2008-heden\2017\Nieuwe systeem (1-3-2017)\Nieuwe afleveringsbon.xlsx" 

    'Define to what the invoice template should be renamed 
    Year = [Tekst3] 
    Month = [Tekst6] 
    Count = [Tekst9] 
    NEWNAME = Year + Month + Count 

    'Move and rename the invoice template copy 
    Name "\\WDMyCloudEX4\Zakelijk\Documenten\SFMT\Afleveringsbonnen 2008-heden\2017\Nieuwe systeem (1-3-2017)\Nieuwe afleveringsbon.xlsx" As "\\WDMyCloudEX4\Zakelijk\Documenten\SFMT\Afleveringsbonnen 2008-heden\2017\Nieuwe systeem (1-3-2017)\P95+$NEWNAME.xlsx" 

    'Wait a second to make sure the copiing is completed 
    Sleep (1000) 

    'Open the moved and renamed invoice template copy 
    Dim xlApp As Object 
    Set xlApp = CreateObject("Excel.Application") 
    xlApp.Visible = True 
    xlApp.Workbooks.Open "\\WDMyCloudEX4\Zakelijk\Documenten\SFMT\Afleveringsbonnen 2008-heden\2017\Nieuwe systeem (1-3-2017)\P95+telling", True, False 
    Set xlApp = Nothing 

End Sub 

任何幫助將是非常歡迎的,這是推動我瘋了...我一直在試圖現在小時。

+0

以上代碼的哪個區域與您有問題? –

回答

0

您沒有說明實際的問題是什麼,但我認爲,問題的至少一部分是與此代碼:

'Open register with customer adresses 
Dim xlApp As Object 
Set xlApp = CreateObject("Excel.Application") 
xlApp.Visible = True 
xlApp.Workbooks.Open "\\WDMyCloudEX4\Zakelijk\Documenten\Betalingen\Faktuur adressen.xlsx", True, False 
Set xlApp = Nothing 

在此代碼創建的是Excel應用程序對象,並打開一個Excel工作簿通過該對象,然後在打開工作簿後立即銷燬應用程序對象。因此,您正在有效打開並立即關閉工作簿。