473,893 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

changing header and table text in word programmaticall y

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 3429
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#):

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

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

Similar topics

31
5741
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 calendar, 7 across by whatever needed down, and I manually create it each month - not a big deal. Every day I go in and darken the background color of the current day's cell by changing the appropriate <TD> entry to <TD bgcolor="c63800"> and...
6
6405
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 Access to change the data types for the fields from text to a number field or an Integer field. I'd be grateful for any advice you could give Many thanks David
2
4357
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 database. The table is a list of values that shouldn't be accidentally changed by the user, so I've taken the administer and delete permissions off. I'm using the regular workgroup file, with the default Admin user name, so there is no funky...
0
1109
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 the contents of one of the cells. By using Page.FindControl() I am getting an error message that it cannot find an instance of an object. I am giving the page code and code behind. I am trying to insert a literal control in the table cell with...
0
1615
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 put the structure in either createdatasource or opendatasource so I have to build it on the fly. The code I have written is as follows:
32
3715
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 RecordSource like this? Am I asking for trouble doing this with a memo field? Thanks in advance. Private Sub cmdNextNote_Click() Dim lngNid As Long If Me.Dirty Then DoCmd.RunCommand acCmdSaveRecord
2
2509
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 open the Word document via StreamReader and extract the text. Here's the problem, the users insist on leaving the "Track Changes" features on. Because of this, the raw text portion of the file contains the change history. I don't want the...
2
9280
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 different text field from a query and the cells expand or contract according to the size of the text field. I have tried "Can Shrink" but this leaves gaps and "Can Grow" but this seems to cause the cells to overlap and then Access throws page breaks...
2
6604
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 open the files and manually delete the data before importing them. Is there a way i can programatically do this? ie programmatically edit the text file and remove the surplus header text, and then import the file with the DoCmd.TransferText...
0
9982
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11243
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10927
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10469
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9645
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7173
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5858
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4280
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.