-
Mar 23rd, 2016, 03:57 PM
#1
[RESOLVED] Word Interop - Add paragraph and InsertFile
Really I am trying, but I am beginning to believe that I hate Interop.
I am trying to add a paragraph to the end of a document I have open.
Code:
Dim newPara As Word.Paragraph = docAChap.Paragraphs.Add
With newPara
.Range.Text = _schn ''The range cannot be deleted error here sometimes
.Range.Font.Bold = 1
.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
End With
The range cannot be deleted error only happens sometimes.
It has to be something simple, like me.
Someone please tell me there is a way to insert a file without losing the format of the file being inserted.
Code:
rng = docAChap.Range(Start:=DirectCast(docAChap.Content.End - 1, Object), End:=DirectCast(docAChap.Content.End, Object))
rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
rng.InsertFile(FileName:="pathHere".ToString)
This inserts the file but formatting is gone.
I know that this looks like a two topic thread, but it is only one, me hatin' on Interop
Last edited by dbasnett; Mar 23rd, 2016 at 04:06 PM.
-
Mar 23rd, 2016, 05:36 PM
#2
Re: Word Interop - Add paragraph and InsertFile
Originally Posted by dbasnett
Really I am trying, but I am beginning to believe that I hate Interop.
I am trying to add a paragraph to the end of a document I have open.
Code:
Dim newPara As Word.Paragraph = docAChap.Paragraphs.Add
With newPara
.Range.Text = _schn ''The range cannot be deleted error here sometimes
.Range.Font.Bold = 1
.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
End With
The range cannot be deleted error only happens sometimes.
It has to be something simple, like me.
Someone please tell me there is a way to insert a file without losing the format of the file being inserted.
Code:
rng = docAChap.Range(Start:=DirectCast(docAChap.Content.End - 1, Object), End:=DirectCast(docAChap.Content.End, Object))
rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
rng.InsertFile(FileName:="pathHere".ToString)
This inserts the file but formatting is gone.
I know that this looks like a two topic thread, but it is only one, me hatin' on Interop
I am actively working on a project that involves creating letters from the ground up, so I have been working with Word Interop a lot lately. In some ways, Interop.Word is similar to Interop.Excel, but Word seems to be vastly under-documented compared to Excel. That is the probably the most aggravating part, I have had to experiment with everything. The real options I came across (besides buying an expensive shiny library) is to either automate using Interop or automate via OpenXML and roll your own.
Anyhow, here is some example code that you can meddle with if it helps:
Code:
Try
'create word object
Dim objword As New Word.Application
objword.Visible = True
'create document
Dim objWordDoc As Word.Document
objWordDoc = objword.Documents.Add()
'create selection variable
Dim objWordSelection = objword.Selection
'go to end
objWordSelection.EndKey(Microsoft.Office.Interop.Word.WdUnits.wdStory)
'type two paragraph lines
objWordSelection.TypeParagraph()
objWordSelection.TypeParagraph()
'trap start range, add text, trap end range (if needed for formatting)
Dim rangeStart = objword.Selection.Start()
objWordSelection.TypeText("roll your own variable here.")
Dim rangeEnd = objword.Selection.End()
'add formatting between ranges
With objword.ActiveDocument.Range(rangeStart, rangeEnd)
.Font.Bold = True
End With
'add paragraph
objWordSelection.TypeParagraph()
'insert file
objWordSelection.InsertFile("C:\Users\jason\Desktop\roll your own file here.docx")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
-
Mar 23rd, 2016, 06:09 PM
#3
Re: Word Interop - Add paragraph and InsertFile
Thanks, on day 5 of my epic odyssey with Interop I'll give it a try. Hell, at this point if someone told me I needed flying monkeys farting in my office I'd give that a try.
-
Mar 23rd, 2016, 09:26 PM
#4
Re: Word Interop - Add paragraph and InsertFile
The "range can not be deleted" error is often associated with attempting to redefine a Form field range. I guess that it is possible that added paragraphs range somehow overlaps a field or some other entity that can not be deleted. I have seen an added Word.Paragraph range extend to the beginning of the preceding sentence if it is not terminated with a hard return. So to make certain that the new paragraphs range is at the end, Collapse it.
Code:
Dim newPara As Word.Paragraph = docAChap.Paragraphs.Add
With newPara
.Range.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
.Text = _schn ''The range cannot be deleted error here sometimes
.Range.Font.Bold = 1
.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
End With
-
Mar 23rd, 2016, 09:32 PM
#5
Re: Word Interop - Add paragraph and InsertFile
Originally Posted by TnTinMN
The "range can not be deleted" error is often associated with attempting to redefine a Form field range. I guess that it is possible that added paragraphs range somehow overlaps a field or some other entity that can not be deleted. I have seen an added Word.Paragraph range extend to the beginning of the preceding sentence if it is not terminated with a hard return. So to make certain that the new paragraphs range is at the end, Collapse it.
Code:
Dim newPara As Word.Paragraph = docAChap.Paragraphs.Add
With newPara
.Range.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
.Text = _schn ''The range cannot be deleted error here sometimes
.Range.Font.Bold = 1
.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
End With
I'll look tomorrow and see if I didn't try just that, but maybe not that way. You wouldn't believe how much code I have written trying to get this to work. GOL fix this weekend I think. Thanks.
-
Mar 24th, 2016, 09:06 AM
#6
Re: Word Interop - Add paragraph and InsertFile
I tried several of the suggestions and they didn't work.
What eventually did work was this,
Code:
Dim strt As Integer = rng.Start
rng.Text = _schn & Environment.NewLine & Environment.NewLine
rng = docAChap.Range(Start:=DirectCast(strt, Object), End:=DirectCast(docAChap.Content.End, Object))
rng.Font.Bold = 1
rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
Thanks all for your assistance.
Going to work on the file now...
Last edited by dbasnett; Mar 24th, 2016 at 09:10 AM.
-
Mar 24th, 2016, 09:14 AM
#7
Re: Word Interop - Add paragraph and InsertFile
I know this may not help, but have you tried using Open XML? I used it in a one off situation to convert documents to a new template. I really wasn't looking forward to using Interop (it's basically horrible and should go away), and while it has a steep learning curve and is under documented, it gives a lot more structured and predictable result, with a few quirks.
Going forward, I'd recommend it. I won't say it's an easy task, but better than eye-poke that is called interop.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Mar 24th, 2016, 02:53 PM
#8
Re: Word Interop - Add paragraph and InsertFile
Originally Posted by SJWhiteley
I know this may not help, but have you tried using Open XML? I used it in a one off situation to convert documents to a new template. I really wasn't looking forward to using Interop (it's basically horrible and should go away), and while it has a steep learning curve and is under documented, it gives a lot more structured and predictable result, with a few quirks.
Going forward, I'd recommend it. I won't say it's an easy task, but better than eye-poke that is called interop.
No I haven't tried Open XML. Other than volume we weren't doing anything that I considered complex, but leave it to Interop to make simple things complicated.
The answer to inserting the files is not to. This has been so mind numbing that I forgot that I originally was opening each part and copy / pasting it, but was told by an 'Interop' expert to use Insert file. When I went back to copy / pasting all my formatting is there!!!!
I am going to mark this resolved, and thanks to all who participated.
-
Mar 25th, 2016, 02:12 PM
#9
Re: Word Interop - Add paragraph and InsertFile
One final thought. Copy and pasting fixed the problem, but it takes four times as long to run. If we do the entire publication it now takes six hours. Thanks Interop developers for not having an option to keep the formatting of the file being inserted.
-
Mar 25th, 2016, 08:12 PM
#10
Re: [RESOLVED] Word Interop - Add paragraph and InsertFile
I assume the formatting that you are referring too is the page layout formatting and not the Font formatting. After some digging, it appears as though the proper command to use is Range.ImportFragment, at least according to the documentation.
However, the command did not work in the Word 2007 version in which it first appeared and it also seems that it did not preserve the layout formatting in Word 2010 as well (see: https://code.msdn.microsoft.com/offi...t-and-e7660288). Perhaps, this has been fixed in later versions.
-
Mar 25th, 2016, 08:22 PM
#11
Re: [RESOLVED] Word Interop - Add paragraph and InsertFile
Originally Posted by TnTinMN
I assume the formatting that you are referring too is the page layout formatting and not the Font formatting. After some digging, it appears as though the proper command to use is Range.ImportFragment, at least according to the documentation.
However, the command did not work in the Word 2007 version in which it first appeared and it also seems that it did not preserve the layout formatting in Word 2010 as well (see: https://code.msdn.microsoft.com/offi...t-and-e7660288). Perhaps, this has been fixed in later versions.
Didn't come across that one, and I am using 2010, so... We normally don't print the entire set of books so after this year it won't matter. For the next 10 years we will print supplements if we print at all. I am pushing to make the online version the 'official' version, and as the Legislature gets younger there is more and more support.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|