Pasting a selected range from Excel to a Word document - excel

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.

Related

PowerPoint VBA - unable to set hyperlink on shape in PPT from Excel code - but the same code works fine from PPT directly

I'm trying to set a hyperlink to a box in PowerPoint from Excel.
If I run this code from PowerPoint, this works fine:
Sub LinkTestInPPT()
Dim act As Object 'act = Active Presentation
Dim pptApp As Object
Set pptApp = GetObject(, "Powerpoint.Application")
Set act = pptApp.ActivePresentation
With act.Slides(1).Shapes("LinkedBox").ActionSettings(ppMouseClick)
.Action = ppActionHyperlink
.Hyperlink.Address = "http://www.microsoft.com"
End With
End Sub
If I try to run the same code from Excel - I can get the contents of the same box, so I know the object reference is correct, but I get a runtime error that says "Automation error" when trying to apply action settings from Excel to PowerPoint. Here's the code that DOESN'T work (after the message box) when run from Excel.
Sub LinkTestFromExcelToPPT()
Dim act As Object 'act = Active Presentation
Dim pptApp As Object
Set pptApp = GetObject(, "Powerpoint.Application")
Set act = pptApp.ActivePresentation
MsgBox act.Slides(1).Shapes("LinkedBox").TextFrame.TextRange.Text
With act.Slides(1).Shapes("LinkedBox").ActionSettings(ppMouseClick)
.Action = ppActionHyperlink
.Hyperlink.Address = "http://www.cnn.com"
End With
End Sub
I have an empty slide with one box on it named "LinkedBox" with the text "Link" in it. The MsgBox appropriately shows "Link" but it fails on the ActionSettings line.
What's wrong?

How automatically make Excel cells equal to Project cells?

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).

Copy value of variable in Excel 2016 macro to new doc in Word 2016

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

VBA Macro script to save a word document created via Excel VBA

Does anyone know what VBA script i should write to save a word document which was created from a VBA excel script?
Here is my script so far but i would like to add a step to save the produced word document into a certain directory with the naming convention referencing a single cell in my excel sheet
Sub Export_to_word()
'Export Return
Dim wdApp As Object
Dim wd As Object
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
Set wd = wdApp.Documents.Add
wdApp.Visible = True
Sheets("PM Performance Return").Select
Range("M1:P200").Select
Selection.Copy
wdApp.Selection.PasteExcelTable False, False, False
wd.SaveAs Filename:="Investment Return Period Data", FileFormat:=wdformatdocm
WordTable.AutoFitBehavior (wdAutoFitWindow)
End Sub
I have added a picture for your reference:
try below
wd.SaveAs Filename:=Worksheets("Data").Range("B2").Value & "Investment Return Period Data", FileFormat:=wdformatdocm
In the above statement folder path is at cell B2 of sheet "Data". You may change it as per your scenario. Also make sure the folder path ends with "\" e.g. C:\mukul.varshney\Delete\

Excel VBA Run-time error '424': Object Required when trying to copy TextBox

I'm attempting to copy the contents of a text box from one workbook to another. I have no problem copying cell values from the first workbook to the 2nd, but I get an object required error when I attempt to copy the text box. This macro is being run from the workbook containing the data I want copied. Using Excel 2007 Code:
Sub UploadData()
Dim xlo As New Excel.Application
Dim xlw As New Excel.Workbook
Set xlw = xlo.Workbooks.Open("c:\myworkbook.xlsx")
xlo.Worksheets(1).Cells(2, 1) = Range("d4").Value 'Copy cell content (this works fine)
xlo.Worksheets(1).Cells(2, 2) = TextBox1.Text 'This gives me the object required error
xlw.Save
xlw.Close
Set xlo = Nothing
Set xlw = Nothing
End Sub
Thanks for any help.
The problem with your macro is that once you have opened your destination Workbook (xlw in your code sample), it is set as the ActiveWorkbook object and you get an error because TextBox1 doesn't exist in that specific Workbook. To resolve this issue, you could define a reference object to your actual Workbook before opening the other one.
Sub UploadData()
Dim xlo As New Excel.Application
Dim xlw As New Excel.Workbook
Dim myWb as Excel.Workbook
Set myWb = ActiveWorkbook
Set xlw = xlo.Workbooks.Open("c:\myworkbook.xlsx")
xlo.Worksheets(1).Cells(2, 1) = myWb.ActiveSheet.Range("d4").Value
xlo.Worksheets(1).Cells(2, 2) = myWb.ActiveSheet.TextBox1.Text
xlw.Save
xlw.Close
Set xlo = Nothing
Set xlw = Nothing
End Sub
If you prefer, you could also use myWb.Activate to put back your main Workbook as active. It will also work if you do it with a Worksheet object. Using one or another mostly depends on what you want to do (if there are multiple sheets, etc.).
I think the reason that this is happening could be because TextBox1 is scoping to the VBA module and its associated sheet, while Range is scoping to the "Active Sheet".
EDIT
It looks like you may be able to use the GetObject function to pull the textbox from the workbook.
The issue is with this line
xlo.Worksheets(1).Cells(2, 2) = TextBox1.Text
You have the textbox defined at some other location which you are not using here. Excel is unable to find the textbox object in the current sheet while this textbox was defined in xlw.
Hence replace this with
xlo.Worksheets(1).Cells(2, 2) = worksheets("xlw").TextBox1.Text

Resources