changing header and table text in word programmatically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BlackMustard
    New Member
    • Aug 2007
    • 88

    changing header and table text in word programmatically

    hi,

    i'm using the following code to modify a standard word document and save it to disk with a new file name.
    Code:
    Sub CreateQuote()
    '***************************************************************************
    'Quotation part
    '***************************************************************************
        Dim appWord As New Word.Application
        
        Dim intRMA, i As Integer
        Dim strDate, strFileName As String
        Dim Range As Range
        
        'Define the path where to save the RMA.doc
        intRMA = InputBox("RMA Number:", "Create Quotation Document", "")
        strDate = Format(Now(), "yyyy-mm-dd")
        strFileName = "CAR " & intRMA & " Spare Part Quote.doc"
        
        'Open the RMA Word Template
        appWord.Documents.Open ("C:\Documents and Settings\qtomlyc\My Documents\Attachments\NEW Spare Part Quote.doc")
        
        ' Make Word invisible through the Application object.
        appWord.Visible = False
        
        'Change date in document
        For i = 1 To 15
            Set Range = appWord.ActiveDocument.Content
            Range.Find.Execute FindText:="2007-XX-XX", ReplaceWith:=strDate
        Next i
        
        'Change reference of RMA in document
        For i = 1 To 15
            Set Range = appWord.ActiveDocument.Content
            Range.Find.Execute FindText:="XXXX", ReplaceWith:=intRMA
        Next i
        
        ' Save the new RMA document
        appWord.ActiveDocument.SaveAs "C:\Documents and Settings\qtomlyc\My Documents\Attachments\CAR " & intRMA & " Spare Part Quote.doc"
        
        ' Reminder to change header date and RMA number
        MsgBox "Header text not changed", vbOKOnly, "Create Quotation Document"
        
        ' Show the document to user in order to enter FAB numbers etc
        appWord.Visible = True
            
        ' Dispose objects to free memory
        Set appWord = Nothing
        Set Range = Nothing
    End Sub
    however, i cannot change header text or text inside of a table with this code. how should i modify it to find and replace text inside these elements too?

    best regards,

    bm
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

    Check this :

    [code=vb]
    Range.Find.Exec ute FindText:="XXXX ", ReplaceWith:=in tRMA,
    Replace:=wdRepl aceAll
    [/code]

    REgards
    Veena

    Comment

    • BlackMustard
      New Member
      • Aug 2007
      • 88

      #3
      unfortunately, nothing happened in the header...

      Comment

      • QVeen72
        Recognized Expert Top Contributor
        • Oct 2006
        • 1445

        #4
        Hi,

        First Change the Variable Name, from Range to TRange as Range would be the resrved Word (if adding Office References),
        Check some thing like this :

        [code=vb]
        Dim TRange As Range
        Set TRange = Word.ActiveDocu ment.Content
        With TRange.Find
        .Text = "OLD_TEXT"
        .Replacement.Te xt = "NEW_TEXT"
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLik e = False
        .MatchAllWordFo rms = False
        End With
        TRange.Find.Exe cute Replace:=wdRepl aceAll

        ' WRITE CODE TO SAVE HERE..
        [/code]

        Regads
        Veena

        Comment

        • BlackMustard
          New Member
          • Aug 2007
          • 88

          #5
          Originally posted by QVeen72
          Hi,

          First Change the Variable Name, from Range to TRange as Range would be the resrved Word (if adding Office References),
          Check some thing like this :

          [code=vb]
          Dim TRange As Range
          Set TRange = Word.ActiveDocu ment.Content
          With TRange.Find
          .Text = "OLD_TEXT"
          .Replacement.Te xt = "NEW_TEXT"
          .Forward = True
          .Wrap = wdFindAsk
          .Format = False
          .MatchCase = False
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLik e = False
          .MatchAllWordFo rms = False
          End With
          TRange.Find.Exe cute Replace:=wdRepl aceAll

          ' WRITE CODE TO SAVE HERE..
          [/code]

          Regads
          Veena
          still doesn't work. i got this code snippet from a collegue with even less vb knowledge than me and modified a little - could you tell me why the replace statement was in a loop to run 15 times?

          Comment

          • BlackMustard
            New Member
            • Aug 2007
            • 88

            #6
            just tilting this up a little...

            as stated previously, i need help with how to access the text in the header and footer of a word document programatically , via VBA, from Outlook.

            i also now need help with accessing a table, where i want to enter some information x number of times in a form and then write out a new table row for each entry.

            does anyone know how to do this?

            Comment

            • legcsabi
              New Member
              • Sep 2007
              • 1

              #7
              here is my working implementation (c#):

              ApplicationClas s wd = new ApplicationClas s();
              object fileName = t_filename;
              object newTemplate = false;
              object docType = 0;
              object isVisible = true;

              try
              {
              if(wd != null && wd.Documents != null)
              {
              document = wd.Documents.Op en(ref fileName, ref missing, ref missing, ref missing, ref missing,
              ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
              ref missing, ref missing, ref missing, ref missing, ref missing);
              }
              }
              catch(Exception ex)
              {
              Console.WriteLi ne(ex.ToString( ));
              }
              bool b = wd.ActiveDocume nt.Sections[1].Headers[WdHeaderFooterI ndex.wdHeaderFo oterPrimary].Exists;
              Range rg = wd.ActiveDocume nt.Sections[1].Headers[WdHeaderFooterI ndex.wdHeaderFo oterPrimary].Range;

              object Igaz = true;
              object Hamis = false;
              object replaceOpt = WdReplace.wdRep laceAll;

              object stringToSearch = "<OWNER>";
              object replaceWith = "ize";
              rg.Find.Execute (ref stringToSearch, ref Igaz, ref Igaz, ref missing, ref missing, ref missing,
              ref missing, ref missing, ref missing, ref replaceWith, ref replaceOpt, ref missing, ref missing,
              ref missing, ref missing);
              wd.Visible = true;

              Comment

              Working...