How to set curser at the end of Word Document from VB.Net? - 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

Related

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

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

Unable to extract and re-insert MS Word Content Control using VBA and InsertXML

This question is related to my other question: Range.InsertXML using Transform
In MS Word it is easy to insert a content control using VBA, for example:
ThisDocument.ContentControls.Add wdContentControlRichText, Selection.Range
I've recently started exploring more in the XML side of things, e.g.:
Debug.Print ThisDocument.Range.XML seems to (or actually does) produce the XML for a Word document. However, if I create a NEW, BLANK document and add a Content Control I am unable to extract and reinsert the Content Control (oCC).
My steps:
added 2 blank paragraphs to a new document
added oCC to the 2nd paragraph
selected the oCC paragraph
immediate window: thisdocument.Paragraphs(1).Range.InsertXML selection.Range.XML
At first glance it LOOKS like the Content Control was duplicated, BUT on closer inspection, it was deleted and only the formatted text remains (see image, top paragraph is actually just formatted text).
Thinking I could out smart MS Word I set the properties of the Content Control to '...can not be deleted', but that didn't help.
I've also tried to insert into a separate document in case the issue had something to do with duplication of something that ought to have been unique.
In a nutshell:
To answer this question I need a way to insert a Content Control to a document using a combination of VBA and XML (or confirmation that what I am attempting is not possible).
Just realized I should use Selection.Range.WordOpenXML instead of Selection.Range.XML

Word / PDF - Merge Documents

I am looking to merge two documents, however it is not your typical merge.
My first document is a mailmerge, creating a cover letter, basically each page has a name and address
My next document is a static document that cannot be changed.
I need to insert the static document into my first merged document, but after every page, therefore, for every one page a document is inserted.
I have tried the insert document in both word 2010 and pdf using adobe acrobat, and as you have thought it only inserted one document after the first page.
I'm looking at VBA, but I have never utilized VBA and word before
Any pointers would be appreciated.
Many thanks
I should have spent more time on this.
The original template contains fields to merge.
On the static document that I mention, click insert tab, Text Section, select Object - Text From File
Select the cover letter / template that contains the fields to merge. This will insert the template followed by the static document that cannot be changed
Note I have spotted some formatting changes on the template following merge - further work required
From this point start your mail merge, and complete merge to Adobe or word.
This creates a mail merged document containing the cover letter with name and address fields followed by the static document.
Extremely simple. I always over complicate things!
I'll work on the changed formatting, but other then that this works

Remove first line from Word Document process it and put a new line at the beginning of the document

I am developing an application wherein I need to delete the first line of several word documents, merge them, merge their tables into one and place it at the top, remove all the individual tables which are merged, and then giving the merged table a name.
I am through with almost everything except removing the first line of the individual word documents (which will be merged into one) and Putting name string to the newly generated table in the new document on top of the table. The newly generated table will be placed at the top of the new document (which is result of merging of all other documents).
Can someone suggest a way to do it. If possible, deletion of first two lines will help more.
Removing first line could mean removing first paragraph or just first line as you could see it. They are not the same therefore I provide both solution below
'Remark: for ActiveDocument
'removing first paragraph
ActiveDocument.Paragraphs(1).Range.Delete
'removing first line
ActiveDocument.Range(0, 0).Select
Selection.MoveEnd wdLine
Selection.Delete

Convert xls File to csv, but extra rows added?

So, I am trying to convert some xls files to a csv, and everything works great, except for one part. The SaveAs function in the Excel interop seems to export all of the rows (including blank ones). I can see these rows when I look at the file using Notepad. (All of the rows I expect, 15 rows with two single quotes, then the rest are just blank). I then have a stored procedure that takes this csv and imports to the desired table (this works on spreadsheets that have been manually converted to csv (e.g. open, File--> Saves As, etc.)
Here is the line of code I am using for my SavesAs in my code. I have tried xlCSV, xlCSVWindows, and xlCSVDOS as my file format, but they all do the same thing.
wb.SaveAs(aFiles(i).Replace(".xls", "B.csv"), Excel.XlFileFormat.xlCSVMSDOS, , , , False) 'saves a copy of the spreadsheet as a csv
So, is there some additional step/setting I need to do to not get the extraneuos rows to show up in the csv?
Note that if I open this newly created csv, and then click Save As, and choose csv, my procedure likes it again.
When you create a CSV from a Workbook, the CSV is generated based upon your UsedRange. Since the UsedRange can be expanded simply by having formatting applied to a cell (without any contents) this is why you are getting blank rows. (You can also get blank columns due to this issue.)
When you open the generated CSV all of those no-content cells no longer contribute to the UsedRange due to having no content or formatting (since only values are saved in CSVs).
You can correct this issue by updating your used range before the save. Here's a brief sub I wrote in VBA that would do the trick. This code would make you lose all formatting, but I figured that wasn't important since you're saving to a CSV anyway. I'll leave the conversion to VB.Net up to you.
Sub CorrectUsedRange()
Dim values
Dim usedRangeAddress As String
Dim r As Range
'Get UsedRange Address prior to deleting Range
usedRangeAddress = ActiveSheet.UsedRange.Address
'Store values of cells to array.
values = ActiveSheet.UsedRange
'Delete all cells in the sheet
ActiveSheet.Cells.Delete
'Restore values to their initial locations
Range(usedRangeAddress) = values
End Sub
Tested your code with VBA and Excel2007 - works nice.
However, I could replicate it somewhat, by formatting an empty cell below my data-cells to bold. Then I would get empty single quotes in the csv. BUT this was also the case, when I used SaveAs.
So, my suggestion would be to clear all non-data cells, then to save your file. This way you can at least exclude this point of error.
I'm afraid that may not be enough. It seems there's an Excel bug that makes even deleting the non-data cells insufficient to prevent them from being written out as empty cells when saving as csv.
http://answers.microsoft.com/en-us/office/forum/office_2010-excel/excel-bug-save-as-csv-saves-previously-deleted/2da9a8b4-50c2-49fd-a998-6b342694681e
Another way, without a script. Hit Ctrl+End . If that ends up in a row AFTER your real data, then select the rows from the first one until at least the row this ends up on, right click, and "Clear Contents".