Selecting top of word document using VBA - excel

I am using excel VBA to populate data onto a word template from an excel spreadsheet. The problem I am having is selecting the top of the word document so the information goes into the right spot.
I have tried using Selection.HomeKey wd:=Story and Selection.MoveUp Unit:=wdScreen, count:=1 both with no success as I am consistently getting
runtime error 4120

Please try
ActiveDocument.Bookmarks("\StartOfDoc").Select
It makes use of a pre-defined, implicit bookmark to take you to the beginning of the document.
I just tested in Word 2016 and it works.

Related

Edit word document through excel

i am trying to have VBA open up a specific word document (this document is protected but has boxes where you are able to edit text). i will then need to fill out said word documents editable areas with variables in the excel document. i have no idea where to start with this code and was posting to see if this is possible and how i could do it.
Additional note. Would it be easier to have vba create a whole new word document in the code so it can just add in all the text?
Since there isn't any code. I'm not sure what stage you're at in this but, from how I read your question you have two ways to do this. Either have your vba code in the word document that opens an Excel file to read cells or have the excel document create a word document with information already filled out. I would recommend using Word to read Excel.
This Question has information about setting fill-able values in Word. There are also a lot of resources out there to find out how to get cell values and open excel documents using VBA.
All this being said. I would say you should probably look for alternatives to all of this. Such as powerapps connecting to an excel file or change your excel file to a SQLExpress database to use that in finding data. The reason I recommend this is because it's much more scalable.

Excel to Word - Formatted with Titles, Heading 1, via ActiveX Check Box

Please see attached image.
I showed a simplified version of what my sheet will be doing. But I will have nearly 100 check boxes, with 100 different topics.
My issue is I want to ONLY show content on the Word Doc when the associated checkbox is "true" on the excel sheet. It must also have 'Heading 1' formatting, and hyperlinks. What is my best path forward?
Have all the content prepopulated in the Word Doc, and have a macro that deletes content that isn't checked in the excel doc?
Start with a blank Word Doc, and populate only content that is checked in Excel.
I have successfully created a way to populate data from excel to word using bookmarks, but not sure how to remove content I don't need.
Thanks so much for your time!
excel to word example

Generating Word financial statements from Excel workbooks

I am trying to generate a Word document that consists of Excel information. When the Excel document updates I would like for the Word document to do the same.
I have managed to setup a template in Excel which carries over the information into Word seamlessly. The formatting is still a struggle though and if the Excel document changes I need to reproduce the whole Word doc from scratch again.
I am wondering whether it is possible to create the template in Word and "connect" it to the Excel (as the underlying information) easily using VBA?
What are some of the best practices or suggestions to try and go about doing something like this.
In Word, on the Insert tab, look at Insert Object. You have the option of inserting / linking part of the contents an Excel worksheet in the Word document. Once linked, whenever you open the Word document, its contents is automatically refreshed with the latest contents from the linked Excel worksheet.
The only thing that you have to be careful about with this solution, is that if you move your Excel files with respect to the Word document (to another folder), then when you open the Word document, Word will not be able to find the Excel documents. When implementing a solution like this, both the Word document and the linked Excel documents are typically kept / copied / moved together.

Populate word template from excel vba

On this discussion it is well explained how to populate Word Templates from excel data using Bookmarks on Word.
In my case, I need to populate the template as many times as the Excel's rows are.
Attached you can find an example. Sorry if I don't add any VBA draft code, but I don't have any idea of how to set it up.
The excel table is:
The word template is:
And the result should be:

Fonts look different when inserting Excel table in a Word document

I am trying to use a VBA macro (for Office 2003) to do the following:
In Excel, the user will select a range of cells
In Word the user will call the macro (via a button or shortcut) to insert the selected Excel range as an embedded object
The code is not the problem so far, my problems are:
Given that the user is working in a Word document, most likely will use the same fonts in Excel
When Excel range was inserted in Word and they both use the same font names and sizes, they look different inside Word ( fonts look as if they stretched a bit)
Styling Cell borders in Excel is not like styling cell borders in Word
I do appreciate any advice on this regards
When you paste as an Excel Worksheet Object, what Word is actually displaying is an image created by Excel. Notice that you can't select any text, for example. Word appears to be distorting the image ever so slightly, so that the fonts won't line up.
Edit: I can't speak for Office 2003, but Office 2007 defaults to the HTML format using PasteExcelTable. The exact macro statement is
Selection.PasteExcelTable False, False, False
This will give formatting that is compatible with Word, but unfortunately the data is not live and won't get updated as the spreadsheet is changed. If your requirements don't include live update, try this method.
Is it possible to have the macro creating a table out of the cells (as normally happens when you manually copy them), rather than inserting an Excel Object.
The best visual results results can be achieved by using Selection.CopyPicture(Appearance, Format); however, you will not be able to edit the data inside Word as you will only get a picture.

Resources