我正在研究发票自动化项目,这里我为此设计了代码但是我遇到问题运行时错误代码显示在特定位置找不到文件
代码是按照Workbooks.Open ("C:\invoicesample\invoice.xlsx")
编写的,我也尝试过更改位置,但仍然无法正常工作
答案 0 :(得分:0)
这是更新的代码,并确保您的目标文件(C:\ invoicesample \ invoice.xlsx)可用。
Option Explicit
Private Sub CommandButton1_Click()
Dim outward As Variant
Dim Invoice As Variant
Dim resources As String
Dim qty As String
Dim Vat As String
Dim unitprice As String
Dim kindattn As String
Dim name As String
Dim address As String
Dim city As String
Dim r As Long
Dim state As String
Dim zip As String
Dim mobileno As Variant
Dim lastrow As Long
Dim phonenumber As String
Dim bankid As String
Dim path As String
Dim myfilename As String
Dim mydate As String
Dim servicetax As String
Dim strfilepath As String
Dim total As Long
lastrow = Sheets("invoiceinfo").Range("A" & Rows.Count).End(xlUp).Row
r = 6
For r = 6 To lastrow
If Cells(r, 21).Value = "done" Then GoTo nextrow
outward = Sheets("Invoiceinfo").Cells(r, 3).Value
Invoice = Sheets("invoiceinfo").Cells(r, 4).Value
resources = Sheets("invoiceinfo").Cells(r, 6).Value
qty = Sheets("invoiceinfo").Cells(r, 7).Value
unitprice = Sheets("invoiceinfo").Cells(r, 8).Value
Vat = Sheets("invoiceinfo").Cells(r, 9).Value
servicetax = Sheets("invoiceinfo").Cells(r, 10).Value
kindattn = Sheets("invoiceinfo").Cells(r, 12).Value
name = Sheets("invoiceinfo").Cells(r, 13).Value
address = Sheets("invoiceinfo").Cells(r, 14).Value
city = Sheets("invoiceinfo").Cells(r, 15).Value
state = Sheets("invoiceinfo").Cells(r, 16).Value
zip = Sheets("invoiceinfo").Cells(r, 17).Value
mobileno = Sheets("invoiceinfo").Cells(r, 18).Value
phonenumber = Sheets("invoiceinfo").Cells(r, 19).Value
bankid = Sheets("invoiceinfo").Cells(r, 20).Value
Cells(r, 21).Value = "done"
Application.DisplayAlerts = False
strfilepath = "C:\invoicesample\invoice.xlsx"
If Dir(strfilepath) = "" Then
MsgBox " file not avilable on path ", vbCritical
Else
Workbooks.Open (strfilepath)
ActiveWorkbook.Sheets("invoice").Activate
ActiveWorkbook.Sheets("invoice").Range("D9").Value = bankid
ActiveWorkbook.Sheets("invoice").Range("D11").Value = kindattn
ActiveWorkbook.Sheets("invoice").Range("D12").Value = name
ActiveWorkbook.Sheets("invoice").Range("D13:H14").Value = address
ActiveWorkbook.Sheets("invoice").Range("D15").Value = city
ActiveWorkbook.Sheets("invoice").Range("F15").Value = state
ActiveWorkbook.Sheets("invoice").Range("H15").Value = zip
ActiveWorkbook.Sheets("invoice").Range("H15").Value = mobileno
ActiveWorkbook.Sheets("Invoice").Range("D17").Value = phonenumber
ActiveWorkbook.Sheets("invoice").Range("M14").Value = outward
ActiveWorkbook.Sheets("invoice").Range("M15").Value = Invoice
ActiveWorkbook.Sheets("invoice").Range("E20:H20").Value = resources
ActiveWorkbook.Sheets("invoice").Range("J20").Value = qty
ActiveWorkbook.Sheets("invoice").Range("L20").Value = unitprice
ActiveWorkbook.Sheets("invoice").Range("M20").Value = total
ActiveWorkbook.Sheets("invoice").Range("M38").Value = Vat
ActiveWorkbook.Sheets("invoice").Range("M39").Value = servicetax
path = "D:\contact details\"
mydate = Date
Date = Format(mydate, "DD_MM_YYYY")
ActiveWorkbook.SaveAs Filename:=path & Invoice & "-" & Date & "-" & name & ".Xlsx"
myfilename = ActiveWorkbook.FullName
Application.DisplayAlerts = True
SetAttr myfilename, vbReadOnly
'ActiveWorkbook.PrintOut Copies:=1
ActiveWorkbook.Close savechanges:=False
End If
nextrow:
Next r
End Sub