none
Insertar Header en documento de Word

    Question

  • Estamos trabajando en un projecto para modificar documentos de word e inserta cabeceras.

    Hemos conseguido inserta texto en la cabecera con difuntes formatos y una linea al final.

    El problema lo tenemos al insertar una imagen ya que descuadra la posicion de la linea y no sabemos como ajustarlo.

    Este es el codigo usado:

    Private

    SubButton1_Click(ByValsender AsSystem.Object, ByVale AsSystem.EventArgs) HandlesButton1.Click

           

    DimwordApplication AsNewApplication()

           

    DimwordDocument AsDocument= Nothing


           

    DimsourceDocPath AsString= AppDomain.CurrentDomain.BaseDirectory & "files\Documentdetreball.rtf"


           

    DimtargetDocPath AsString= AppDomain.CurrentDomain.BaseDirectory & "files\aal.rtf"


           

    DimMyPhoto AsNewBitmap(AppDomain.CurrentDomain.BaseDirectory & "files\saa.png")

           

    DimparamSourceDocPath AsObject= sourceDocPath

           

    DimparamMissing AsObject= Type.Missing

           

    Try


                wordDocument = wordApplication.Documents.Open(paramSourceDocPath, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, _

                 paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, _

                 paramMissing, paramMissing, paramMissing, paramMissing)

               

    IfwordDocument IsNotNothingThen


                   

    '--------    


                   

    DimMyRange1 AsRange


                    MyRange1 = wordDocument.Sections(1).Headers(

    WdHeaderFooterIndex.wdHeaderFooterPrimary).Range

                   

    WithMyRange1

                        .ParagraphFormat.Alignment =

    WdParagraphAlignment.wdAlignParagraphRight

                        .Font.Name =

    "Arial"


                        .Font.Size =

    "16"


                        .Font.Bold =

    True


                        .InsertBefore(

    "Informacio Clinica"& vbCrLf)

                   

    EndWith


                   

    DimMyRange2 AsRange


                    MyRange2 = wordDocument.Sections(1).Headers(

    WdHeaderFooterIndex.wdHeaderFooterPrimary).Range

                   

    WithMyRange2

                        .Start = MyRange1.End

                        .End = MyRange1.End

                        .ParagraphFormat.Alignment =

    WdParagraphAlignment.wdAlignParagraphRight

                        .Font.Name =

    "Arial"


                        .Font.Size =

    "12"


                        .Font.Bold =

    True


                        .InsertBefore(

    "pacient"& vbCrLf & "Hc:"& "999"& " Ed: "& "24a"& vbCrLf)

                   

    EndWith


                   

    IfMsgBox("insertar imagen", vbYesNo) = vbYes Then


                       

    DimMyRangeImg AsRange


                        MyRangeImg = wordDocument.Sections(1).Headers(

    WdHeaderFooterIndex.wdHeaderFooterPrimary).Range

                        MyRangeImg.InlineShapes.AddPicture(

    "c:\temp\files\saa.png")

                   

    EndIf


                   

                   

    DimMyRange3 AsRange


                    MyRange3 = wordDocument.Sections(1).Headers(

    WdHeaderFooterIndex.wdHeaderFooterPrimary).Range

                    wordDocument.Sections(1).Headers(

    WdHeaderFooterIndex.wdHeaderFooterPrimary).Shapes.AddLine(0, _

                                                                                              wordDocument.PageSetup.HeaderDistance + MyRange3.End, _

                                                                                               wordDocument.PageSetup.PageWidth - wordDocument.PageSetup.RightMargin - wordDocument.PageSetup.LeftMargin, _

                                                                                               wordDocument.PageSetup.HeaderDistance + MyRange3.End, MyRange3)

                    wordDocument.SaveAs2(targetDocPath)

               

    EndIf


           

    Catchex AsException


                MsgBox(ex.Message)

           

    Finally


               

    IfwordDocument IsNotNothingThen


                    wordDocument.Close(paramMissing, paramMissing, paramMissing)

                    wordDocument =

    Nothing


               

    EndIf


               

    IfwordApplication IsNotNothingThen


                    wordApplication.Quit(paramMissing, paramMissing, paramMissing)

                    wordApplication =

    Nothing


               

    EndIf


               

    GC.Collect()

               

    GC.WaitForPendingFinalizers()

               

    GC.Collect()

               

    GC.WaitForPendingFinalizers()

                System.Diagnostics.

    Process.Start(targetDocPath)

               

    End


           

    EndTry


       

    EndSub


    Un saludo, Albert Arnau

    Thursday, May 24, 2012 11:51 AM

All replies