How to Autofit Contents all tables in Word Document with vbscript - automation

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!

Related

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

executing a find and replace in a word doc from Excel VBA

I am trying to open specific Word templates based on data from Excel (that part is working). Then, once the template is open, I am trying to do find based on tags in the Excel doc and replacing with corresponding data in the same column. When I run the macro, it opens the template and just spins and spins without giving me the output. Here is the code:
` Sub New_Purification_SOP()
'
' New_Purification_SOP Macro
''Open an existing Word Document from Excel
Dim objWord As Object
Dim myValue As Variant
Dim PurCol As Variant
'open input box requesting line of the material to be made
myValue = InputBox("Select Row to create SOP")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
'Change the directory path and file name to the location
'of the document you want to open from Excel
If ActiveSheet.Cells(myValue, 10) = "Supe" And _
ActiveSheet.Cells(myValue, 12) = "IgG1" Then
objWord.Documents.Open "S:\generic filename"
With objWord
For PurCol = 3 To 13 'move through columns left to right
TagName = .Cells(10, PurCol).Value 'get tag name from row
TagValue = .Cells(myValue, PurCol).Value 'get tag name from row
With objWord.Content.Find
.Text = TagName
.Replacement.Text = TagValue
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll 'Forward = True, Wrap = _
wdFindContinue
End With
Next PurCol
End With`
...
I am very new to VBA so please critique as much as you are willing.
You may want to include the error handling in your opening / creation of the word object. I've found that if I already have an instance of word up it can actually crash the application. That may be contributing to your issue with your program.
source: https://stackoverflow.com/questions/17177412/close-release-word-object-in-vba
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
'We've tried to get Word but if it's nothing then it isn't open
If objWord Is Nothing Then
Set objWord = CreateObject("Word.Application")
End If
'It's good practice to reset error warnings
On Error GoTo 0
After that, troubleshooting find/replace can be a painful task. See this article for why: https://gregmaxey.com/word_tip_pages/words_fickle_vba_find_property.html. What you may want to do is use the macro recorder to check the find / replace code, then just lift that into your loop. It can save a lot of time debugging.
The problem comes from using Find.Wrap = wdFindContinue in your code.
This property instructs Word to continue doing the Find - sort of like the user continuously pressing "Find Next" in the dialog box. It should rarely (more like never) be used when coding as it can result in a non-ending loop, as in this instance.
In this case, since wdReplaceAll is being used - meaning the code finishes in one step - Wrap doesn't necessarily need to be specified. But generally good practice is to use Find.Wrap = wdFindStop.

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"

Print Data from Excel in Word with VBA Makro

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