[英]How to copy data from one already opened excel file (Workbook1,Sheet1,cell A11) to another already opened excel file(workbook2,sheet1,A11)
[英]How to get data from an already opened excel workbook with a Word macro?
我目前正在使用vba宏,您能幫我嗎? 我將非常感謝能在此方面為我提供幫助的任何人。
我想從Word Document宏訪問Excel打開的工作簿的數據。 由於某些原因,我需要從用戶會話中已經打開的Excel工作簿中獲取數據,而不要使用其路徑在后台重新打開它。
這是我要轉換的宏的當前位,以便使用已經打開的“ GUI.xls”。 該窗口的名稱為“ Microsoft Excel – GUI.xls [兼容模式]”,我希望我對問題的解釋足夠好,我在其他地方找不到該主題,如果已經存在,對不起。
如果可能的話,我希望AppExcel對象直接鏈接到打開的工作簿。 問題是我嘗試從Word訪問工作簿。
非常感謝您的幫助,
西蒙
Set AppExcel = CreateObject("Excel.Application")
AppExcel.Workbooks.Open SourcePath1 & "" & "\GUI.xls"
Data_Name = AppExcel.Worksheets("Output").Cells(RowNum, 2).value
Data_Ending = AppExcel.Worksheets("Output").Cells(RowNum, 3).value
Dim WordFileName As String
WordFileName = Data_Name & Data_Ending
Call candp(SourcePath, WordFileName, TargetName)
AppExcel.Activeworkbook.Close savechanges:=False
AppExcel.Quit
Set AppExcel = Nothing
嘗試使用GetObject
:
Dim WordFileName As String
Dim exWb As Object
Dim AppExcel As Object
'try to get file if it's already open
On Error Resume Next
Set exWb = GetObject(SourcePath1 & "\GUI.xls")
On Error GoTo 0
'if it's not already opened - open it
If exWb Is Nothing Then
Set AppExcel = CreateObject("Excel.Application")
Set exWb = AppExcel.Workbooks.Open(SourcePath1 & "\GUI.xls")
End If
With exWb.Worksheets("Output")
Data_Name = .Cells(RowNum, 2).Value
Data_Ending = .Cells(RowNum, 3).Value
End With
WordFileName = Data_Name & Data_Ending
Call candp(SourcePath, WordFileName, TargetName)
exWb.Close savechanges:=False
Set exWb = Nothing
If Not AppExcel Is Nothing Then AppExcel.Quit
Set AppExcel = Nothing
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.