Print Data from Excel in Word with VBA Makro - vba

I'm trying to print Data from my Excelsheet to a table that already exists in an already existing word File.
My Data in excel is a simple question like: "What is the weather like?"
Now I want to print this question in to a table in word.
The Word File and the table is already existing.
First I'm opening the word file from my excel makro.
Dim AppWD As Object
Set AppWD = CreateObject("Word.Application") 'Word als Objekt starten
AppWD.Visible = True
AppWD.documents.Open "file location"
doc.Unprotect
This works perfectly fine, but then I'm first of all trying to remove a Row in that table as a test, i do that like this:
AppWD.Tables.Item(1).Rows(2).Delete
But here's the problem, this doesnt work.
I always get the Run-time error '424':
Object required
I think the problem is that my makro doesn't know that it has to write in to word and not into my excelsheet.

It's because you are not creating and using your objects correctly. is this what you are trying? (UNTESTED)
Dim AppWD As Object, doc As Object
Set AppWD = CreateObject("Word.Application")
AppWD.Visible = True
Set doc = AppWD.documents.Open("file location")
doc.Unprotect
doc.Tables.Item(1).Rows(2).Delete

Related

How to Autofit Contents all tables in Word Document with vbscript

I have a word document with several tables in it with variable column and rows
My Goal is to write a vbscript file which Autofits every table according to its context
Here is a image of the option that I need to enable with by script
From Something like this
to To Something like this
My Current code seems to not work:
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
dim fpath
fpath="D:\tables.docx"
Set objDoc = objWord.Documents.Open(fpath)
For Each tbl In objDoc.Tables
tbl.AutoFitBehavior(wdAutoFitContent)
Next
objDoc.Save
objWord.Quit
The above code has no effect on word file, it just opens the file and closes it.
Please Help!

Pasting multiple images in a word doc

I need assistance in building a macro that would take the image that is stored in the clipboard and paste it in the word doc. There will be multiple images so sequential images must be pasted at the end of the document.
Here is my current code so far
' Create word document and paste to word
Set wordobj = CreateObject("Word.Application")
Set objdoc = wordobj.Documents.Add
wordobj.Visible = True
Set objSelection = wordobj.Selection
objSelection.Paste
' Paste in the active end of the word document
'??? tried multiple lines but it gives "object" error
objdoc.ActiveDocument.Content
objdoc.Collapse Direction:=wdCollapseEnd
objSelection.Paste.Paste
Any assistance will be helpful
Since your code creates a new document each time it's run, the only way there'd be anything to paste over is if that new document already has some content. But since you don't even specify a template to load from, that can only mean Word's 'Normal' template, which should never have any content. Regardless, it's as simple as:
Sub Demo()
Dim wordobj As Object, objdoc As Object
Set wordobj = CreateObject("Word.Application")
Set objdoc = wordobj.Documents.Add
With objdoc.Range
.InsertAfter vbCr
.Characters.Last.Paste
End With
End Sub

Referencing already opened word document in VBA

I am using Excel VBA to open a word Template and it populates the template with information from an excel spreadsheet. From there, I have a button on the spreadsheet that I want to then populate another line in the word document whenever it is clicked. The problem I am encountering is that when I run the button macro it just opens up another word document and pastes the button information instead of doing it on the already opened document. I will attach my code below and I believe it is an easy fix I just can't seem to find a way around it.
Sub RepairCal()
Dim objWord As Object
Dim objDoc As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open("C:\Users\z003narc\Documents\Gage Lab Form Template.docm")
objWord.Activate
With Selection
objWord.ActiveDocument.Selection.MoveDown count:=6
objWord.ActiveDocument.Selection.MoveRight count:=5
objWord.ActiveDocument.Selection.TypeText Text:="Repair and Calibration"
End With
End Sub
Try this:
With objWord
.Selection.MoveDown count:=6
.Selection.MoveRight count:=5
.Selection.TypeText Text:="Repair and Calibration"
End With
Are you saying you already have the document open when you run this every time? The way you wrote the code, it will do this:
1) Open new blank Microsoft Word process
2) In the new process, opens the workbook you have chosen
3) Makes the new process the active window
4) does stuff
If you want it to attach to the file you already have open, you would need to do GetObject instead of CreateObject. So change:
Set objWord = CreateObject("Word.Application")
To
Set objWord = GetObject(, "Word.Application")
This will attach to the currently open process, then open the workbook in that process. However, it will still open up a fresh copy of that document each time, because that's what you're telling it to do.

VBA - Opening word document to export data from excel to word doc

I have a little bit of an issue that i can't solve.
I have programmed VBA code to export cell data from excel into word textboxes.
How it works:
I have a button in the excel doc you highlight the row you want to work with you press the button it than grabs the data from all the cells i have told it to and imports them into word.
The problem i am having that is if I select another row and press the button again it tries to open that same word doc again and errors as it is already open. Is there anyway i can do and if statement to check to see if its open if its not open it and if it is use the active word doc?
here is my current code
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\Users\swhiston\Desktop\IT Services Invoice.docx"
Thanks
GreatSc0tt
You want the Word object to by declared at Module/Class level and test whether or not it's already open before you open it
Option Explicit
Dim objWord As Object
Dim objDoc as Object
Sub OpenWord()
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
set objDoc = objWord.Documents.Open( "C:\Users\swhiston\Desktop\IT Services Invoice.docx")
End Sub
Sub MyCodeHere()
'Open Word If It's Not Already Open
If objwork Is Nothing Then OpenWord
'Your Code Here
End Sub
whenever you try to execute your code it will reopen the same file so either apply a condition that checks if file is open or not and if not then only it opens the file or every time you should close the file at the end of your code.
you can refer: Detect whether Excel workbook is already open
keep this in else part:
objWord.Documents.Open "C:\Users\swhiston\Desktop\IT Services Invoice.docx"

MS Access VBA convert query output to Excel format but not saving anywhere

I've been trying to use transfer spreadsheet methods but they appear to require an output path.
I just need to find out how to take a given query and simply "open up" an Excel file that contains the query output. I don't need the file actually saved anywhere.
You can open up your file without saving it by creating an Excel instance (or grabbing an existing one) and using the CopyFromRecordset function of the Excel.Range object.
This assumes your data are in an ADO recordset. You need to have references to Microsoft Excel XX.0 Object Library and Microsoft ActiveX Data Objects X.X Library` (if you are using ADO. If you use DAO then use whatever DAO reference you need)
I use this to grab an an Excel app or create a new one is Excel is not open already. I use WasANewInstanceReturned to figure how I need to clean up the Excel resources at the end. (Obviously I don't want to quit Excel if it is being use by something else).
Function GetExcelApplication(Optional ByRef WasANewInstanceReturned As Boolean) As Excel.Application
If ExcelInstanceCount > 0 Then
Set GetExcelApplication = GetObject(, "Excel.Application")
WasANewInstanceReturned = False
Else
Set GetExcelApplication = New Excel.Application
WasANewInstanceReturned = True
End If
End Function
Then grab that instance
Dim ApXL As Excel.Application, WasANewInstanceReturned as Boolean
Set ApXL = GetExcelApplication(WasANewInstanceReturned)
Add a workbook
Dim wbExp As Excel.Workbook
Set wbExp = ApXL.Workbooks.Add
Grab the first sheet
Dim wsSheet1 As Excel.Worksheet
Set wsSheet1 = wbExp.Sheets(1)
Put your recordset's field names in the first row
Dim fld As ADODB.Field
Dim col As Integer
col = 1
With wsSheet1
For Each fld In rst.Fields
.Cells(1, col).Value = fld.Name 'puts the field names in the first row
End With
col = col + 1
Next fld
End With
Then move the data just below the field names
wsSheet1 .Range("A2").CopyFromRecordset rst
Voila! You have an excel file open, with your data that has not been saved anywhere!
I usually set ApXL.ScreenUpdating = False before doing any of this and ApXL.ScreenUpdating = True at the end.
I'll let you stitch this together for your needs.
The file must be saved somewhere for Excel to open it.
If the dataset is small enough, you can use copy/paste (no file here). Otherwise, just use the %TEMP% folder for the file location.
Edit:
One simple way to get the TEMP folder is to use =Environ("TEMP")
I open and export a query from access to excel. First I created a worksheet in excel and saved it. Then I created a module in the vba part of Access (2013):
Option Compare Database
' Testtoexporttoexcel'
Function ExportQuerytoExcel()
On Error GoTo ExportQuerytoExcel_Err
' Exports the query to excel to a sheet named Nameofyoursheet
DoCmd.TransferSpreadsheet acExport, 10, "nameofyourquery", "yourPath:\nameofyourworkbook", False, "Nameofyour worksheet"
ExportQuerytoExcel_Exit:
Exit Function
ExportQuerytoExcel_Err:
MsgBox Error$
Resume ExportQuerytoExcel_Exit
End Function
-----then add another function that says:
Option Compare Database
Function OpenExcelFromAccess()
'Opens Excel to the chart
Dim MYXL As Object
Set MYXL = CreateObject("Excel.Application")
With MYXL
.Application.Visible = True
.workbooks.Open "Yourpath:\nameofyourworkbook"
End With
'Application.Quit
End Function
hope this helps, this is my first time answering a question.
Aloha