473,493 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

changing header and table text in word programmatically

88 New Member
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
6 3400
QVeen72
1,445 Recognized Expert Top Contributor
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
BlackMustard
88 New Member
unfortunately, nothing happened in the header...
Sep 10 '07 #3
QVeen72
1,445 Recognized Expert Top Contributor
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
BlackMustard
88 New Member
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
BlackMustard
88 New Member
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
legcsabi
1 New Member
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

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

Similar topics

31
5655
by: Arthur Shapiro | last post by:
I'm the webmaster for a recreational organization. As part of one page of the site, I have an HTML "Calendar at a Glance" of the organization's events for the month. It's a simple table of a...
6
6338
by: David Gartrell | last post by:
Hi i'm trying to import an Excel Spreadsheet into Access2000 but the data types for two of the fields in my imported table are being identified incorrectly. Is there a way of using some VB code in...
2
4333
by: Jozef | last post by:
Hello, Is there a way to change table permissions in VB Code? I can't seem to find much that's concise in the help file. Here's the situation; I have a table in the "data" portion of a split...
0
1091
by: Ramesh | last post by:
I am using a datalist control in my page and it has a header template. In the header template I have a table control with table rows and table cells. While the page is loading I am trying to change...
0
1580
by: Bob Smith | last post by:
I have existing word documents that we need to have the data document reflect whether we're running in development, test or production. The data header is longer than 255 characters, so I cannot...
32
3633
by: deko | last post by:
I have a popup form with a textbox that is bound to a memo field. I've been warned about memo fields so I'm wondering if I should use this code. Is there any risk with changing the form's...
2
2487
by: Kevin K | last post by:
Hi, I'm having a problem with extracting text from a Word document using StreamReader. As I'm developing a web application, I do NOT want the server to make calls to Word. I want to simply...
2
9241
by: Mo | last post by:
I am trying to produce an Access report which behaves like a two column table in Word. In other words each cell in column 1 contains a label and each cell in column 2 contains data from a...
2
6572
by: Cliff72 | last post by:
I'm creating a database that will be uploading some text files into an access table. The problem is that the text files have a header which messes up my import specs. so what i have had to do is to...
0
7119
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6989
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7367
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
285
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.