【发布时间】:2022-06-15 04:32:10
【问题描述】:
我很幸运能够在这里和那里找到代码片段,并且能够构建一个创建字母的宏。发件人的信息取自一个 Excel 电子表格,我在其中填充了律师姓名、地址、电话号码等。宏运行正常。问题是,电子表格不断被覆盖,我上周添加的记录今天不再存在。我上周甚至昨天删除的记录今天又在那里了。我想知道我打开电子表格的方式(连接)是否会导致这种行为,或者是否与电子表格的更新方式有关。要更新它,我将它从服务器复制到我的桌面,删除只读属性,编辑它,保存更改并关闭它,然后再次将其设为只读,然后将其复制回服务器,覆盖旧的一。在我编辑电子表格时,可能有人正在运行宏。以下是相关代码:
Option Explicit
Public slkCompanyInfo As String
'This public variable is used in the Letter() macro. Do not delete this statement
Public Sub Letter()
Dim mySQLquery As String
Dim myKey As String
Dim mySource As String
Dim slkAddresseeName, slkRegarding, slkFileNum, slkSalutation As String
Dim slkTemplate As String
Dim myConn As ADODB.Connection
Dim myRs As ADODB.Recordset
Dim slkTempDoc As String
Dim imgFullPath As String
imgFullPath = "\\server1\SignatureImages\"
' Prompt user for Login ID. Make sure it's all lowercase
myKey = LCase(InputBox("Enter Attorney or Paralegal User ID (e.g., jtorres or b324):"))
' Make DB connection
mySQLquery = "SELECT * FROM [all_users$] WHERE LoginID = '" & myKey & "'"
mySource = "\\server1\LetterMemoDB.xlsx"
Set myConn = New ADODB.Connection
Set myRs = New ADODB.Recordset
With myConn
.Provider = "Microsoft.ACE.OLEDB.12.0;Data Source=" & mySource & ";Extended Properties=""Excel 12.0;HDR=YES"";"
End With
myConn.Open
myRs.Open mySQLquery, myConn, adOpenStatic, adLockReadOnly
' End of DB connections
'Main block of code to build letter goes here.
' Place cursor at beginning of letter body
Selection.GoTo What:=wdGoToBookmark, Name:="Body"
' Cleanup
myConn.Close
Documents(slkTempDoc).Activate
Unload frmLtrAddress
Else: MsgBox "The Login ID you entered does not have an associated letter template. Please run the macro again and enter a valid Login ID.", vbOKOnly, "Invalid value entered or left blank"
End If
Else: MsgBox "Please run the macro again and enter a valid Login ID at the prompt", vbOKOnly, "Invalid value entered or left blank"
End If
End Sub
我知道我应该列出我已经尝试解决的问题,但我无法想象该怎么做。您可以提供的任何帮助将不胜感激。我在发帖前搜索过,但没有找到任何相关内容。
【问题讨论】:
-
我应该补充一下,我使用的是 Word 和 Excel 2016。