Word制作桌牌或席卡更换人名的宏函数
新建Word文档,用文本框制作好席卡,然后把电子表格里的人名,粘贴到第二页,点视图,多页显示。宏编辑器,输入代码
function testa(){
let stext = Application.Documents.Item(1).Tables.Item(1).Range.Columns.Item(1).Cells.Item(6).Range.Text
Application.Documents.Item(1).Shapes.Item(1).TextFrame.TextRange.Text = stext;
Application.Documents.Item(1).Shapes.Item(2).TextFrame.TextRange.Text = stext;
}
或者按人数复制文本框后,一次性替换人名。
function testa(){
let count = 4
for(var i =1;i<=count;i++){
let stext = Application.Documents.Item(1).Tables.Item(1).Range.Columns.Item(1).Cells.Item(i).Range.Text
Application.Documents.Item(1).Shapes.Item(2*i-1).TextFrame.TextRange.Text = stext;
Application.Documents.Item(1).Shapes.Item(2*i).TextFrame.TextRange.Text = stext;
}
}
修正 2025-08-12增加的函数,批量生成文本框,新建空白页,在新页面插入2个文本框。有需要的可以自行改进。
function test(){
let cards_count =2; //桌牌数量, 需为偶数
let cards_width = 16; //桌牌(席卡)宽度 单位:厘米
let cards_height = 4; //桌牌(席卡)高度 单位:厘米
let cardtocard_distance = 3; //桌牌(席卡)之间距离 单位:厘米
let cards_textfontsize = 16; //桌牌(席卡)文字大小
let cards_textfontname = "宋体"; //桌牌(席卡)文字 字体
let page_height = Application.ActiveDocument.PageSetup.PageHeight;
let page_width = Application.ActiveDocument.PageSetup.PageWidth;
let points_right = Application.ActiveDocument.PageSetup.RightMargin;
let points_left = Application.ActiveDocument.PageSetup.LeftMargin;
let points_top = Application.ActiveDocument.PageSetup.TopMargin;
let points_bottom = Application.ActiveDocument.PageSetup.BottomMargin;
cards_width =Application.CentimetersToPoints(cards_width);
cards_height =Application.CentimetersToPoints(cards_height);
cardtocard_distance = Application.CentimetersToPoints(cardtocard_distance);
page_width = page_width-points_left-points_right; //去掉左右边距后有效宽度
if (cards_width>page_width){
cards_width=page_width;
}else{
points_left =points_left + Math.floor((page_width-cards_width)/2); //文本框居中
}
page_height = page_height - points_top - points_bottom;//去掉上下边距后有效宽度
if(cards_height>Math.floor(page_height/2)){
cards_height = Math.floor(page_height/2)
}else{
points_top = points_top+Math.floor((page_height-cards_height*2-cardtocard_distance)/2);
}
for (var i=0;i<Math.floor(cards_count/2);i++){
Selection.Collapse(wdCollapseStart);
(obj=>{
obj.InsertBreak(wdSectionBreakNextPage); obj.SetRange(0, 0);
})(Selection); //插入新空白页,在新页面,插入两个卡片
let a = Application.Documents.Item(1).Shapes.AddTextbox(msoTextOrientationHorizontal,points_left,points_top,cards_width,cards_height)
a.WrapFormat.Type = wdWrapTopBottom;//上下型环绕; //a.TextFrame.WarpFormat 文本框里文本的环绕方式
let stext = Application.Documents.Item(1).Tables.Item(1).Range.Columns.Item(1).Cells.Item(Math.floor(i/2)+1).Range.Text;
stext = CleanString(stext).trim();
a.TextFrame.TextRange.Text =stext;
a.TextFrame.TextRange.Font.Size = cards_textfontsize;
a.TextFrame.TextRange.Font.Name = cards_textfontname;
a.TextFrame.TextRange.Paragraphs.Alignment = wdAlignParagraphCenter;
a.TextFrame.VerticalAnchor = msoAnchorMiddle // 文本框中文本,垂直对齐方式
let b_points_top =points_top + cardtocard_distance + cards_height;
let b = Application.Documents.Item(1).Shapes.AddTextbox(msoTextOrientationHorizontal,points_left,b_points_top,cards_width,cards_height)
b.WrapFormat.Type = wdWrapTopBottom;//上下型环绕; //a.TextFrame.WarpFormat 文本框里文本的环绕方式
b.TextFrame.TextRange.Text = stext;
b.TextFrame.TextRange.Font.Size = cards_textfontsize;
b.TextFrame.TextRange.Font.Name = cards_textfontname; //a.TextFrame.HorizontalAnchor = msoAnchorCenter //文本框中文本,水平对齐方式 msoHorizontalAnchor
//a.Select();
b.TextFrame.TextRange.Paragraphs.Alignment = wdAlignParagraphCenter;
b.TextFrame.VerticalAnchor = msoAnchorMiddle // 文本框中文本,垂直对齐方式
b.Rotation = 180 ;
}
}