none
InterOp Woe's RRS feed

  • Question

  • Day 5 of my epic odyssey with Interop...

    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.

            Dim newPara As Word.Paragraph = docAChap.Paragraphs.Add
            With newPara
                .Range.Text = "InteropMonkey" ''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.

    Also, someone please tell me there is a way to insert a file without losing the format of the file being inserted.  Please.

            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.  If there is an option to the .InsertFile method that says insert formatted or un-formatted I have yet to find it.  It is hard to believe that it wouldn't be an option.

    At this point if someone told me I needed flying monkeys farting in my office to get Interop to work I'd give it a try.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


    Wednesday, March 23, 2016 11:38 PM

All replies

  • Hi dbasnett,

    Based on your description, your question is more related to Word Development. We will move your thread to Word for Developers forum for better support.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, March 24, 2016 1:52 AM
  • Hi dbasnett,

    Based on your description, your question is more related to Word Development. We will move your thread to Word for Developers forum for better support.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Not one response in over 18 hours.  I'd have to say that we either have a different definition of 'better support, or this is really complicated and I am being impatient.

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Thursday, March 24, 2016 7:55 PM
  • Hi dbasnett,

    Sorry for the delay.

    >> The range cannot be deleted error only happens sometimes.

    What is version of your word application? I made a test with the code below under Word 2013, but I failed to reproduce your issue. Could you share us complete code and details steps to reproduce your issue?

       Private Sub AddParagraph_Click(sender As Object, e As EventArgs) Handles AddParagraph.Click
            Dim app As Word.Application
            Dim doc As Word.Document
            Dim p As Word.Paragraph
            Dim rng As Word.Range
            app = New Word.Application
            app.Visible = True
            doc = app.Documents.Open("D:\work\20160325.docx")
            p = doc.Paragraphs.Add
            With p
                .Range.Text = "test"
                .Range.Font.Bold = 1
                .Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
            End With
            rng = doc.Range(Start:=DirectCast(doc.Content.End - 1, Object), End:=DirectCast(doc.Content.End, Object))
            rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
            rng.InsertFile(FileName:="D:\work\Test (2).docx".ToString)
        End Sub

    >> This inserts the file but formatting is gone.

    Which format is gone, I make a test with above code, the format of text like color is correct.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Monday, March 28, 2016 8:21 AM
  • At this point if someone told me I needed flying monkeys farting in my office to get Interop to work I'd give it a try.


    This has nothing to do with Interop, per se. Maybe you should take more time to learn how Word works and how to use the tools at your disposal. Posting in the correct forum would help, too.

    The .InsertFile method inserts a source document, with all the Style names in use, into a target document. If any of the Styles used in the source document are defined in the target document, the Style definitions existing in the target document will be applied to the imported content. This may result in the format of the inserted content changing. This is by design. If you don't want that, you have other options. For example:
    1. Open both files and uses a method like:
    DocTgt.Range.PasteAndFormat Type:=wdFormatOriginalFormatting
    to preserve the original formatting, or:
    DocTgt.Range.PasteSpecial Type:=wdPasteText
    to paste as plain text; or
    2. Open both files and uses a method like:
    DocTgt.Range.FormattedText = DocSrc.Range.FormattedText
    to preserve the original formatting, or:
    DocTgt.Range.Text = DocSrc.Range.Text
    to paste as plain text.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, March 29, 2016 2:53 AM
  • ... Maybe you should take more time to learn how Word works and how to use the tools at your disposal. Posting in the correct forum would help, too.

    ...This may result in the format of the inserted content changing. This is by design. If you don't want that, you have other options. For example:
    1. Open both files and uses a method like:
    DocTgt.Range.PasteAndFormat Type:=wdFormatOriginalFormatting
    to preserve the original formatting,...


    Cheers
    Paul Edstein
    [MS MVP - Word]

    After four days of no response I get attitude, and a restatement of the problem, yes the formatting is gone. 

    At this point I know all the 'Word' I ever want to know.  As I said in my original post, it sure would have been nice to have the option to insert a file and preserve the formatting.  Wonder if MS has ever heard that asked as an improvement?

    I knew about option 1, but was told by some other 'Word' guru how stupid I was, use insert file.  Insert file cut the run time of this from 5.5 hours to 1.5 hours.

    It is back to 5.5 and never touch any code that has to do with Word. 

    In the two and half years I have worked for the Missouri General Assembly I have advocated for the switch from Word Perfect to MS Word.  Recently, because of the attitude of this community, I am wondering if I am sticking my neck out for no reason.  I have nothing to gain and everything to lose.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Tuesday, March 29, 2016 3:04 PM
  • At this point I know all the 'Word' I ever want to know.

    And therein lies to root of the problem.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, March 29, 2016 8:50 PM
  • At this point I know all the 'Word' I ever want to know.

    And therein lies to root of the problem.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Really?  The problem couldn't possibly be with the interface and documentation? 

    The other common comment I kept receiving was that it was a mistake to use interop, I should be using OpenXML.

    You know, at this point it works.  It is slow.  Thanks for all of the support after four days.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


    • Edited by dbasnett Tuesday, March 29, 2016 9:23 PM
    Tuesday, March 29, 2016 9:22 PM
  • Hi dbasnett,

    >> As I said in my original post, it sure would have been nice to have the option to insert a file and preserve the formatting.

    As my above reply, the formatting did not go while inserting the file. I assume it is related with your word document, I suggest you use word application to create a new word document, and make a test again. If this issue still exist when you create a new document, it would be helpful if you could share us your word application version, and the document you created through onedrive.

    >> Insert file cut the run time of this from 5.5 hours to 1.5 hours.

    Do you mean inserting file waste about 5.5 hours? It is really amazing. Do you insert with code or manually? I suggest you manually insert file to check how long it takes.

    >> You know, at this point it works.  It is slow.  Thanks for all of the support after four days.

    Do you want to keep following this issue? Based on this, it seems it works. If you would close this thread, I would suggest you mark the helpful reply as answer to close this thread.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, March 30, 2016 2:19 AM
  • Inserting file - run time 1.5 hours - formatting lost

    Open document, copy, then paste - run time 5.5 hours - formatting preserved.

    I wanted to take the survey, but this is the response clicking on the link.

    Bad Request - Request Too Long


    HTTP Error 400. The size of the request headers is too long.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Wednesday, March 30, 2016 2:58 AM
  • Hi dbasnett,

    >> I wanted to take the survey, but this is the response clicking on the link.

    For this, I have submitted a feeback.

    >> Inserting file - run time 1.5 hours - formatting lost
    >> Open document, copy, then paste - run time 5.5 hours - formatting preserved.

    For this, you just told us the result, but nothing about your document. As my above reply, I could not reproduce your issue with a word file under Word 2013. Is this issue exist in a new simple word document? Could you share us more information about your document or could you share us the document through OneDrive?

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, March 30, 2016 5:52 AM