Results 1 to 11 of 11

Thread: [RESOLVED] Word Interop - Add paragraph and InsertFile

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Resolved [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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Word Interop - Add paragraph and InsertFile

    Quote Originally Posted by dbasnett View Post
    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

  3. #3

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    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

  5. #5

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Word Interop - Add paragraph and InsertFile

    Quote Originally Posted by TnTinMN View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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."

  8. #8

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Word Interop - Add paragraph and InsertFile

    Quote Originally Posted by SJWhiteley View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    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.

  11. #11

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: [RESOLVED] Word Interop - Add paragraph and InsertFile

    Quote Originally Posted by TnTinMN View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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
  •  



Click Here to Expand Forum to Full Width