为什么我无法在特定位置保存文件?

时间:2015-12-16 04:23:44

标签: vba ms-word word-vba save-as

我使用下面的代码从.txt文件中插入一个数字,"另存为" word文件使用该号码作为名称,然后完成邮件合并。

代码工作正常,直到我不得不切换计算机,因此文件位置发生了变化......我显然改变了位置以匹配新计算机的位置。

现在,将数字插入到正确的位置,然后打开保存对话框,但它不会将其放在正确的位置,也不会插入指定的数字。

无论我将文件路径更改为什么,它都会尝试将其保存在此处: C:\ Users \ Schlechter Ag Liquid \ OneDrive \ BOLTemplate \

Sub CommandButton1_Click()


Invoice = System.PrivateProfileString("C:\Users\Schlechter Ag Liquid\OneDrive\BOLTemplate\" & _
    "invoice-number.txt", "InvoiceNumber", "Invoice")

If Invoice = "" Then
    Invoice = 1
Else
    Invoice = Invoice + 1
End If

System.PrivateProfileString("C:\Users\Schlechter Ag Liquid\OneDrive\BOLTemplate\" & _
    "invoice-number.txt", "InvoiceNumber", "Invoice") = Invoice

' Insert the number in the document
ActiveDocument.Bookmarks("Invoicenan").Range.InsertBefore Format(Invoice, "")

ActiveDocument.SaveAs FileName:= _
 "C:\Users\Schlechter Ag Liquid\OneDrive\BOLs\" & Format(Invoice, "") & ".docx"

ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
    ActiveDocument.MailMerge.OpenDataSource Name:= _
        "C:\Users\Schlechter Ag Liquid\OneDrive\BOLTemplate\Customer Database.accdb" _
        , ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
        AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
        WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
        Format:=wdOpenFormatAuto, Connection:= _
        "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=C:\Users\Schlechter Ag Liquid\OneDrive\BOLTemplate\Customer Database.accdb;Mode=Read;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=6;Jet OLE" _
        , SQLStatement:="SELECT * FROM `report1 (1)`", SQLStatement1:="", SubType _
        :=wdMergeSubTypeAccess
    ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
     WordBasic.MailMergeFindEntry
End Sub

它说错误在这一行,但我似乎无法弄清楚它有什么问题。

ActiveDocument.SaveAs FileName:= _
 "C:\Users\Schlechter Ag Liquid\OneDrive\BOLs\" & Format(Invoice, "") & ".docx"

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

为什么要格式化发票?如果你想制作一个字符串,为什么不设置CStr(Invoice)而不是Format?如果你把一个Integer添加到一个字符串,它通常会自动将自己转换为一个字符串......我建议你摆脱格式,然后将Invoice留在那里。所以:

ActiveDocument.SaveAs FileName:= _
    "C:\Users\Schlechter Ag Liquid\OneDrive\BOLs\" & Invoice & ".docx"

至少你可以尝试一下......如果它不起作用,那么我很抱歉:)

编辑: 我可能有可能错过了你的部分代码。应该调用文件本身" invoice-1.docx"?如果是这样,你需要改变一些东西:

Invoice = "Invoice" & 1

然后你可以使用我写的其他代码;)