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
Related
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!
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.
I am trying to use excel data to perform various tasks on a word document. However, this will be used by other people, so I wanted to copy all contents from the word doc (and formatting and tables) to a new word doc and edit the new one to prevent others from accidentally saving over the first one (template one). I also want to close the template (first) doc and keep the second (new) one open.
I am new to VBA and am having trouble understanding how to handle this. After a bunch of googling, I've compiled this (not working) code:
Dim WordApp As Object
Dim WordDoc As Word.Document
Dim NewWordDoc As Word.Document
'Dim other stuff'
'-------------------------------------------------------------------
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open("C:\Users\Windows\Documents\CRC Labels
test.doc")
Set NewWordDoc = WordApp.Documents.Add 'I think this gives an error'
'------------------------------------------------------------------------
'Line that actives WordDoc'
Selection.WholeStory 'Select whole document
Selection.Expand wdParagraph 'Expands your selection to current paragraph
Selection.Copy 'Copy your selection
'Line that closes WordDoc'
'Line that activates NewWordDoc'
Selection.PasteAndFormat wdPasteDefault 'Pastes in the content
'------------------------------------------------------------------------
'Do stuff with NewWordDoc'
'------------------------------------------------------------------------
Set WordDoc = Nothing
Set WordApp = Nothing
Any other ideas of handling this situation? Thank you for your response.
When you find yourself using an existing document as the basis for creating new documents it's time to consider saving that document as a Word Template. This is a particular type of file (*.dotx or *.dotm) that may contain
boiler-plate text
Building blocks with additional boiler-plate to be inserted as required
styles (formatting command sets)
customized keyboard shortcuts
Ribbon customizations
VBA code (macros, in *.dotm, only)
that will be inherited and/or shared by all documents generated from the template.
In order to create a new document from a template, use the Documents.Add method, specifying the path and file name:
Dim wdDoc as Word.Document
Set wdDoc = Documents.Add(path & filename)
'From outside Word: Set wdDoc = wdApplication.Documents.Add
This will create a new document from the template, copying the complete template content without affecting the template, itself.
Note: You can also use a "plain document" (*.docx) with the Documents.Add method and it will copy the content. It will not, however, link back to that document so it can't share macros, for instance.
I am using send-keys to select all data and copy it from another application. My goal is to paste this data into word and save as a PDF. The problem i seem to have, is that using Microsoft interop, requires you to programattically format the data. If i copy the data from the other application, and paste it manually in a real word doc, the format retains itself.
Is there a way to easily take my clipboard data and use it with this code?
Try
Dim oWord As Word.Application
Dim oDoc As Word.Document
'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add
oPara1 = oDoc.Content.Paragraphs.Add
oPara1.Range.Text = Clipboard.SetText
'TIll Above your entire odoc is formatted
'From below I will save it to my own code
Dim newdoc As Word.Document
newdoc = oDoc
newdoc.SaveAs2("K:\file.pdf", Word.WdSaveFormat.wdFormatPDF)
'All done. Close this form.
'BSPGlobals.DataBase.Contact.ExitApp()
MessageBox.Show("Print to Doc Done.")
Catch ex As Exception
MessageBox.Show("Error at Printing the bill." & vbCrLf & ex.Message)
End Try
Depending on the source and format of the Clipboard data, you can influence the way the Clipboard contents is pasted into Word by fiddling around with the following Application options (don't forget to restore the original settings when you are done):
' when pasting between different Office documents
oWord.Options.PasteFormatBetweenDocuments = Word.WdPasteOptions.wdKeepSourceFormatting
' when contents is copied from a document that uses styles
oWord.Options.PasteFormatBetweenStyledDocuments = Word.WdPasteOptions.wdKeepSourceFormatting
' when pasting from an external source such as a web page
oWord.Options.PasteFormatFromExternalSource = Word.WdPasteOptions.wdKeepSourceFormatting
Do Word documents have any scripting capabilities for presenting dynamic information? What I want to do is have the document dynamically pull in the contents of an HTML file and append it at the end of the document every time it is opened.
Anybody know if this is possible?
Put the following in a code module. The AutoOpen routine is run each time the document is opened.
Public Sub AutoOpen()
Dim currentDocument As Document
Set currentDocument = ActiveDocument
Dim sourceDocument As Document
Set sourceDocument = Application.Documents.Open(FileName:="e:\mySourceName.docx")
sourceDocument.Range.Copy
sourceDocument.Close wdDoNotSaveChanges
Set sourceDocument = Nothing
Dim pasteRange As Range
Set pasteRange = currentDocument.Range
pasteRange.Collapse wdCollapseEnd
pasteRange.Paste
End Sub