I have 2 documents, an Excel document and a Microsoft Project document. I want to add an "update" button in the Excel document that will make certain cells equal to certain cells in the Project file.
To some degree, I'm trying to do the opposite of this question: How can I make a macro in Excel workbook tab to open MS Project and copy reference cells
Here's the button macro I have so far:
(In this example, one of the tasks/columns in the Project file is "ID", and the desired value I'm trying to make Cell (4,7) equal to exists on another column on the same ID row in the Project file. Having trouble figuring out how to do this.)
Sub Update()
projApp As MSProject.Application
Set ProjApp = GetObject(, "MSProject.Application")
projApp.Visible = False
projApp.FileOpenEx "C:\files\project.mpp"
ActiveWorkbook.Worksheets("Inputs").Cells(4,7) = projApp.Find Field:= "ID", Test:="equals", Value:="5748"
End Sub
This code will open a Project file, search for a task by ID, then transfer data from that task to the Excel file. The important thing here is that the Find method returns True/False and not a reference to the found task.
Sub Update()
Dim projApp As MSProject.Application
Dim iOpened As Boolean
On Error Resume Next
Set projApp = GetObject(, "MSProject.Application")
If projApp Is Nothing Then
Set projApp = CreateObject("MSProject.Application")
iOpened = True
End If
projApp.Visible = True
projApp.FileOpenEx "C:\files\project.mpp"
If projApp.Find(Field:="ID", Test:="equals", Value:="5748") Then
Dim t As MSProject.Task
Set t = projApp.ActiveCell.Task
ActiveWorkbook.Worksheets("Inputs").Cells(4, 7) = t.Finish
End If
projApp.FileCloseEx pjDoNotSave
If iOpened Then
projApp.Quit pjDoNotSave
End If
End Sub
Notes:
This code does not depend on whether the Project application is already open or not (GetObject vs CreateObject).
Until the code works flawlessly, it's best to ensure the automated application is visible (Project, in this case).
Related
I want to insert in a W10 Excel 2016 macro some code that will
open a new Word 2016 document and
copy the value of variable "s" (string) from the Excel macro to that new Word document
then returns to the Excel macro.
Maybe it helps that I know Word can be opened with this macro:
Sub startWord()
Application.ActivateMicrosoftApp xlMicrosoftWord
End Sub
TIA for your help.
If it’s a simple copy of a string into a new Word document, then the following code should work. You haven’t indicated where you get the string from, so I’ve used the contents of cell A1 on Sheet1. You can adjust this to suit.
For the following to work, you need to enable a reference within VBA. In the VBA Editor, select Tools/References & check the box ‘Microsoft Word 16.0 Object Library’. If it’s not already checked, you’ll find it listed alphabetically.
This works for me – let me know how you go with it.
Option Explicit
Dim WordApp As Word.Application, myDoc As Word.Document, s As String
Sub CopyToWord()
On Error Resume Next
Set WordApp = GetObject(class:="Word.Application")
On Error GoTo 0
If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")
WordApp.Visible = True
WordApp.Activate
Set myDoc = WordApp.Documents.Add
s = Sheet1.Range("A1").Value
With myDoc.Content
.Text = s
End With
End Sub
I have a situation where I need to pass a variable from Word to Excel via a macro. The reasoning is a little complicated, but basically I want to use this as a way to create a customized index. I point that out in case there might be an easier way to create the index. This particular index is created based on the paragraph a word or phrase is found in and not the page number. There doesn't appear to be a way to do this in Word.
So, I want to highlight the word or phrase, press my macro combo (or add a right click menu item to do it) which assigns the highlighted text to a variable and then sends it to one of three open worksheets (there will be 3 sections to the index) and adds it to the bottom row of the spreadsheet. From there I need to alphabetize the list and combine all the same phrases into one cell.
I know how to send items from Excel to Word, but when I try to research the idea of sending from Word to Excel all I can find are examples of Excel retrieving something from Word and returning it back to Excel. This is not applicable to what I'm doing. There will be thousands of index entries (before I combine them).
If it isn't possible to go directly from Word to Excel then I suppose I could send the selected text to another another document, but again I don't know how to do this.
Please help!
I did not understand why you need to work in Excel from Word, but I prepared a piece of code to show you how you can (easily) acces Excel object and work using its objects:
Sub testWorkingInExcel()
Dim objEx As Excel.Application, wb As Excel.Workbook, W As Excel.Workbook, sh As Excel.Worksheet
Dim lastEmptyRow As Long, boolFound As Boolean
On Error Resume Next
Set objEx = GetObject(, "Excel.Application") 'find Excel open session if any
If Err.Number <> 0 Then
Err.Clear: On Error GoTo 0
Set objEx = CreateObject("Excel.Application")
Set wb = objEx.Workbooks.Open("Workbook full name")
Else
For Each W In objEx.Workbooks
If W.Name = "Needed workbook" Then
Set wb = W: boolFound = True: Exit Sub
End If
Next
If Not boolFound Then Set wb = objEx.Workbooks.Open("Workbook full name")
End If
On Error GoTo 0
Set sh = wb.Worksheets("NeededSheet")
lastEmptyRow = sh.Range("A" & sh.Rows.Count).End(xlUp).Row
'Now, you have the last empty row of the needed page and you can do
'whatever you could do directly in Excel...
End Sub
I'm attempting to write a script that opens 5 different excel files, renames a worksheet in each file, and modifies the contents of several cells in each file, all in a "for each" loop. So far I can't even rename the worksheets...
I am working with the "for each" loop function, but can't quite figure out how to reference the Dim variable in the code
'launch Excel and open file
Set xlObj = CreateObject("Excel.Application")
Dim names(i)
names(0)="VCS"
names(1)="VRS"
names(2)="VIN"
names(3)="VEU"
names(4)="VGT"
for i = 0 to 4
Set xlFile = xlObj.WorkBooks.Open("c:\warem32\EIA_Demand\NG_CONS_SUM_A_EPG0_" & names(i) & "_MMCF_M.xls")
'turn off screen alerts
xlObj.Application.DisplayAlerts = False
'change sheet to desired worksheet name
Worksheets.Name("Data 1").Name = "Data1"
Next
xlFile.Close True
next
xlObj.Quit
Just for reference the name of the first referenced should be:
c:\warem32\EIA_Demand\NG_CONS_SUM_A_EPG0_VCS_MMCF_M.xls
IS it possible to reference the x value within the line of code that opens the excel file (specifically, where I have the characters ("x") in the code)?
If I can code this correctly, each iteration through the loop will alter the file name, and allow the script to select a different file, renaming the "data 1" tab to "Data1". The code currently does not work at all. I would like to also modify several cells in row 3 of the data1 worksheet using a nested loop, but figured I'd start with this first.
Thank you!
Try:
'launch Excel and open file
Set xlObj = CreateObject("Excel.Application")
'turn off screen alerts
xlObj.Application.DisplayAlerts = False
Dim MyNames As Variant
MyNames = Array("VCS", "VRS", "VIN", "VEU", "VGT")
for i = LBound(MyNames) To UBound(MyNames)
Set xlFile = xlObj.WorkBooks.Open("c:\warem32\EIA_Demand\NG_CONS_SUM_A_EPG0_(" & MyNames(i) & ")_MMCF_M.xls")
'change sheet to desired worksheet name
Worksheets.Name("Data 1").Name = "Data1"
xlFile.Close True
Next i
(FYI, this is untested, but, I believe, should work)
I am converting code I had written in Excel VBA to vb.NET on VS 2017. When I run the code, I get the error
This method or property is not available because the Clipboard is
empty or not valid.
This message appears while the application is running, when it tries to paste the selected range from Excel to the Word document. I have kept the worksheet being copied visible while the code runs, and can see that it selects the correct range but doesn't actually copy it.
What is the correct way to copy a range of cells in vb.NET?
Here is the part of my code where the error occurs:
excelApp = New Excel.Application
excelWB = excelApp.Workbooks.Open(SurveyFormLoc)
excelApp.Visible = True
With excelApp
.Sheets("Site Details").Select
.Range("B2:I11").Copy()
End With
excelWB.Save()
wdApp = CreateObject("Word.Application")
wdApp.Visible = False
wdDoc = wdApp.Documents.Open(DesignReportLoc)
With wdDoc
.Application.Selection.Find.Text = "INSERT FROM SURVEY FORM"
.Application.Selection.Find.Execute()
.Application.Selection.ParagraphFormat.Alignment = 0
End With
With wdApp
.Selection.PasteSpecial(Link:=True, DataType:=0, Placement:=0, DisplayAsIcon:=False) 'Asked question to get this
.Selection.TypeParagraph()
End With
Could the problem be that excelWB.Save() resets copy selection? The same thing happens in user interface too.
I have a Word 2010 document embedded inside an Excel sheet. I want to create content control boxes inside the word doc which can be populated programatically. For this I need to set tags for the content control.
I read on the MSDN website and some other sources that it is simple enough - you just have to enable Design Mode and then right click the content control box and click Properties. However, the properties option is grayed out and disabled even though I'm in Design Mode.
When I do this on a standalone Word document (not an embedded one), it works just fine. So that's a workaround I'm using right now. However it's really inconvenient to have to create the boxes in the standalone Word doc and copy them over into the one embedded in Excel.
Is it possible to edit properties of content control box in a Word doc embedded inside an Excel sheet?
It can be done programmatically, something like the code snippet that follows.
An embedded Word document is an Excel OLEObject; such an object can be named, for example: ws.Objects(1).Name = "WordDoc" - this is saved in the workbook and will remain the same, even if other objects are added later. This does not name the document, only the object on the surface of the Worksheet.
If the embedded document has never been accessed during a session, it first needs to be activated. As doing so causes the screen to jump and the selection to change, IF conditions are included to test that and first activate the OLEObject, as well as re-selecting the cell that was active previously.
Working with the .Tag property is shown in the For Each...Next loop. You can see the result if you go into Design Mode.
Note that the macro will not work if you're in Design Mode or if the selection is in the embedded Word document.
Sub Test()
Dim ws As Excel.Worksheet
Dim currCel As Excel.Range
Dim oDoc As OLEObject
Set currCel = Application.Selection
Set ws = ActiveWorkbook.Worksheets("Sheet1")
'Debug.Print ws.OLEObjects.Count
Set oDoc = ws.OLEObjects("WordDoc")
' Debug.Print oDoc.OLEType 'Type 1 = Embedded
WorkWithWordDoc oDoc, currCel
End Sub
Sub WorkWithWordDoc(oDoc As OLEObject, selRange As Excel.Range)
Dim doc As Word.Document
Dim wasActivated As Boolean
Dim cc As Word.ContentControl
'On first opening the Workbook
'the OLE interface of the OLEObject
'isn't accessible, so activate it
wasActivated = True
On Error Resume Next
Set doc = oDoc.Object
If Err.Number = 1004 Then
Excel.Application.ScreenUpdating = False
oDoc.Activate
wasActivated = False
Set doc = oDoc.Object
Excel.Application.ScreenUpdating = True
End If
On Error GoTo 0
For Each cc In doc.ContentControls
cc.Tag = "CC in embedded Doc"
Next
'Clean up
If Not wasActivated Then
'Deactivate the document
selRange.Select
End If
Set doc = Nothing
End Sub