How to use a macro to "tab" between form fields, in a table, in a protected Word document? - vba

I'm writing a macro which copies data from an excel spreadsheet to a word document (to save myself time copy/pasting). The word document is protected and has legacy form fields to enter data. I'm trying to figure out how to iterate between these fields. For most of the fields, Selection.Next(Unit:=wdParagraph, Count:=1).Select works just fine. However, some of the fields are in a table, which that solution doesn't work for.
Selection.Next(Unit:=wdParagraph, Count:=1).Select will get me into the form in the first cell of the table. But it stops there, failing to go to the next field. I've tried a number of variations, such as increasing the Count, and for Unit tried wdParagraph, wdItem, wdCell, wdColumn, wdTable, wdSection, and wdCharacter. No matter what I try, the selection stays in the first cell of the table.
I've also tried Selection.TypeText, Selection.NextField.Select and Selection.MoveDown (and similar), which throw 4605 errors that I assume are due to document protection.

Found the solution:
<document variable here>.FormFields(<index of form field in table>).Select

Related

How to set curser at the end of Word Document from VB.Net?

I am doing Copy/Paste for tables from word document to another Word document via VB.Net, but it either keeps two line in between or merge the table.
I am utilizing VB.Net in automating Word document, I am copying one formatted table from a word document, and then paste it into a different word document.
the problem here is that I have to put a "separater" between the newly pasted table and the one pasted earlier, otherwise word will merge the two (and will keep merging every single newly pasted table).
I tried to put this code before Pasting
oWord.Selection.MoveDown(Word.WdUnits.wdLine, 0)
oWord.Selection.InsertBreak(Word.WdBreakType.wdLineBreak)
oWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault)
it worked fine but it puts two line instead of one.
appreciate if anyone give me a way to keep Pasting (or even adding new paragragh) remain always at the end of the document (with only one line-width separation)
I got a very acceptable solution
instead of using
oWord.Selection.MoveDown(Word.WdUnits.wdLine, 0)
oWord.Selection.InsertBreak(Word.WdBreakType.wdLineBreak)
oWord.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault)
to insert a break, I used the following code, and it worked perfectly
With oWord.Selection
.Collapse(Direction:=Word.WdCollapseDirection.wdCollapseStart)
.InsertParagraph()
.Collapse(Direction:=Word.WdCollapseDirection.wdCollapseEnd)
End With
hope this be useful for anyone facing same problem

Saving cell Presets in a dropdown list

At the moment I have created a spreadsheet which takes a bunch of inputs, runs them through a list of formulas, and then spits out the results onto a "report" worksheet.
I've been manually saving each of these reports as separate CSVs but I was hoping for a better method moving forward as it is getting quite tiring to have to open 10 CSVs when i do my monthly reports.
I am looking for a way to start saving all of these reports into a "database". My hope to to have one cell be for an user entry name and for two buttons. One to save the current report under the name entered by the user, and two to remove old records. I would then be able to revisit old entries by selecting them in the dropdown.
I've dabbled with VBA and Macros in the past but this is a little more complicated than what I've dealt with in the past. Looking for some help/direction.
Thanks for your time!
Depending on how your reports need to be used, you might find it satisfactory to simply make your data into one big Excel Table ( Insert Tab > Table ). When you do this, Excel will automatically fill-down any formulas that you enter in a column, and also show the formula using the headers instead of A1-style references.
I use this format, adding Y under Remove from Active List on each line that is already done. Then whenever I save the file or look at it for today's status, I filter out what's old and just look at the new. The other filters enable copy-pasting or printing whatever arrangement I like.
The filters and other things in the table can be referenced in VBA as Sheets("ThisSheet").ListObjects(1), which is an object with a number of useful properties and methods.
For VBA information, read more here: https://www.thespreadsheetguru.com/blog/2014/6/20/the-vba-guide-to-listobject-excel-tables
This is my code for auto-filtering the table to hide inactive items at time of save. You add it at ThisWorkbook in the VBA editor:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets("Sheet1").Activate
SelectedCell = ActiveCell.Address 'this saves your screen selection for after the filtering
ActiveSheet.ListObjects(1).Range(1, 1).Select
If ActiveSheet.ListObjects(1).AutoFilter.FilterMode = True Then
ActiveSheet.ListObjects.Item(1).AutoFilter.ShowAllData
End If
A = ActiveSheet.ListObjects(1).Range.Rows(1).Find("Remove from List").Column - _
ActiveSheet.ListObjects(1).Range.Column + 1
ActiveSheet.ListObjects(1).Range.AutoFilter field:=A, Criteria1:="="
Range(SelectedCell).Select
End sub

Insert several Autotext items in order - vba Word

I am working with a Macro enable Word 2016.
I have a macro that will insert several AutoText entries (4) which are entered in a loop. This portion is working ok but I notice that they are not entered in order.
The reason why they're not entered in order is because in each loop iteration I always reference a specific bookmark (as a range). I wonder if there is a way to update the range at each iteration so the AutoText entries are entered one after the other ones?
Thank you!
After some trial and error, I found a way to do this. What I did was the following:
Moved the selection (cursor) to the desired bookmark
Insert the AutoText at the Selection.range location
For all the next items, I always used the Selection.range instead of using the bookmark, since the Selection (cursor) will update as I'm adding text.

Insert blank form

I created a form in Word and protected everything except the form fields.
I want to add the form without any user inserts on the next page, so the user can fill the form for another device.
EDIT: also is there a way to exclude not filled form fields from the print?
You can use Word macros using VBA (read more here). To insert new page to your document, you can run this code:
Selection.InsertBreak Type:=wdPageBreak
To better understand how to work with Word and VBA refer to this page, it might be very helpful as a start point:
http://msdn.microsoft.com/en-us/library/ff604039(v=office.14).aspx

Copy ComboBox from a Word Document to another Word Document with vba

I have a combo box (combo1) in a word document and I would like to copy it to another word document. (end game will be looping through 100's of docs).
I can't for the life of me work out how to select and/or copy the combo box, although its easy enough to do outside of vba.
So far I've tried turning it into a bookmark, seems to select ok, but won't copy.
ActiveDocument.Bookmarks(combo1_bm).Select
Selection.Copy
I thought it would be able to done as an inline shape (as that is how they are added?), however again the selection appears to work, but the Copy doesn't.
ActiveDocument.InlineShapes(combo1).Select
Selection.Copy
Any ideas on what I can try next?
Cheers,
Michael
Your attempt with bookmark was quite good. You just need to extend your code a bit:
ActiveDocument.Bookmarks("combo1_bm").Range.Copy
....
Selection.Paste 'or different pasting procedure
Keep in mind that you don't need to select object before copy it. Just try doing as I show above. Moreover, don't miss quotation marks for names or use bookmark index to work with appropriate one. Copy method will copy inside range of the bookmark and keep original bookmark in place, unchanged.
This should do the trick.
Set ComboBox1Range = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("combo1_bm").Range.Start - 1, _
End:=ActiveDocument.Bookmarks("combo1_bm").Range.End)
ComboBox1Range.Expand Unit:=wdParagraph
ComboBox1Range.Copy