2016-12-26 35 views
-3

已經宣佈在Form2的變量這是我的表格1我想用我在Form1中

Public Class Form1 
Dim excelapp As Microsoft.Office.Interop.Excel.Application 
Dim excelwb As Microsoft.Office.Interop.Excel.Workbook 
Dim excelws As Microsoft.Office.Interop.Excel.Worksheet 

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    'Calling form for New 
    Dim new1 As New Form2 
    new1.Show() 
End Sub 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 
    'To Open File 
    Using FileDialog As New OpenFileDialog 
     FileDialog.Title = "Select your Excel file" 
     FileDialog.Filter = "Microsoft Excel|*.xl*|All Files|*.*" 
     FileDialog.ShowDialog() 
     Process.Start(FileDialog.FileName) 
     Dim s As String = FileDialog.FileName 
     excelwb = excelapp.Workbooks.Open(FileDialog.FileName) 
     excelws = excelwb.Worksheets(1) 


     'End Using 

     ' Using FileProcess As Process = Process.Start(FileDialog.FileName) 

    End Using 

    'Calling form for Open 

    Dim new1 As New Form2 
    new1.Show() 

這是我的表2

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
    'New Entry in existing excel file 
    excelws.Cells(3, 1).Value = TextBox1.Text 
    excelws.Cells(3, 2).Value = TextBox2.Text 
    excelws.Save() 
End Sub 

我想用所有的變量和所有的form2中form1的東西。 請幫助我如何在我的表單2中使用這些東西。 我正在準備一個應用程序來接受用戶輸入併爲其生成pdf。 即時通訊使用Excel保存數據和VB Express 2010創建窗體。 請指導我:)非常感謝。

+0

在functions/subs之外聲明它們爲PUBLIC。 – FDavidov

+0

你能幫我解碼嗎?我有點新。如果我宣佈它們是公開的,我應該如何以form2形式提及這些公共數據? –

+0

我沒有看到第一種形式的任何變量;聲明,「提及」或以其他方式 – Plutonix

回答

1

更改您的excelws,等報關單表1是Public

Public excelapp As Microsoft.Office.Interop.Excel.Application 
Public excelwb As Microsoft.Office.Interop.Excel.Workbook 
Public excelws As Microsoft.Office.Interop.Excel.Worksheet 

然後,假設你的Form1中仍稱UserForm1,你可以改變你的表格2的代碼如下:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
    'New Entry in existing excel file 
    UserForm1.excelws.Cells(3, 1).Value = TextBox1.Text 
    UserForm1.excelws.Cells(3, 2).Value = TextBox2.Text 
    UserForm1.excelws.Save() 
End Sub 

如果不叫UserForm1,只需替換相應的名稱即可。