簡體   English   中英

通過電子郵件發送時出現 xlsm 文件錯誤

[英]xlsm file error upon sending through email

我是這里的新手 vba 編碼員。

我用用戶表單創建了一個 .xlsm。 在我的電腦上一切正常,但是當我通過電子郵件發送文件時,收件人在打開文件時會遇到以下問題:

  1. 我在 Workbook_Open 上添加了一個事件處理程序來自動打開用戶表單。 當接收者打開文件時,它會收到這個錯誤並且Debug按鈕返回到這一行:

在此處輸入圖片說明 在此處輸入圖片說明

  1. 單擊用戶表單的提交按鈕時,數據應該傳輸到“ThisWorkbook”,但它會創建一個新文件(我猜是以前的版本)並將數據粘貼到那里。

誰能幫我找出我的文件出了什么問題? 謝謝你。

下面是我的代碼:

內部工作簿事件處理程序:

Sub Workbook_Open()

RunForm

End Sub

模塊一:

Option Explicit
Option Base 1

Sub PopulateComboBox()

Dim PaymentTerms() As String, PaymentFreq() As String, PaymentTermsAlt() As String
Dim i As Integer, j As Integer, m As Integer, n As Integer, o As Integer

j = WorksheetFunction.CountA(Sheets("Populate").Columns("A:A"))
n = WorksheetFunction.CountA(Sheets("Populate").Columns("B:B"))

ReDim PaymentTerms(j - 1) As String
ReDim PaymentFreq(n - 1) As String
ReDim PaymentTermsAlt(j - 1) As String

For i = 1 To j - 1
    PaymentTerms(i) = ThisWorkbook.Sheets("Populate").Range("A2:A" & (j - 1)).Cells(i, 1)
    UserForm1.ComboTerms.AddItem PaymentTerms(i)
Next i

For m = 1 To n - 1
    PaymentFreq(m) = ThisWorkbook.Sheets("Populate").Range("B2:B" & (n - 1)).Cells(m, 1)
    UserForm1.ComboFreq.AddItem PaymentFreq(m)
Next m

For o = 1 To j - 1
    PaymentTermsAlt(o) = ThisWorkbook.Sheets("Populate").Range("A2:A" & (j - 1)).Cells(o, 1)
    UserForm1.ComboTermsAlt.AddItem PaymentTermsAlt(o)
Next o

UserForm1.ComboTerms.Text = PaymentTerms(1)
UserForm1.ComboFreq.Text = PaymentFreq(1)
UserForm1.ComboTermsAlt.Text = PaymentTermsAlt(1)

End Sub

Sub RunForm()

ThisWorkbook.Sheets("Printout").Activate
UserForm1.Show

End Sub

內部用戶表單:

Option Explicit

Sub CommandButton1_Click()

Application.ScreenUpdating = False

If Not IsNumeric(BasePay) Or Not IsNumeric(Interest) Then
MsgBox ("Please Enter Numeric Value for Base Pay or Interest Rate")
Exit Sub
End If

If BasePay < 0 Or Interest < 0 Then
MsgBox ("Base Pay or Interest cannot be negative value")
Exit Sub
End If

ThisWorkbook.Sheets("Printout").Range("A1") = "Prepared For " & ClientName
ThisWorkbook.Sheets("Printout").Range("O1").Value = BasePay.Text
ThisWorkbook.Sheets("Printout").Range("S2").Value = Interest.Text / 100
ThisWorkbook.Sheets("Printout").Range("L3").Value = ComboTerms.Text
ThisWorkbook.Sheets("Printout").Range("O3").Value = ComboFreq.Text
ThisWorkbook.Sheets("Printout").Range("Q2").Value = ComboTermsAlt.Text

If NewCar Then
ThisWorkbook.Sheets("Printout").Range("U2").Value = "New"
Else
ThisWorkbook.Sheets("Printout").Range("U2").Value = "Used"
End If


'----- Transfer Add-On Items to Printout Sheet ---------
Dim i           As Integer
Dim j           As Integer
Dim k           As Integer

k = 6
For i = 1 To 9
    ThisWorkbook.Sheets("Printout").Cells(k, 1).MergeArea.ClearContents
    k = k + 2
Next

k = 6
For i = 10 To 18
    ThisWorkbook.Sheets("Printout").Cells(k, 5).MergeArea.ClearContents
   k = k + 2
Next

k = 6
For i = 19 To 27
    ThisWorkbook.Sheets("Printout").Cells(k, 9).MergeArea.ClearContents
    k = k + 2
Next

k = 6
For i = 28 To 36
    ThisWorkbook.Sheets("Printout").Cells(k, 13).MergeArea.ClearContents
    k = k + 2
Next

'---- Category 1 ------
i = 6
For j = 1 To 9
    If UserForm1.Controls("Checkbox" & j).Value = True Then
        ThisWorkbook.Sheets("Printout").Range("A" & i).Value = Me.Controls("Checkbox" & j).Caption
        i = i + 2
    Else
        ThisWorkbook.Sheets("Printout").Range("A" & i).Value = ""
    End If
Next j

'---- Category 2 ------
i = 6
For j = 10 To 18
    If UserForm1.Controls("Checkbox" & j).Value = True Then
        ThisWorkbook.Sheets("Printout").Range("E" & i).Value = Me.Controls("Checkbox" & j).Caption
        i = i + 2
    Else
        ThisWorkbook.Sheets("Printout").Range("E" & i).Value = ""
    End If
Next j

'---- Category 3 ------
i = 6
For j = 19 To 27
    If UserForm1.Controls("Checkbox" & j).Value = True Then
        ThisWorkbook.Sheets("Printout").Range("I" & i).Value = Me.Controls("Checkbox" & j).Caption
        i = i + 2
    Else
        ThisWorkbook.Sheets("Printout").Range("I" & i).Value = ""
    End If
Next j

'---- Category 4 ------
i = 6
For j = 28 To 36
    If UserForm1.Controls("Checkbox" & j).Value = True Then
        ThisWorkbook.Sheets("Printout").Range("M" & i).Value = Me.Controls("Checkbox" & j).Caption
        i = i + 2
    Else
        ThisWorkbook.Sheets("Printout").Range("M" & i).Value = ""
    End If
Next j

UserForm1.Hide

End Sub

Sub CommandButton2_Click()

Unload UserForm1
UserForm1.Show

End Sub

Sub CommandButton3_Click()
Unload UserForm1
End Sub

Sub NewCar_Click()


Dim LastRow     As Integer
Dim i           As Integer
Dim j           As Integer

LastRow = WorksheetFunction.CountA(Sheets("Populate").Columns("D:D"))

'---- Count No of Checkbox
Dim Ctrl As MSForms.Control
Dim n As Integer
Dim cbcount As Long

For n = 0 To Me.Controls.Count - 1
    If Left(UserForm1.Controls(n).Name, 8) = "CheckBox" Then
        cbcount = cbcount + 1
    End If
Next n

i = 2 '--- Preset counter i
For j = 1 To cbcount
    UserForm1.Controls("Checkbox" & j).Caption = ThisWorkbook.Sheets("Populate").Cells(i, 4).Value
    i = i + 1
    
    If i > LastRow And i < (cbcount / 4) Then
        UserForm1.Controls("Checkbox" & j).Caption = ""
    End If
    
    If i > LastRow And i > (cbcount / 4 + 1) Then
        i = 2
    End If
        
Next j

End Sub

Sub UsedCar_Click()

Dim LastRow     As Integer
Dim i           As Integer
Dim j           As Integer

LastRow = WorksheetFunction.CountA(Sheets("Populate").Columns("D:D"))

'---- Count No of Checkbox
Dim Ctrl As MSForms.Control
Dim n As Integer
Dim cbcount As Long

For n = 0 To Me.Controls.Count - 1
    If Left(Me.Controls(n).Name, 8) = "CheckBox" Then
        cbcount = cbcount + 1
    End If
Next n

i = 2 '--- Preset counter i
For j = 1 To cbcount
    UserForm1.Controls("Checkbox" & j).Caption = ThisWorkbook.Sheets("Populate").Cells(i, 8).Value
    i = i + 1
    
    If i > LastRow And i < (cbcount / 4) Then
        UserForm1.Controls("Checkbox" & j).Caption = ""
    End If
    
    If i > LastRow And i > (cbcount / 4 + 1) Then
        i = 2
    End If
        
Next j

End Sub

Sub UserForm_Initialize()

Call PopulateComboBox

'----- Rename Frame Boxes Caption
Dim k As Integer, nc As Integer
nc = 1
For k = 2 To 5
    Me.Controls("Frame" & k).Caption = ThisWorkbook.Sheets("Printout").Cells(5, nc)
    nc = nc + 4
Next k
'--------------------------------------------------


Dim LastRow     As Integer
Dim i           As Integer
Dim j           As Integer

LastRow = WorksheetFunction.CountA(ThisWorkbook.Sheets("Populate").Columns("D:D"))

'---- Count No of Checkbox
Dim Ctrl As MSForms.Control
Dim n As Integer
Dim cbcount As Long

For n = 0 To Me.Controls.Count - 1
    If Left(Me.Controls(n).Name, 8) = "CheckBox" Then
        cbcount = cbcount + 1
    End If
Next n

i = 2 '--- Preset counter i
For j = 1 To cbcount
    UserForm1.Controls("Checkbox" & j).Caption = ThisWorkbook.Sheets("Populate").Cells(i, 4).Value
    i = i + 1
    
    If i > LastRow And i < (cbcount / 4) Then
        UserForm1.Controls("Checkbox" & j).Caption = ""
    End If
    
    If i > LastRow And i > (cbcount / 4 + 1) Then
        i = 2
    End If
        
Next j
    
End Sub

操作系統的安全警告和默認設置問題。 在打開此文件之前,您的尋址者需要執行以下操作: 1. 在文件上單擊鼠標右鍵 2. 選擇屬性 3. 按解鎖(下圖) 之后,一切正常。 如果您從互聯網上下載自己的文件,則可以自己測試。

截屏

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM