By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

changing header and table text in word programmatically

P: 88
hi,

i'm using the following code to modify a standard word document and save it to disk with a new file name.
Expand|Select|Wrap|Line Numbers
  1. Sub CreateQuote()
  2. '***************************************************************************
  3. 'Quotation part
  4. '***************************************************************************
  5.     Dim appWord As New Word.Application
  6.  
  7.     Dim intRMA, i As Integer
  8.     Dim strDate, strFileName As String
  9.     Dim Range As Range
  10.  
  11.     'Define the path where to save the RMA.doc
  12.     intRMA = InputBox("RMA Number:", "Create Quotation Document", "")
  13.     strDate = Format(Now(), "yyyy-mm-dd")
  14.     strFileName = "CAR " & intRMA & " Spare Part Quote.doc"
  15.  
  16.     'Open the RMA Word Template
  17.     appWord.Documents.Open ("C:\Documents and Settings\qtomlyc\My Documents\Attachments\NEW Spare Part Quote.doc")
  18.  
  19.     ' Make Word invisible through the Application object.
  20.     appWord.Visible = False
  21.  
  22.     'Change date in document
  23.     For i = 1 To 15
  24.         Set Range = appWord.ActiveDocument.Content
  25.         Range.Find.Execute FindText:="2007-XX-XX", ReplaceWith:=strDate
  26.     Next i
  27.  
  28.     'Change reference of RMA in document
  29.     For i = 1 To 15
  30.         Set Range = appWord.ActiveDocument.Content
  31.         Range.Find.Execute FindText:="XXXX", ReplaceWith:=intRMA
  32.     Next i
  33.  
  34.     ' Save the new RMA document
  35.     appWord.ActiveDocument.SaveAs "C:\Documents and Settings\qtomlyc\My Documents\Attachments\CAR " & intRMA & " Spare Part Quote.doc"
  36.  
  37.     ' Reminder to change header date and RMA number
  38.     MsgBox "Header text not changed", vbOKOnly, "Create Quotation Document"
  39.  
  40.     ' Show the document to user in order to enter FAB numbers etc
  41.     appWord.Visible = True
  42.  
  43.     ' Dispose objects to free memory
  44.     Set appWord = Nothing
  45.     Set Range = Nothing
  46. 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
Sep 10 '07 #1
Share this Question
Share on Google+
6 Replies


QVeen72
Expert 100+
P: 1,445
Hi,

Check this :

Expand|Select|Wrap|Line Numbers
  1.         Range.Find.Execute FindText:="XXXX", ReplaceWith:=intRMA,
  2. Replace:=wdReplaceAll
  3.  
REgards
Veena
Sep 10 '07 #2

P: 88
unfortunately, nothing happened in the header...
Sep 10 '07 #3

QVeen72
Expert 100+
P: 1,445
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 :

Expand|Select|Wrap|Line Numbers
  1.     Dim TRange As Range
  2.     Set TRange = Word.ActiveDocument.Content
  3.     With TRange.Find
  4.         .Text = "OLD_TEXT"
  5.         .Replacement.Text = "NEW_TEXT"
  6.         .Forward = True
  7.         .Wrap = wdFindAsk
  8.         .Format = False
  9.         .MatchCase = False
  10.         .MatchWholeWord = False
  11.         .MatchWildcards = False
  12.         .MatchSoundsLike = False
  13.         .MatchAllWordForms = False
  14.     End With
  15.     TRange.Find.Execute Replace:=wdReplaceAll
  16.  
  17.     ' WRITE CODE TO SAVE HERE..
  18.  
Regads
Veena
Sep 10 '07 #4

P: 88
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 :

Expand|Select|Wrap|Line Numbers
  1.     Dim TRange As Range
  2.     Set TRange = Word.ActiveDocument.Content
  3.     With TRange.Find
  4.         .Text = "OLD_TEXT"
  5.         .Replacement.Text = "NEW_TEXT"
  6.         .Forward = True
  7.         .Wrap = wdFindAsk
  8.         .Format = False
  9.         .MatchCase = False
  10.         .MatchWholeWord = False
  11.         .MatchWildcards = False
  12.         .MatchSoundsLike = False
  13.         .MatchAllWordForms = False
  14.     End With
  15.     TRange.Find.Execute Replace:=wdReplaceAll
  16.  
  17.     ' WRITE CODE TO SAVE HERE..
  18.  
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?
Sep 10 '07 #5

P: 88
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?
Sep 14 '07 #6

P: 1
here is my working implementation (c#):

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

try
{
if(wd != null && wd.Documents != null)
{
document = wd.Documents.Open(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.WriteLine(ex.ToString());
}
bool b = wd.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Exists;
Range rg = wd.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;

object Igaz = true;
object Hamis = false;
object replaceOpt = WdReplace.wdReplaceAll;

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;
Sep 25 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.