I want to create a "driver" workbook where someone can update values that will be applied to another workbook. The contents of cell B8 of "macros.xlsm" contains the text string I want to use for the author of "report1.xlsx". I have written the following macro but keep getting a
"Object doesn't support this property or method" error on the last line.
Sub add_properties()
Dim xL As Excel.Application
Set xL = New Excel.Application
Dim mainWB As Excel.Workbook
Dim reportWB As Excel.Workbook
Set mainWB = xL.Workbooks.Open("C:\Users\ga1085\adHoc\macros.xlsm")
Set reportWB = xL.Workbooks.Open("C:\Users\ga1085\adHoc\report1.xlsx")
MsgBox mainWB.Sheets("adHoc").Range("B8").Value
mainWB.Sheets("adHoc").Range("b8").Copy
reportWB.BuiltinDocumentProperties("author").PasteSpecial (xlPasteValues)
End Sub
I am also using "macros.xlsm" to update margins, headers, etc for "report1.xlsx" - will this work on those too?
Try opening your workbooks this way. You'll need to add the Microsoft Excel Object Library to your references.
Dim xL As excel.Application
Set xL = New excel.Application
Dim mainWB As excel.Workbook
Dim reportWB as excel.Workbook
Set mainWB = xL.Workbooks.Open("macros.xlsm")
Set reportWB = xL.Workbooks.Open("report1.xlsx")
I figured out how to get it to work. Macros.xlsm is already open and contains the macro that I want to execute. I didn't have to use PasteSpecial. I think the main difference is that I had to declare a variable "author" then use the variable "author" to update the report workbook. I am new to VBA - and would appreciate any other input or explanations. Thank you for your help.
Sub add_properties()
Dim author
Dim reportWB As Excel.Workbook
Set reportWB = Workbooks.Open("C:\Users\ga1085\adHoc\report1.xlsx")
author = Workbooks("macros.xlsm").Sheets("adHoc").Range("b8").Value
reportWB.BuiltinDocumentProperties("author").Value = author
End Sub
Related
I'm sorry but after three days I Give Up... So I'm asking what should be a very simple question but every example I find opens an Excel file or opens a read Only version of the same file, to do something where as in my case the file has already been opened from another macro using the code below, I just can't seem to figure how to adapt it for a file already open??
I'm also trying to figure out how to move excel to the active window from within the code? I would really appreciate any help
Dim oApp As Object
Dim x As Variant
Dim sPath As String
Dim oExcel As Excel.Application
Dim oWB As Workbook
Dim oSheet As String
sPath = "E:\Special Folders\WWWRoot\temp.xlsx"
oSheet = "--Keywording--"
On Error Resume Next
Set oExcel = New Excel.Application
Set oWB = oExcel.Workbooks.Open(sPath)
oExcel.Visible = True
Sheets(oSheet).Select
Range("A1:G1000").Clear
Range("A1").Select
Sheets(oSheet).Cells(1, 1).Select
Sheets(oSheet).PasteSpecial (xlPasteAll)
Range("A1").Select
Sorry, but can't you just turn on the Macro Recorder and get what you want?
Sub TryThis()
Windows("SecondaryWorkbook.xlsb").Activate
Range("A1").Select
Windows("PrimaryWorkbook.xls").Activate
Range("A1").Select
End Sub
I believe you need to be in the same instance of Excel, or this wonr't work. AFAIK, different instances of Excel don't communicate with each other...at all.
I have two Excel Workbooks:
Source.xlsx
Tool.xlsm
Source.xlsx contains a Worksheet with the VBA Object Name shtTests:
Let's assume that in Tool.xlsm I have a variable that contains a reference to the Workbook stored in Source.xlsx:
Dim wkbSource as Workbook
Set wkbSource = GetSourceWorkbook() ' Some function that gives a reference to the workbook
Core Question: How can I reference shtTests within Tool.xlsm by using shtTests' VBA Name?
Or to formulate the question as code... assume you have this code snippet:
Dim wkbSourceShtTests as Worksheet
Set wkbSourceShtTests = GetShtTestsFromWkbSources(wkbSources)
Question: What does GetShtTestsFromWkbSources have to look like?
Note: I do not want to reference it by its Excel Name like you would do using wkbSources.Worksheets("Test Cloning") because people might change its Excel Name some day.
Is this what you are trying?
Sub Sample()
Dim wbThis As Workbook, wbThat As Workbook
Dim wsThat As Worksheet
Dim wsCodeName As String
Set wbThis = ThisWorkbook
Set wbThat = Workbooks("Book4") '<~~ Change this to relevant workbook
wsCodeName = "ShtSheets"
Set wsThat = wbThat.Worksheets(CStr(wbThat.VBProject.VBComponents(wsCodeName).Properties(7)))
Debug.Print wsThat.Name
End Sub
Note: For this to work, you need to enable access to Visual Basic Projects
On the File menu Excel, Click Options|Trust Center|Trust Center Settings|Macro Settings, Check the box "Trust Access to the VBA Project Object Model"
If you wanted a function instead of setting up the trusted access then this would probably work:
Sub example()
Dim wkbSource As Workbook
Set wkbSource = GetSourceWorkbook()
Dim wkbSourceShtTests As Worksheet
Set wkbSourceShtTests = GetShtTestsFromWkbSources(wkbSource, "shtTests")
End Sub
Function GetShtTestsFromWkbSources(wkbk As Workbook, codename As String) As Worksheet
For Each sht In wkbk.Sheets
If sht.codename = codename Then Set GetShtTestsFromWkbSources = sht
Next
End Function
I've been having trouble taking cell values from excel and using them in a word macro (I'm trying to insert string values from sheet cells at various bookmarks in a word doc). Right now I'm just trying to be able to access cell values, but I'm coming up with an error 429 (ActiveX component can't create object). Any help/advice on how to approach pulling values from excel and using them in word would be appreciated.
Dim objExcel As New Excel.Application
Dim exWbs As Excel.Workbooks
Dim exWb As Excel.Workbook
Dim strWbName As String
Dim cisInfo As CIS
Sub PopulateDoc()
Set exWb = New Excel.Workbook
exWb = exWbs.Open("CIS.xlsx")
exWb.Sheets("Property Information").Cells(2, 8).Value = "test"
End Sub
You cannot create a New instance of a WorkBook instead:
Set exWb = objExcel.Workbooks.Open("CIS.xlsx")
(You also need to have added a reference to the Microsoft Excel XX Object Library)
I'm just trying to work on an open Excel spreadsheet from Word using VBA. I did a search on this and found the following code on How to get reference to an open Excel spreadsheet from Word?. The problem is, it seems to open a separate instance of Excel rather than the one I already have open, and then closes it after the action. How can I get the procedure to switch to the open Excel spreadsheet, perform the desired actions, and leave the spreadsheet open?
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 'Here's where I would like to insert my code
xl.Quit
Set wk = Nothing
Set wkbk = Nothing
Set xl = Nothing
End Sub
Thanks for any assistance!
Thanks again to dcromley for his earlier response. In the meantime, here's what I came up after a couple more searches on the Internet. It takes care of the situation where the Excel document is already open. Hopefully that will help others with similar situations, although it's not exhaustive (for example, if several Excel documents are open).
Sub DoStuffWithExcelInWordRevised()
Dim xl As Excel.Application
Dim wkbk As Excel.Workbook
Dim wk As Excel.Worksheet
On Error Resume Next
Set xl = GetObject(, "Excel.Application")
On Error GoTo 0
If xl Is Nothing Then
Set xl = CreateObject("Excel.Application")
Set wkbk = xl.Workbooks.Open("C:\test.xlsx")
Set wk = wkbk.Sheets(1)
End If
xl.Visible = True
With xl
' Insert Excel code here
End With
Set wk = Nothing
Set wkbk = Nothing
Set xl = Nothing
End Sub
Try:
Add xl.Visible = True
Del xl.Quit
It appears that you are trying to do this with a Workbook that is already open. Since you want to use the open workbook / application then you need to:
Set xl = GetObject(,"Excel.Application")
I want to sum a range from a sheet from one excel workbook and write it to another workbook. I came up with a following code but it is not working.
1) it opens a file open windows which asks me to select workbook to update
2) after selecting a workbook it writes "0" as a value.
Code:
Dim xl1 As New Excel.Application
Dim xl2 As New Excel.Application
Dim wb1 As Excel.Workbook
Dim wb2 As Excel.Workbook
Dim st1 As Excel.Worksheet
Dim st2 As Excel.Worksheet
wb1 = xl1.Workbooks.Open("F:\excelsumtest\file1.xlsx")
wb2 = xl2.Workbooks.Open("F:\excelsumtest\file2.xlsx")
st1 = wb1.Worksheets(1)
st2 = wb2.Worksheets(1)
st2.Cells(1, 1).formula = "=Sum(st1!A1:S1)"
Cheers
Try changing this line:
wb2 = xl2.Workbooks.Open("F:\excelsumtest\file2.xlsx")
'to this:
wb2 = xl1.Workbooks.Open("F:\excelsumtest\file2.xlsx")
I'm tying both workbooks to 1 instance of an Excel application. This isn't tested, but I think that this way, Excel will understand that it's the worksheet from the xl1 workbook that you're trying to insert.
If not, you might have to fully define the workbook you're referencing in your formula.
Hope this helps.
This worked for me:
Dim xlsApp As New Microsoft.Office.Interop.Excel.Application
xlsApp.DisplayAlerts = False