I have some VBA code that copies stuff from Excel and pastes it into Word. The problem I'm having is how to open the spreadsheet. I can open it using an absolute path reference by
我有一些VBA代码可以从Excel复制东西并将其粘贴到Word中。我遇到的问题是如何打开电子表格。我可以使用绝对路径引用来打开它
Workbooks.Open "C:\path\filename.xls"
I would prefer to reference the spreadsheet using a relative path reference. I was able to find code for relative path references from an Excel workbook to another one but it doesn't seem to work if you're doing it from Word.
我更愿意使用相对路径引用来引用电子表格。我能够找到从Excel工作簿到另一个工作簿的相对路径引用的代码,但是如果你从Word中执行它,它似乎不起作用。
1 个解决方案
#1
0
Add a reference to Excel object library, then create an object in code and use that object to control an instance of Excel. Just make sure to avoid things like ActiveWorkbook, just in case.
添加对Excel对象库的引用,然后在代码中创建一个对象并使用该对象来控制Excel的实例。只是为了以防万一,请确保避免使用像ActiveWorkbook这样的东西。
After adding the reference:
添加参考后:
Sub DoStuffWithExcelInWord()
Dim xl As Excel.Application
Dim wkbk As Excel.Workbook
Dim wk As Excel.Worksheet
Set xl = CreateObject("Excel.Application")
Set wkbk = xl.Workbooks.Open("C:\test.csv")
Set wk = wkbk.Sheets(1)
Debug.Print wk.Cells(1, 1).Value
xl.Quit
Set wk = Nothing
Set wkbk = Nothing
Set xl = Nothing
End Sub
You can create something very similar using Excel to automate Word too, if that's more of what you're looking for.
您可以使用Excel创建非常相似的东西来自动化Word,如果这更像是您正在寻找的东西。
#1
0
Add a reference to Excel object library, then create an object in code and use that object to control an instance of Excel. Just make sure to avoid things like ActiveWorkbook, just in case.
添加对Excel对象库的引用,然后在代码中创建一个对象并使用该对象来控制Excel的实例。只是为了以防万一,请确保避免使用像ActiveWorkbook这样的东西。
After adding the reference:
添加参考后:
Sub DoStuffWithExcelInWord()
Dim xl As Excel.Application
Dim wkbk As Excel.Workbook
Dim wk As Excel.Worksheet
Set xl = CreateObject("Excel.Application")
Set wkbk = xl.Workbooks.Open("C:\test.csv")
Set wk = wkbk.Sheets(1)
Debug.Print wk.Cells(1, 1).Value
xl.Quit
Set wk = Nothing
Set wkbk = Nothing
Set xl = Nothing
End Sub
You can create something very similar using Excel to automate Word too, if that's more of what you're looking for.
您可以使用Excel创建非常相似的东西来自动化Word,如果这更像是您正在寻找的东西。